1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-05-03 22:17:14 +08:00
golearn/examples/knnclassifier_iris.go

32 lines
799 B
Go
Raw Normal View History

2014-01-04 19:31:33 +00:00
package main
import (
2014-04-30 08:57:13 +01:00
"fmt"
2014-01-04 19:31:33 +00:00
2014-05-01 19:56:30 +01:00
data "github.com/sjwhitworth/golearn/data"
2014-04-30 22:13:07 +08:00
knnclass "github.com/sjwhitworth/golearn/knn"
util "github.com/sjwhitworth/golearn/utilities"
2014-04-30 08:57:13 +01:00
mat "github.com/skelterjohn/go.matrix"
)
2014-01-04 19:31:33 +00:00
2014-04-30 08:57:13 +01:00
func main() {
2014-01-04 19:31:33 +00:00
//Parses the infamous Iris data.
2014-05-01 19:56:30 +01:00
cols, rows, _, labels, data := data.ParseCsv("datasets/iris.csv", 4, []int{0, 1, 2})
2014-01-04 19:31:33 +00:00
//Initialises a new KNN classifier
knn := knnclass.KNNClassifier{}
2014-05-01 19:56:30 +01:00
knn.New(labels, data, rows, cols, "euclidean")
2014-04-30 08:57:13 +01:00
2014-01-04 19:31:33 +00:00
for {
//Creates a random array of N float64s between 0 and 7
2014-01-05 00:23:31 +00:00
randArray := util.RandomArray(3, 7)
2014-01-04 19:31:33 +00:00
//Initialises a vector with this array
2014-04-30 08:57:13 +01:00
random := mat.MakeDenseMatrix(randArray, 1, 3)
2014-01-04 19:31:33 +00:00
//Calculates the Euclidean distance and returns the most popular label
labels, _ := knn.Predict(random, 3)
fmt.Println(labels)
}
2014-04-30 08:57:13 +01:00
}