-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.go
244 lines (223 loc) · 6.46 KB
/
knn.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"strconv"
"strings"
)
var (
trainFile string = "data\\trains.txt" //data file
testFile string = "input" //data file
k int // k number of neigbours
manualVector string
)
type Flower struct {
Params []float64 `json:"params"`
Name string `json:"name"`
Distances []Distance `json:"distance"`
}
type Flowers struct {
Fl []Flower
}
type Distance struct {
Index int `json:"index"`
Distance float64 `json:"distance"`
Finded int `json:"finded"`
}
type FlowersNamesCount struct {
name string
count int
}
func init() {
flag.IntVar(&k, "k", k, "number of neigbours to analyze")
flag.StringVar(&trainFile, "tr", trainFile, "train-file")
flag.StringVar(&testFile, "ts", testFile, "test-file")
}
func readCsv(path string) ([][]string, error) {
var dataFile *os.File
var err error
if path != "input" {
dataFile, err = os.OpenFile(path, os.O_RDONLY, 0666)
}
// dataFile, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
return nil, err
}
if path != "input" {
defer dataFile.Close()
}
if err == nil {
var buf []byte
var rerr error
// Reading text from file
if path != "input" {
buf, rerr = ioutil.ReadAll(dataFile)
} else {
buf = []byte(manualVector)
rerr = nil
}
// buf, rerr := ioutil.ReadAll(dataFile)
if rerr != nil {
return nil, err
}
// Parsing from comma-separated text
r := csv.NewReader(strings.NewReader(string(buf)))
records, err := r.ReadAll()
if err != nil {
return nil, err
}
return records, nil
}
return nil, err
}
func convertStrArrayToJson(records [][]string) string {
// Converting from array of string to JSON
jsonData := ""
strNum := 0
paramsLength := len(records[0]) - 1
for _, record := range records {
wrongStr := false
if len(record) < paramsLength || len(record) > paramsLength+1 {
fmt.Println("Wrong parameters count")
wrongStr = true
}
flName := record[len(record)-1] // Cutting flower name
record = record[:len(record)-1]
strNum++
stringArray := "[" // Opening sq bracket
for _, arrField := range record { // Filling string representation of array
_, err := strconv.ParseFloat(arrField, 64)
if err != nil {
fmt.Println("Wrong parameters type in string: ", strNum)
wrongStr = true
}
stringArray += arrField + ","
}
stringArray = stringArray[:len(stringArray)-1] // Removing last ',' character
stringArray += "]" // Closing sq bracket
if !wrongStr {
jsonData += "{ \"name\": \"" + flName + "\", \"params\":" + stringArray + ", \"Distance\": [] }," // Converting to JSON
}
}
jsonData = "[" + jsonData[:len(jsonData)-1] + "]"
return jsonData
}
func (f *Flowers) readData(path string) {
records, err := readCsv(path)
if err == nil {
jsonData := convertStrArrayToJson(records)
json.Unmarshal([]byte(jsonData), &f.Fl)
} else {
fmt.Println("Read CSV error: " + err.Error())
os.Exit(1)
}
}
func (ts *Flowers) calcDistances(tr *Flowers) {
for i := 0; i < len(ts.Fl); i++ {
for j := 0; j < len(tr.Fl); j++ {
distance, err := euclideanDistance(tr.Fl[j].Params, ts.Fl[i].Params)
if err != nil {
fmt.Println("Error")
}
k := 0
// Inseting with sorting
for ; k < len(ts.Fl[i].Distances) && ts.Fl[i].Distances[k].Distance < distance; k++ {
}
// Add new element to slice
ts.Fl[i].Distances = append(ts.Fl[i].Distances, Distance{Index: -1, Distance: -1.0, Finded: 0})
copy(ts.Fl[i].Distances[k+1:], ts.Fl[i].Distances[k:]) // Move part after element for inserting
// Fill a new element
ts.Fl[i].Distances[k].Distance = distance // Fill distance (link)
ts.Fl[i].Distances[k].Index = j // Fill linked element
}
// fmt.Println(ts.Fl[i].Name, ts.Fl[i].Params, ts.Fl[i].Distances)
}
}
func (flower *Flower) groupByName(tr *Flowers, itt int) []FlowersNamesCount {
var trFlowers []FlowersNamesCount
// Calculate count flowers with the same name
for i := 0; i < len(flower.Distances) && i <= itt; i++ {
j := 0
for ; j < len(trFlowers); j++ {
ok := (trFlowers[j].name == tr.Fl[flower.Distances[i].Index].Name)
if ok {
trFlowers[j].count++
break
}
}
if j == len(trFlowers) {
trFlowers = append(trFlowers, FlowersNamesCount{name: tr.Fl[flower.Distances[i].Index].Name, count: 1})
}
}
return trFlowers
}
func euclideanDistance(x, y []float64) (float64, error) {
var sum float64
_len := len(x)
if _len != len(y) {
return 0, fmt.Errorf(("The length of vectors " + fmt.Sprint(x) + " and " + fmt.Sprint(y) + " has to be "))
}
for i := 0; i < _len; i++ {
sum += math.Pow(x[i]-y[i], 2)
}
return math.Sqrt(sum), nil
}
func printResult() {
var accuracy float64
var accuracies []float64
var counter int
tr := new(Flowers)
tr.readData(trainFile) // Filling train data
ts := new(Flowers)
if testFile == "input" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter the vector: ")
manualVector, _ = reader.ReadString('\n')
fmt.Print(manualVector)
}
ts.readData(testFile) // Filling testing data
ts.calcDistances(tr) // Calculating distances
if k > len(tr.Fl) {
k = len(tr.Fl)
}
for j := 0; j < k; j++ {
for i := 0; i < len(ts.Fl); i++ {
flower := ts.Fl[i]
// Looking for most often founded (using amount of elements with the same name)
trFlowers := flower.groupByName(tr, j+1)
maxCount := 0
maxName := ""
for l := 0; l < len(trFlowers); l++ {
if maxCount < trFlowers[l].count {
maxName = trFlowers[l].name
maxCount = trFlowers[l].count
}
}
// if names in ts and tr are different then hilight string with `diff`
diff := ""
if maxName != flower.Name {
diff = "diff"
counter++
}
fmt.Println("line ", i+1, "\t"+maxName+"\tFound:\t", maxCount, "\tcheck name from test:\t"+flower.Name+"\t", diff)
}
accuracy = (float64((len(ts.Fl) - counter)) / float64(len(ts.Fl))) * 100.0
accuracies = append(accuracies, accuracy)
counter = 0
s := fmt.Sprintf("%.2f", accuracy)
fmt.Println("k = ", j+1, " ", s+"%")
}
for g := 0; g < len(accuracies); g++ {
fmt.Println("Accuracy for k=", g+1, " ", accuracies[g], "%")
}
}
func main() {
flag.Parse() //Parse comandline arguments
printResult()
}