Saturday, September 30, 2017

Stats package on Golang

When we deal with data on the meaning of data analysis, data science, and machine learning, Go’s statistics package does a good job.

On the area of data science and machine learning, people usually use Python and R. On the internet, we can see just a few amount of information about data science with Go.

So I’ll leave concise memo about that.
Here I’ll introduce how to use basic statistics package.




Install


On the terminal, by following command, you can get stats package.

go get github.com/montanaflynn/stats

Simple exploring


To see how it works, let’s try some functions.

package main

import (
    "github.com/montanaflynn/stats"
    "fmt"
)

func main() {
    test := []float64{1.0, 2.0, 1.2, 4.2, 3.1}

    //min
    min, _ := stats.Min(test)
    fmt.Printf("mean: %v\n", min)

The variable, test, is the data to try function.
By stats.Min(), we can get minimum value of the slice.
On the same way, we can get statistical information of the data.


func main() {
    test := []float64{1.0, 2.0, 1.2, 4.2, 3.1}

    //min
    min, _ := stats.Min(test)
    fmt.Printf("mean: %v\n", min)
    //max
    max, _ := stats.Max(test)
    fmt.Printf("max: %v\n", max)
    //mean
    mean, _ := stats.Mean(test)
    fmt.Printf("mean: %v\n", mean)
    //median
    median, _ := stats.Median(test)
    fmt.Printf("median: %v\n", median)
    //variance
    variance, _ := stats.Variance(test)
    fmt.Printf("variance: %v\n", variance)
}

By executing the code, it prints the following.

mean: 1
max: 4.2
mean: 2.3
median: 2
variance: 1.4480000000000002

By stats package, statistical information can easily be obtained.

To check precisely, you should read the official document and README.
montanaflynn/stats