-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate.go
More file actions
216 lines (172 loc) · 4.76 KB
/
private.go
File metadata and controls
216 lines (172 loc) · 4.76 KB
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
package DecisionTree
import (
"math"
"reflect"
"github.com/SamuelCarroll/DataTypes"
)
// var classes []ClassAvg
// var classSamples [][]*dataTypes.Data
func avgClass(allData []*dataTypes.Data, classSamples [][]*dataTypes.Data, classes []ClassAvg) {
for _, datum := range allData {
classIndex := datum.Class - 1
classes[classIndex].averages = runningAvg(classes[classIndex].averages, *datum, classes[classIndex].count)
classes[classIndex].count++
classSamples[classIndex] = append(classSamples[classIndex], datum)
}
for i, class := range classes {
classes[i].stdDev = findStds(classSamples[i], class)
}
}
func getMajority(data []*dataTypes.Data) int {
count1, count2, count3 := 0, 0, 0
for _, datum := range data {
if datum.Class == 1 {
count1++
} else if datum.Class == 2 {
count2++
} else {
count3++
}
}
if count1 > count2 && count1 > count3 {
return 1
} else if count2 > count1 && count2 > count3 {
return 2
} else {
return 3
}
}
func stoppingCond(nodeData []*dataTypes.Data, stopCond float64) bool {
var count [3]int
var percent [3]float64
for _, elem := range nodeData {
count[elem.Class-1]++
}
for i := range count {
percent[i] = float64(count[i]) / float64(len(nodeData))
if percent[i] >= stopCond {
return true
}
}
return false
}
func findEntropy(valueIndex, classCount int, avg, stdDev float64, nodeData []*dataTypes.Data) float64 {
var classInstances []float64
var classEntropies []float64
var classWeights []float64
for i := 0; i < classCount; i++ {
classInstances = append(classInstances, 0.0)
classEntropies = append(classEntropies, 0.0)
classWeights = append(classWeights, 0.0)
}
for _, datum := range nodeData {
instance := GetFloatReflectVal(datum.FeatureSlice[valueIndex])
classIndex := datum.Class - 1
classInstances[classIndex] += countClass(instance, avg+stdDev)
}
lenData := float64(len(nodeData))
entropy := 0.0
for i := 0; i < classCount; i++ {
if classInstances[i] > 0 {
classWeights[i] = classInstances[i] / lenData
classEntropies[i] = classWeights[i] * math.Log2(classWeights[i])
entropy += classWeights[i] * classEntropies[i]
}
}
return entropy * -1
}
func countClass(instance float64, splitVal float64) float64 {
if instance < splitVal {
return 1
}
return 0
}
func initializeAvgs(example dataTypes.Data) []interface{} {
var newAvgVals []interface{}
for i := range example.FeatureSlice {
switch example.FeatureSlice[i].(type) {
case float64:
newAvgVals = append(newAvgVals, 0.0)
case bool:
newAvgVals = append(newAvgVals, false)
case string:
newAvgVals = append(newAvgVals, "")
}
}
return newAvgVals
}
//TODO generalize this
func findLeast(values []float64) (int, float64) {
leastIndex := 0
leastVal := values[0]
for i, val := range values {
if val < leastVal {
leastVal = val
leastIndex = i
}
}
return leastIndex, leastVal
}
func runningAvg(oldAvgs []interface{}, newVal dataTypes.Data, n int) []interface{} {
if len(oldAvgs) < len(newVal.FeatureSlice) {
oldAvgs = initializeAvgs(newVal)
}
for i := range newVal.FeatureSlice {
//reflect the type of the feature slice index handle float, bool and string (don't worry about bool and str yet)
switch val := oldAvgs[i].(type) {
case float64:
temp := float64(val) * float64(n)
temp += GetFloatReflectVal(newVal.FeatureSlice[i])
oldAvgs[i] = temp / float64(n+1)
}
}
return oldAvgs
}
func findStds(classSam []*dataTypes.Data, class ClassAvg) []interface{} {
var stdDev []interface{}
if len(classSam) == 0 {
return stdDev
}
featureLen := len(classSam[0].FeatureSlice)
for i := 0; i < featureLen; i++ {
classTotal := 0.0
for _, sample := range classSam {
class.stdDev = append(class.stdDev, 0.0)
//reflect the type of the feature slice index handle float, bool and string (don't worry about bool and str yet)
fsample := GetFloatReflectVal(sample.FeatureSlice[i])
fclass := GetFloatReflectVal(class.averages[i])
classTotal += math.Pow((fsample - fclass), 2)
}
stdDev = append(stdDev, classTotal/float64(class.count))
}
return stdDev
}
func findIndex(entropyVals []float64) int {
minVal := entropyVals[0]
minIndex := 0
for i, contender := range entropyVals {
if contender < minVal {
minIndex = i
minVal = contender
}
}
return minIndex
}
func GetFloatReflectVal(val interface{}) float64 {
v := reflect.ValueOf(val)
v = reflect.Indirect(v)
floatVal := v.Convert(reflect.TypeOf(0.0))
return floatVal.Float()
}
func GetBoolReflectVal(val interface{}) bool {
v := reflect.ValueOf(val)
v = reflect.Indirect(v)
boolVal := v.Convert(reflect.TypeOf(true))
return boolVal.Bool()
}
func GetStrReflectVal(val interface{}) string {
v := reflect.ValueOf(val)
v = reflect.Indirect(v)
strVal := v.Convert(reflect.TypeOf(""))
return strVal.String()
}