1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-28 13:48:56 +08:00

Starting interface design

This commit is contained in:
Stephen Whitworth 2014-04-30 08:57:13 +01:00
parent 0e86db820e
commit e1663eefa8
7 changed files with 125 additions and 46 deletions

15
base/predictor.go Normal file
View File

@ -0,0 +1,15 @@
package base
// Specifies the base interfaces
type Estimator interface {
Fit()
Summarise()
}
type Predictor interface {
Predict()
}
type Model interface {
Score()
}

57
data/csv.go Normal file
View File

@ -0,0 +1,57 @@
/* Data - consists of helper functions for parsing different data formats */
package data
import (
"encoding/csv"
"fmt"
"io"
"os"
"strconv"
)
//Parses a CSV file, returning the number of columns and rows, the headers, the labels associated with
//classification, and the data that will be used for training.
func ParseCsv(filepath string, label int, columns []int) (int, int, []string, []string, []float64) {
labels := make([]string, 0)
data := make([]float64, 0)
headers := make([]string, 0)
rows := 0
file, err := os.Open(filepath)
if err != nil {
fmt.Println("Error:", err)
}
defer file.Close()
reader := csv.NewReader(file)
headerrow, _ := reader.Read()
for _, col := range columns {
entry := headerrow[col]
headers = append(headers, entry)
}
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
fmt.Println("Error:", err)
}
//
labels = append(labels, record[label])
//Iterate over our rows and append the values to a slice
for _, col := range columns {
entry := record[col]
number, _ := strconv.ParseFloat(entry, 64)
data = append(data, number)
}
rows += 1
}
cols := len(columns)
return cols, rows, headers, labels, data
}

View File

@ -1,31 +1,30 @@
package main
import (
mat "github.com/skelterjohn/go.matrix"
base "golearn/base"
util "golearn/utilities"
knnclass "golearn/knn"
"fmt"
)
"fmt"
mat "github.com/skelterjohn/go.matrix"
"golearn/data"
util "golearn/utilities"
)
func main(){
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.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)
}
}
}

View File

@ -1,12 +1,12 @@
package knn
import (
mat "github.com/skelterjohn/go.matrix"
"math"
"fmt"
util "golearn/utilities"
base "golearn/base"
)
"fmt"
mat "github.com/skelterjohn/go.matrix"
base "golearn/base"
util "golearn/utilities"
"math"
)
//A KNN Classifier. Consists of a data matrix, associated labels in the same order as the matrix, and a name.
type KNNClassifier struct {
@ -15,11 +15,6 @@ 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")
// }
KNN.Data = *mat.MakeDenseMatrix(numbers, x, y)
KNN.Name = name
@ -54,7 +49,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 +72,4 @@ func (KNN *KNNClassifier) Predict(vector *mat.DenseMatrix, K int) (string, []int
label := sortedlabels[0]
return label, values
}
}

View File

@ -1,12 +1,12 @@
package knn
import (
mat "github.com/skelterjohn/go.matrix"
"math"
"fmt"
util "golearn/utilities"
base "golearn/base"
)
"fmt"
mat "github.com/skelterjohn/go.matrix"
base "golearn/base"
util "golearn/utilities"
"math"
)
//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 +49,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 +66,4 @@ func (KNN *KNNRegressor) Predict(vector *mat.DenseMatrix, K int) (float64, []int
average := sum / float64(K)
return average, values
}
}

13
lm/linear_regression.go Normal file
View File

@ -0,0 +1,13 @@
package lm
import (
"fmt"
mat "github.com/skelterjohn/go.matrix"
base "golearn/base"
util "golearn/utilities"
"math"
)
type LinearModel struct {
base.BaseRegressor
}

View File

@ -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
}
}