0

作为 numpy 的忠实粉丝,我很高兴地发现 golang 库正在开发中。我根据文档编写了一个小型测试程序,如下所示:

package main

import (

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

func main() {

    xs := []float64 {

        23.32, 44.32, 100.12, 191.90,
        23.22, 90.21, 12.22, 191.21,
        1.21, 12.21, 34.23, 91.02,
    }

    variance := stat.Variance(xs)
    fmt.Printf("Data: %v\n", xs)

    stddev := math.Sqrt(variance)

    fmt.Printf("Standard deviation: %d\n\n", stddev)
}

当我尝试构建程序时,我注意到以下编译器错误:

C:\>go build hello.go
# command-line-arguments
.hello.go:19:30: not enough arguments in call to stat.Variance
        have ([]float64)
        want ([]float64, []float64)

任何建议将不胜感激。

谢谢你。

4

1 回答 1

4

stat.Variance期望两个[]float64相同长度的类型参数:

func Variance(x, weights []float64) float64

您缺少weights参数。如果要将随机变量的所有权重设置为 1,则可以nil作为函数的第二个参数传递。stat.Variance

stat 包文档

于 2018-05-06T20:20:26.080 回答