-
Notifications
You must be signed in to change notification settings - Fork 26
/
kmeans_test.go
60 lines (56 loc) · 1.19 KB
/
kmeans_test.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
package kmeans
import (
"io/ioutil"
"log"
"path/filepath"
"strconv"
"strings"
"testing"
)
// Test K-Means Algorithm in Iris Dataset
func TestKmeans(t *testing.T) {
filePath, err := filepath.Abs("data/iris.csv")
if err != nil {
log.Fatal(err)
}
content, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(content), "\n")
irisData := make([][]float64, len(lines))
irisLabels := make([]string, len(lines))
for ii, line := range lines {
vector := strings.Split(line, ",")
label := vector[len(vector)-1]
vector = vector[:len(vector)-1]
floatVector := make([]float64, len(vector))
for jj := range vector {
floatVector[jj], err = strconv.ParseFloat(vector[jj], 64)
}
irisData[ii] = floatVector
irisLabels[ii] = label
}
threshold := 10
// Best Distance for Iris is Canberra Distance
labels, err := Kmeans(irisData, 3, CanberraDistance, threshold)
if err != nil {
log.Fatal(err)
}
misclassifiedOnes := 0
for ii, jj := range labels {
if ii < 50 {
if jj != 2 {
misclassifiedOnes++
}
} else if ii < 100 {
if jj != 1 {
misclassifiedOnes++
}
} else {
if jj != 0 {
misclassifiedOnes++
}
}
}
}