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

32 lines
823 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-05-03 23:08:43 +01:00
"github.com/gonum/matrix/mat64"
2014-05-01 19:56:30 +01:00
data "github.com/sjwhitworth/golearn/data"
2014-05-01 21:20:44 +01:00
knn "github.com/sjwhitworth/golearn/knn"
2014-04-30 22:13:07 +08:00
util "github.com/sjwhitworth/golearn/utilities"
)
func main() {
2014-01-05 00:23:31 +00:00
//Parses the infamous Iris data.
2014-05-01 19:56:30 +01:00
cols, rows, _, labels, data := data.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
2014-05-01 21:20:44 +01:00
cls := knn.NewKnnRegressor(newlabels, data, rows, cols, "euclidean")
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-05-03 23:08:43 +01:00
random := mat64.NewDense(1, 2, randArray)
2014-01-05 00:23:31 +00:00
//Calculates the Euclidean distance and returns the most popular label
2014-05-03 23:08:43 +01:00
outcome := cls.Predict(random, 3)
2014-01-05 00:23:31 +00:00
fmt.Println(outcome)
}
2014-04-30 22:13:07 +08:00
}