1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-26 13:49:14 +08:00

Added example documentation

This commit is contained in:
Stephen Whitworth 2014-01-04 19:31:33 +00:00
parent 84569019b8
commit 334c12385e
6 changed files with 32 additions and 22 deletions

BIN
examples/knn_iris Executable file

Binary file not shown.

30
examples/knn_iris.go Normal file
View File

@ -0,0 +1,30 @@
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)
//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)
}
}

View File

@ -1,4 +1,4 @@
package main
package knn
import (
mat "github.com/skelterjohn/go.matrix"
@ -6,7 +6,6 @@ import (
"fmt"
util "golearn/utilities"
base "golearn/base"
// "errors""
)
//A KNN Classifier. Consists of a data matrix, associated labels in the same order as the matrix, and a name.
@ -78,17 +77,4 @@ func (KNN *KNNClassifier) Predict(vector *mat.DenseMatrix, K int) (string, []int
label := sortedlabels[0]
return label, values
}
func main(){
cols, rows, _, labels, data := base.ParseCsv("../datasets/iris.csv", 4, []int{0,1,2})
knn := KNNClassifier{}
knn.New("Testing", labels, data, rows, cols)
for {
randArray := util.RandomArray(3)
random := mat.MakeDenseMatrix(randArray,1,3)
labels, _ := knn.Predict(random, 3)
fmt.Println(labels)
}
}

View File

@ -1,5 +1 @@
package knn
import (
"testing"
)
package knn

View File

@ -3,7 +3,6 @@ package utilities
import (
"sort"
rand "math/rand"
"fmt"
)
type sortedIntMap struct {
@ -71,6 +70,5 @@ func RandomArray(n int) []float64 {
for i := 0; i < n; i++ {
ReturnedArray[i] = rand.Float64() * float64(rand.Intn(7))
}
fmt.Println(ReturnedArray)
return ReturnedArray
}