gpt4 book ai didi

go - 使用自定义 Golang 库计算算术平均值

转载 作者:数据小太阳 更新时间:2023-10-29 03:36:47 25 4
gpt4 key购买 nike

package main

import (
"fmt"

maths "github.com/ematvey/go-fn/fn"
)

var avg float64

func main() {

A := []float64{2, 3, 5, 7, 11, 13}

avg := maths.ArithMean(A)
fmt.Println(avg)
}

我无法调用 ArithMean 函数。它给出了错误:

cannot use A (type []float64) as type *fn.Vector in argument to fn.ArithMean

最佳答案

package fn

type Vector

type Vector struct {
A []float64 // data
L int // length
}

例如,

package main

import (
"fmt"

maths "github.com/ematvey/go-fn/fn"
)

func main() {
a := []float64{2, 3, 5, 7, 11, 13}
v := &maths.Vector{A: a, L: len(a)}
avg := maths.ArithMean(v)
fmt.Println(avg)
}

输出:

6.833333333333333

其他统计软件包使用起来可能更容易、更直观。

例如,

func Mean

func Mean(x, weights []float64) float64

Mean computes the weighted mean of the data set.

sum_i {w_i * x_i} / sum_i {w_i}

If weights is nil then all of the weights are 1. If weights is not nil, then len(x) must equal len(weights).

package main

import (
"fmt"

"gonum.org/v1/gonum/stat"
)

func main() {
a := []float64{2, 3, 5, 7, 11, 13}
mean := stat.Mean(a, nil)
fmt.Println(mean)
}

输出:

6.833333333333333

关于go - 使用自定义 Golang 库计算算术平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com