1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-28 13:48:56 +08:00
golearn/examples/knnregressor_random.go

33 lines
854 B
Go
Raw Normal View History

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