Skip to content

Instantly share code, notes, and snippets.

@sjwhitworth
Last active January 2, 2016 07:19
Show Gist options
  • Select an option

  • Save sjwhitworth/8268926 to your computer and use it in GitHub Desktop.

Select an option

Save sjwhitworth/8268926 to your computer and use it in GitHub Desktop.
KNN classification of the Iris dataset using github.com/sjwhitworth/golearn
package main
import (
mat "github.com/skelterjohn/go.matrix"
base "golearn/base"
util "golearn/utilities"
knnclass "golearn/knn"
"fmt"
)
func main(){
//Parses the infamous Iris data.
cols, rows, _, labels, data := base.ParseCsv("datasets/iris.csv", 4, []int{0,1,2})
//Initialises a new KNN classifier
knn := knnclass.KNNClassifier{}
knn.New("Testing", labels, data, rows, cols)
for {
//Creates a random array of N float64s between 0 and 7
randArray := util.RandomArray(3, 7)
//Initialises a vector with this array
random := mat.MakeDenseMatrix(randArray,1,3)
//Calculates the Euclidean distance and returns the most popular label
labels, _ := knn.Predict(random, 3)
fmt.Println(labels)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment