Skip to content

Commit

Permalink
Merge pull request sjwhitworth#8 from sjwhitworth/organizing
Browse files Browse the repository at this point in the history
Add package file, rewrite import.
  • Loading branch information
sjwhitworth committed May 1, 2014
2 parents e7c19c8 + 0f07475 commit e27f25f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 54 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ GoLearn

A small start on a machine learning library in Go.

Install
=======

```
go get github.com/sjwhitworth/golearn
cd src/github.com/sjwhitworth/golearn
go get ./...
```

Examples
=======

```
cd examples/
go run knnclassifier_iris.go
```

Join the team
=============

Expand Down
25 changes: 13 additions & 12 deletions examples/knnclassifier_iris.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
23 changes: 12 additions & 11 deletions examples/knnregressor_random.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
1 change: 1 addition & 0 deletions golearn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package golearn
20 changes: 11 additions & 9 deletions knn/knn.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -77,4 +79,4 @@ func (KNN *KNNClassifier) Predict(vector *mat.DenseMatrix, K int) (string, []int
label := sortedlabels[0]

return label, values
}
}
17 changes: 9 additions & 8 deletions knn/knnregressor.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -66,4 +67,4 @@ func (KNN *KNNRegressor) Predict(vector *mat.DenseMatrix, K int) (float64, []int

average := sum / float64(K)
return average, values
}
}
28 changes: 14 additions & 14 deletions utilities/utilities.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -87,4 +87,4 @@ func ConvertLabelsToFloat(labels []string) []float64 {
floats = append(floats, converted)
}
return floats
}
}

0 comments on commit e27f25f

Please sign in to comment.