简体   繁体   中英

Declaring types in Go for a sample gonum application

Being an ardent fan of numpy, I was pleased to discover that a library for golang was in progress. I wrote a small test program, based heavily on the documentation, that looks like the following:

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)
}

When I attempted to build the program, I noticed the following compiler error:

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)

Any advice would be most appreciated.

Thank you.

stat.Variance expects two parameters of type []float64 of the same length:

func Variance(x, weights []float64) float64

You are missing the weights parameter. You can pass nil as the second parameter of stat.Variance function if you wants to set all the weights of the random variables to 1.

stat Package Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM