mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-28 13:48:56 +08:00
Add package file, rewrite import.
This commit is contained in:
parent
e7c19c8975
commit
0c2187763f
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
golearn.go
Normal file
1
golearn.go
Normal file
@ -0,0 +1 @@
|
||||
package golearn
|
20
knn/knn.go
20
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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user