From 0c2187763f3eac1dc5405b63978203304d79c1be Mon Sep 17 00:00:00 2001 From: Bert Chang Date: Wed, 30 Apr 2014 22:13:07 +0800 Subject: [PATCH] Add package file, rewrite import. --- examples/knnclassifier_iris.go | 25 +++++++++++++------------ examples/knnregressor_random.go | 23 ++++++++++++----------- golearn.go | 1 + knn/knn.go | 20 +++++++++++--------- knn/knnregressor.go | 17 +++++++++-------- utilities/utilities.go | 28 ++++++++++++++-------------- 6 files changed, 60 insertions(+), 54 deletions(-) create mode 100644 golearn.go diff --git a/examples/knnclassifier_iris.go b/examples/knnclassifier_iris.go index 8511372..5b4bc14 100644 --- a/examples/knnclassifier_iris.go +++ b/examples/knnclassifier_iris.go @@ -1,31 +1,32 @@ package main import ( - mat "github.com/skelterjohn/go.matrix" - base "golearn/base" - util "golearn/utilities" - knnclass "golearn/knn" - "fmt" - ) + "fmt" -func main(){ + 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() { //Parses the infamous Iris data. - cols, rows, _, labels, data := base.ParseCsv("datasets/iris.csv", 4, []int{0,1,2}) + cols, rows, _, labels, data := base.ParseCsv("datasets/iris.csv", 4, []int{0, 1, 2}) //Initialises a new KNN classifier knn := knnclass.KNNClassifier{} - knn.C + // knn.C 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) + random := mat.MakeDenseMatrix(randArray, 1, 3) //Calculates the Euclidean distance and returns the most popular label labels, _ := knn.Predict(random, 3) fmt.Println(labels) } -} \ No newline at end of file +} diff --git a/examples/knnregressor_random.go b/examples/knnregressor_random.go index dccf2cc..55e2332 100644 --- a/examples/knnregressor_random.go +++ b/examples/knnregressor_random.go @@ -1,31 +1,32 @@ package main import ( - mat "github.com/skelterjohn/go.matrix" - base "golearn/base" - util "golearn/utilities" - knnclass "golearn/knn" - "fmt" - ) + "fmt" -func main(){ + 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() { //Parses the infamous Iris data. - cols, rows, _, labels, data := base.ParseCsv("datasets/randomdata.csv", 2, []int{0,1}) + cols, rows, _, labels, data := base.ParseCsv("datasets/randomdata.csv", 2, []int{0, 1}) newlabels := util.ConvertLabelsToFloat(labels) //Initialises a new KNN classifier knn := knnclass.KNNRegressor{} knn.New("Testing", newlabels, data, rows, cols) - + for { //Creates a random array of N float64s between 0 and Y randArray := util.RandomArray(2, 100) //Initialises a vector with this array - random := mat.MakeDenseMatrix(randArray,1,2) + random := mat.MakeDenseMatrix(randArray, 1, 2) //Calculates the Euclidean distance and returns the most popular label outcome, _ := knn.Predict(random, 3) fmt.Println(outcome) } -} \ No newline at end of file +} diff --git a/golearn.go b/golearn.go new file mode 100644 index 0000000..7072cd8 --- /dev/null +++ b/golearn.go @@ -0,0 +1 @@ +package golearn diff --git a/knn/knn.go b/knn/knn.go index 8536c5b..b6db1b8 100644 --- a/knn/knn.go +++ b/knn/knn.go @@ -1,12 +1,14 @@ package knn import ( - mat "github.com/skelterjohn/go.matrix" - "math" - "fmt" - util "golearn/utilities" - base "golearn/base" - ) + "fmt" + "math" + + base "github.com/sjwhitworth/golearn/base" + util "github.com/sjwhitworth/golearn/utilities" + + mat "github.com/skelterjohn/go.matrix" +) //A KNN Classifier. Consists of a data matrix, associated labels in the same order as the matrix, and a name. type KNNClassifier struct { @@ -15,7 +17,7 @@ type KNNClassifier struct { //Mints a new classifier. func (KNN *KNNClassifier) New(name string, labels []string, numbers []float64, x int, y int) { - + //Write in some error handling here // if x != len(KNN.Labels) { // return errors.New("KNN: There must be a label for each row") @@ -54,7 +56,7 @@ func (KNN *KNNClassifier) Predict(vector *mat.DenseMatrix, K int) (string, []int labels := make([]string, 0) maxmap := make(map[string]int) - for i := 0; i < rows; i++{ + for i := 0; i < rows; i++ { row := KNN.Data.GetRowVector(i) eucdistance := KNN.ComputeDistance(row, vector) rownumbers[i] = eucdistance @@ -77,4 +79,4 @@ func (KNN *KNNClassifier) Predict(vector *mat.DenseMatrix, K int) (string, []int label := sortedlabels[0] return label, values -} \ No newline at end of file +} diff --git a/knn/knnregressor.go b/knn/knnregressor.go index 5f7c2f4..1837d67 100644 --- a/knn/knnregressor.go +++ b/knn/knnregressor.go @@ -1,12 +1,13 @@ package knn import ( - mat "github.com/skelterjohn/go.matrix" - "math" - "fmt" - util "golearn/utilities" - base "golearn/base" - ) + "fmt" + "math" + + base "github.com/sjwhitworth/golearn/base" + util "github.com/sjwhitworth/golearn/utilities" + mat "github.com/skelterjohn/go.matrix" +) //A KNN Regressor. Consists of a data matrix, associated result variables in the same order as the matrix, and a name. type KNNRegressor struct { @@ -49,7 +50,7 @@ func (KNN *KNNRegressor) Predict(vector *mat.DenseMatrix, K int) (float64, []int labels := make([]float64, 1) sum := 0.0 - for i := 0; i < rows; i++{ + for i := 0; i < rows; i++ { row := KNN.Data.GetRowVector(i) eucdistance := KNN.ComputeDistance(row, vector) rownumbers[i] = eucdistance @@ -66,4 +67,4 @@ func (KNN *KNNRegressor) Predict(vector *mat.DenseMatrix, K int) (float64, []int average := sum / float64(K) return average, values -} \ No newline at end of file +} diff --git a/utilities/utilities.go b/utilities/utilities.go index d018ada..601c79a 100644 --- a/utilities/utilities.go +++ b/utilities/utilities.go @@ -1,29 +1,29 @@ package utilities import ( - "sort" - rand "math/rand" - "fmt" - "strconv" - ) + "fmt" + rand "math/rand" + "sort" + "strconv" +) type sortedIntMap struct { m map[int]float64 s []int } - + func (sm *sortedIntMap) Len() int { return len(sm.m) } - + func (sm *sortedIntMap) Less(i, j int) bool { return sm.m[sm.s[i]] < sm.m[sm.s[j]] } - + func (sm *sortedIntMap) Swap(i, j int) { sm.s[i], sm.s[j] = sm.s[j], sm.s[i] } - + func SortIntMap(m map[int]float64) []int { sm := new(sortedIntMap) sm.m = m @@ -41,19 +41,19 @@ type sortedStringMap struct { m map[string]int s []string } - + func (sm *sortedStringMap) Len() int { return len(sm.m) } - + func (sm *sortedStringMap) Less(i, j int) bool { return sm.m[sm.s[i]] < sm.m[sm.s[j]] } - + func (sm *sortedStringMap) Swap(i, j int) { sm.s[i], sm.s[j] = sm.s[j], sm.s[i] } - + func SortStringMap(m map[string]int) []string { sm := new(sortedStringMap) sm.m = m @@ -87,4 +87,4 @@ func ConvertLabelsToFloat(labels []string) []float64 { floats = append(floats, converted) } return floats -} \ No newline at end of file +}