-
Notifications
You must be signed in to change notification settings - Fork 13
/
golang-k-nn-speedup.go
114 lines (87 loc) · 2.21 KB
/
golang-k-nn-speedup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"bytes"
"fmt"
"io/ioutil"
"math"
"runtime"
"strconv"
)
type LabelWithFeatures struct {
Label []byte
Features []float32
}
func NewLabelWithFeatures(parsedLine [][]byte) LabelWithFeatures {
label := parsedLine[0]
features := make([]float32, len(parsedLine)-1)
for i, feature := range parsedLine {
// skip label
if i == 0 {
continue
}
features[i-1] = byteSliceTofloat32(feature)
}
return LabelWithFeatures{label, features}
}
var newline = []byte("\n")
var comma = []byte(",")
func byteSliceTofloat32(b []byte) float32 {
x, _ := strconv.ParseFloat(string(b), 32) //10, 8)
return float32(x)
}
func parseCSVFile(filePath string) []LabelWithFeatures {
fileContent, _ := ioutil.ReadFile(filePath)
lines := bytes.Split(fileContent, newline)
numRows := len(lines)
labelsWithFeatures := make([]LabelWithFeatures, numRows-2)
for i, line := range lines {
// skip headers
if i == 0 || i == numRows-1 {
continue
}
labelsWithFeatures[i-1] = NewLabelWithFeatures(bytes.Split(line, comma))
}
return labelsWithFeatures
}
func squareDistanceWithBailout(features1, features2 []float32, bailout float32) (d float32) {
for i := 0; i < len(features1); i++ {
x := features1[i] - features2[i]
d += x * x
if d > bailout {
break
}
}
return
}
var trainingSample = parseCSVFile("trainingsample.csv")
func classify(features []float32) (label []byte) {
label = trainingSample[0].Label
d := squareDistanceWithBailout(features, trainingSample[0].Features, math.MaxFloat32)
for _, row := range trainingSample {
dNew := squareDistanceWithBailout(features, row.Features, d)
if dNew < d {
label = row.Label
d = dNew
}
}
return
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
validationSample := parseCSVFile("validationsample.csv")
var totalCorrect float32 = 0
successChannel := make(chan float32)
for _, test := range validationSample {
go func(t LabelWithFeatures) {
if string(t.Label) == string(classify(t.Features)) {
successChannel <- 1
} else {
successChannel <- 0
}
}(test)
}
for i := 0; i < len(validationSample); i++ {
totalCorrect += <-successChannel
}
fmt.Println(float32(totalCorrect) / float32(len(validationSample)))
}