forked from sjwhitworth/golearn
/
id3.go
591 lines (529 loc) · 15.4 KB
/
id3.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package trees
import (
"bytes"
"encoding/json"
"fmt"
"github.com/antongulenko/golearn/base"
"github.com/antongulenko/golearn/evaluation"
"sort"
)
// NodeType determines whether a DecisionTreeNode is a leaf or not.
type NodeType int
const (
// LeafNode means there are no children
LeafNode NodeType = 1
// RuleNode means we should look at the next attribute value
RuleNode NodeType = 2
)
// RuleGenerator implementations analyse instances and determine
// the best value to split on.
type RuleGenerator interface {
GenerateSplitRule(base.FixedDataGrid) *DecisionTreeRule
}
// DecisionTreeRule represents the "decision" in "decision tree".
type DecisionTreeRule struct {
SplitAttr base.Attribute `json:"split_attribute"`
SplitVal float64 `json:"split_val"`
}
func (d *DecisionTreeRule) MarshalJSON() ([]byte, error) {
ret := make(map[string]interface{})
if d.SplitAttr == nil {
ret["split_attribute"] = "unknown"
ret["split_val"] = 0
return json.Marshal(ret)
}
marshaledSplitAttrRaw, err := d.SplitAttr.MarshalJSON()
if err != nil {
return nil, err
}
marshaledSplitAttr := make(map[string]interface{})
err = json.Unmarshal(marshaledSplitAttrRaw, &marshaledSplitAttr)
if err != nil {
panic(err)
}
ret["split_attribute"] = marshaledSplitAttr
ret["split_val"] = d.SplitVal
return json.Marshal(ret)
}
func (d *DecisionTreeRule) unmarshalJSON(data []byte) error {
var jsonMap map[string]interface{}
err := json.Unmarshal(data, &jsonMap)
if err != nil {
return err
}
if splitVal, ok := jsonMap["split_val"]; ok {
d.SplitVal = splitVal.(float64)
}
split := jsonMap["split_attribute"]
splitBytes, err := json.Marshal(split)
if err != nil {
panic(err)
}
if string(splitBytes) != "\"unknown\"" {
d.SplitAttr, err = base.DeserializeAttribute(splitBytes)
if err != nil {
return err
}
if d.SplitAttr == nil {
panic("Should not be nil")
return fmt.Errorf("base.DeserializeAttribute returned nil")
}
} else {
d.SplitAttr = nil
}
return nil
}
func (d *DecisionTreeRule) UnmarshalJSON(data []byte) error {
ret := d.unmarshalJSON(data)
return ret
}
// String prints a human-readable summary of this thing.
func (d *DecisionTreeRule) String() string {
if d.SplitAttr == nil {
return fmt.Sprintf("INVALID:DecisionTreeRule(SplitAttr is nil)")
}
if _, ok := d.SplitAttr.(*base.FloatAttribute); ok {
return fmt.Sprintf("DecisionTreeRule(%s <= %f)", d.SplitAttr.GetName(), d.SplitVal)
}
return fmt.Sprintf("DecisionTreeRule(%s)", d.SplitAttr.GetName())
}
// DecisionTreeNode represents a given portion of a decision tree.
type DecisionTreeNode struct {
Type NodeType `json:"node_type"`
Children map[string]*DecisionTreeNode `json:"children"`
ClassDist map[string]int `json:"class_dist"`
Class string `json:"class_string"`
ClassAttr base.Attribute `json:"-"`
SplitRule *DecisionTreeRule `json:"decision_tree_rule"`
}
func getClassAttr(from base.FixedDataGrid) base.Attribute {
allClassAttrs := from.AllClassAttributes()
return allClassAttrs[0]
}
// Save sends the classification tree to an output file
func (d *DecisionTreeNode) Save(filePath string) error {
metadata := base.ClassifierMetadataV1{
FormatVersion: 1,
ClassifierName: "test",
ClassifierVersion: "1",
}
serializer, err := base.CreateSerializedClassifierStub(filePath, metadata)
if err != nil {
return err
}
err = d.SaveWithPrefix(serializer, "")
if err != nil {
return err
}
return serializer.Close()
}
func (d *DecisionTreeNode) SaveWithPrefix(writer *base.ClassifierSerializer, prefix string) error {
b, err := json.Marshal(d)
if err != nil {
return err
}
err = writer.WriteBytesForKey(writer.Prefix(prefix, "tree"), b)
if err != nil {
return err
}
err = writer.WriteAttributeForKey(writer.Prefix(prefix, "treeClassAttr"), d.ClassAttr)
if err != nil {
return err
}
return nil
}
// Load reads from the classifier from an output file
func (d *DecisionTreeNode) Load(filePath string) error {
reader, err := base.ReadSerializedClassifierStub(filePath)
if err != nil {
return err
}
return d.LoadWithPrefix(reader, "")
}
// LoadWithPrefix reads from the classifier from part of another model
func (d *DecisionTreeNode) LoadWithPrefix(reader *base.ClassifierDeserializer, prefix string) error {
b, err := reader.GetBytesForKey(reader.Prefix(prefix, "tree"))
if err != nil {
return err
}
err = json.Unmarshal(b, d)
if err != nil {
return err
}
a, err := reader.GetAttributeForKey(reader.Prefix(prefix, "treeClassAttr"))
if err != nil {
return err
}
d.ClassAttr = a
return nil
}
// InferID3Tree builds a decision tree using a RuleGenerator
// from a set of Instances (implements the ID3 algorithm)
func InferID3Tree(from base.FixedDataGrid, with RuleGenerator) *DecisionTreeNode {
// Count the number of classes at this node
classes := base.GetClassDistribution(from)
classAttr := getClassAttr(from)
// If there's only one class, return a DecisionTreeLeaf with
// the only class available
if len(classes) == 1 {
maxClass := ""
for i := range classes {
maxClass = i
}
ret := &DecisionTreeNode{
LeafNode,
nil,
classes,
maxClass,
classAttr,
&DecisionTreeRule{nil, 0.0},
}
return ret
}
// Only have the class attribute
maxVal := 0
maxClass := ""
for i := range classes {
if classes[i] > maxVal {
maxClass = i
maxVal = classes[i]
}
}
// If there are no more Attributes left to split on,
// return a DecisionTreeLeaf with the majority class
cols, _ := from.Size()
if cols == 2 {
ret := &DecisionTreeNode{
LeafNode,
nil,
classes,
maxClass,
classAttr,
&DecisionTreeRule{nil, 0.0},
}
return ret
}
// Generate a return structure
ret := &DecisionTreeNode{
RuleNode,
nil,
classes,
maxClass,
classAttr,
nil,
}
// Generate the splitting rule
splitRule := with.GenerateSplitRule(from)
if splitRule == nil || splitRule.SplitAttr == nil {
// Can't determine, just return what we have
return ret
}
// Split the attributes based on this attribute's value
var splitInstances map[string]base.FixedDataGrid
if _, ok := splitRule.SplitAttr.(*base.FloatAttribute); ok {
splitInstances = base.DecomposeOnNumericAttributeThreshold(from,
splitRule.SplitAttr, splitRule.SplitVal)
} else {
splitInstances = base.DecomposeOnAttributeValues(from, splitRule.SplitAttr)
}
// Create new children from these attributes
ret.Children = make(map[string]*DecisionTreeNode)
for k := range splitInstances {
newInstances := splitInstances[k]
ret.Children[k] = InferID3Tree(newInstances, with)
}
ret.SplitRule = splitRule
return ret
}
// getNestedString returns the contents of node d
// prefixed by level number of tags (also prints children)
func (d *DecisionTreeNode) getNestedString(level int) string {
buf := bytes.NewBuffer(nil)
tmp := bytes.NewBuffer(nil)
for i := 0; i < level; i++ {
tmp.WriteString("\t")
}
buf.WriteString(tmp.String())
if d.Children == nil {
buf.WriteString(fmt.Sprintf("Leaf(%s)", d.Class))
} else {
var keys []string
buf.WriteString(fmt.Sprintf("Rule(%s)", d.SplitRule))
for k := range d.Children {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
buf.WriteString("\n")
buf.WriteString(tmp.String())
buf.WriteString("\t")
buf.WriteString(k)
buf.WriteString("\n")
buf.WriteString(d.Children[k].getNestedString(level + 1))
}
}
return buf.String()
}
// String returns a human-readable representation of a given node
// and it's children
func (d *DecisionTreeNode) String() string {
return d.getNestedString(0)
}
// computeAccuracy is a helper method for Prune()
func computeAccuracy(predictions base.FixedDataGrid, from base.FixedDataGrid) float64 {
cf, _ := evaluation.GetConfusionMatrix(from, predictions)
return evaluation.GetAccuracy(cf)
}
// Prune eliminates branches which hurt accuracy
func (d *DecisionTreeNode) Prune(using base.FixedDataGrid) {
// If you're a leaf, you're already pruned
if d.Children == nil {
return
}
if d.SplitRule == nil {
return
}
// Recursively prune children of this node
sub := base.DecomposeOnAttributeValues(using, d.SplitRule.SplitAttr)
for k := range d.Children {
if sub[k] == nil {
continue
}
subH, subV := sub[k].Size()
if subH == 0 || subV == 0 {
continue
}
d.Children[k].Prune(sub[k])
}
// Get a baseline accuracy
predictions, _ := d.Predict(using)
baselineAccuracy := computeAccuracy(predictions, using)
// Speculatively remove the children and re-evaluate
tmpChildren := d.Children
d.Children = nil
predictions, _ = d.Predict(using)
newAccuracy := computeAccuracy(predictions, using)
// Keep the children removed if better, else restore
if newAccuracy < baselineAccuracy {
d.Children = tmpChildren
}
}
// Predict outputs a base.Instances containing predictions from this tree
func (d *DecisionTreeNode) Predict(what base.FixedDataGrid) (base.FixedDataGrid, error) {
predictions := base.GeneratePredictionVector(what)
classAttr := getClassAttr(predictions)
classAttrSpec, err := predictions.GetAttribute(classAttr)
if err != nil {
panic(err)
}
predAttrs := base.AttributeDifferenceReferences(what.AllAttributes(), predictions.AllClassAttributes())
predAttrSpecs := base.ResolveAttributes(what, predAttrs)
what.MapOverRows(predAttrSpecs, func(row [][]byte, rowNo int) (bool, error) {
cur := d
for {
if cur.Children == nil {
predictions.Set(classAttrSpec, rowNo, classAttr.GetSysValFromString(cur.Class))
break
} else {
splitVal := cur.SplitRule.SplitVal
at := cur.SplitRule.SplitAttr
ats, err := what.GetAttribute(at)
if err != nil {
//predictions.Set(classAttrSpec, rowNo, classAttr.GetSysValFromString(cur.Class))
//break
panic(err)
}
var classVar string
if _, ok := ats.GetAttribute().(*base.FloatAttribute); ok {
// If it's a numeric Attribute (e.g. FloatAttribute) check that
// the value of the current node is greater than the old one
classVal := base.UnpackBytesToFloat(what.Get(ats, rowNo))
if classVal > splitVal {
classVar = "1"
} else {
classVar = "0"
}
} else {
classVar = ats.GetAttribute().GetStringFromSysVal(what.Get(ats, rowNo))
}
if next, ok := cur.Children[classVar]; ok {
cur = next
} else {
// Suspicious of this
var bestChild string
for c := range cur.Children {
bestChild = c
if c > classVar {
break
}
}
cur = cur.Children[bestChild]
}
}
}
return true, nil
})
return predictions, nil
}
type ClassProba struct {
Probability float64
ClassValue string
}
type ClassesProba []ClassProba
func (o ClassesProba) Len() int {
return len(o)
}
func (o ClassesProba) Swap(i, j int) {
o[i], o[j] = o[j], o[i]
}
func (o ClassesProba) Less(i, j int) bool {
return o[i].Probability > o[j].Probability
}
// Predict class probabilities of the input samples what, returns a sorted array (by probability) of classes, and another array representing it's probabilities
func (t *ID3DecisionTree) PredictProba(what base.FixedDataGrid) (ClassesProba, error) {
d := t.Root
predictions := base.GeneratePredictionVector(what)
predAttrs := base.AttributeDifferenceReferences(what.AllAttributes(), predictions.AllClassAttributes())
predAttrSpecs := base.ResolveAttributes(what, predAttrs)
_, rowCount := what.Size()
if rowCount > 1 {
panic("PredictProba supports only 1 row predictions")
}
var results ClassesProba
what.MapOverRows(predAttrSpecs, func(row [][]byte, rowNo int) (bool, error) {
cur := d
for {
if cur.Children == nil {
totalDist := 0
for _, dist := range cur.ClassDist {
totalDist += dist
}
for class, dist := range cur.ClassDist {
classProba := ClassProba{ClassValue: class, Probability: float64(float64(dist) / float64(totalDist))}
results = append(results, classProba)
}
sort.Sort(results)
break
} else {
splitVal := cur.SplitRule.SplitVal
at := cur.SplitRule.SplitAttr
ats, err := what.GetAttribute(at)
if err != nil {
//predictions.Set(classAttrSpec, rowNo, classAttr.GetSysValFromString(cur.Class))
//break
panic(err)
}
var classVar string
if _, ok := ats.GetAttribute().(*base.FloatAttribute); ok {
// If it's a numeric Attribute (e.g. FloatAttribute) check that
// the value of the current node is greater than the old one
classVal := base.UnpackBytesToFloat(what.Get(ats, rowNo))
if classVal > splitVal {
classVar = "1"
} else {
classVar = "0"
}
} else {
classVar = ats.GetAttribute().GetStringFromSysVal(what.Get(ats, rowNo))
}
if next, ok := cur.Children[classVar]; ok {
cur = next
} else {
// Suspicious of this
var bestChild string
for c := range cur.Children {
bestChild = c
if c > classVar {
break
}
}
cur = cur.Children[bestChild]
}
}
}
return true, nil
})
return results, nil
}
//
// ID3 Tree type
//
// ID3DecisionTree represents an ID3-based decision tree
// using the Information Gain metric to select which attributes
// to split on at each node.
type ID3DecisionTree struct {
base.BaseClassifier
Root *DecisionTreeNode
PruneSplit float64
Rule RuleGenerator
}
// NewID3DecisionTree returns a new ID3DecisionTree with the specified test-prune
// ratio and InformationGain as the rule generator.
// If the ratio is less than 0.001, the tree isn't pruned.
func NewID3DecisionTree(prune float64) *ID3DecisionTree {
return &ID3DecisionTree{
base.BaseClassifier{},
nil,
prune,
new(InformationGainRuleGenerator),
}
}
// NewID3DecisionTreeFromRule returns a new ID3DecisionTree with the specified test-prun
// ratio and the given rule gnereator.
func NewID3DecisionTreeFromRule(prune float64, rule RuleGenerator) *ID3DecisionTree {
return &ID3DecisionTree{
base.BaseClassifier{},
nil,
prune,
rule,
}
}
// Fit builds the ID3 decision tree
func (t *ID3DecisionTree) Fit(on base.FixedDataGrid) error {
if t.PruneSplit > 0.001 {
trainData, testData := base.InstancesTrainTestSplit(on, t.PruneSplit)
t.Root = InferID3Tree(trainData, t.Rule)
t.Root.Prune(testData)
} else {
t.Root = InferID3Tree(on, t.Rule)
}
return nil
}
// Predict outputs predictions from the ID3 decision tree
func (t *ID3DecisionTree) Predict(what base.FixedDataGrid) (base.FixedDataGrid, error) {
return t.Root.Predict(what)
}
// String returns a human-readable version of this ID3 tree
func (t *ID3DecisionTree) String() string {
return fmt.Sprintf("ID3DecisionTree(%s\n)", t.Root)
}
func (t *ID3DecisionTree) GetMetadata() base.ClassifierMetadataV1 {
return base.ClassifierMetadataV1{
FormatVersion: 1,
ClassifierName: "ID3",
ClassifierVersion: "1.0",
ClassifierMetadata: nil,
}
}
func (t *ID3DecisionTree) Save(filePath string) error {
writer, err := base.CreateSerializedClassifierStub(filePath, t.GetMetadata())
if err != nil {
return err
}
fmt.Printf("writer: %v", writer)
return t.SaveWithPrefix(writer, "")
}
func (t *ID3DecisionTree) SaveWithPrefix(writer *base.ClassifierSerializer, prefix string) error {
return t.Root.SaveWithPrefix(writer, prefix)
}
func (t *ID3DecisionTree) Load(filePath string) error {
reader, err := base.ReadSerializedClassifierStub(filePath)
if err != nil {
return err
}
return t.LoadWithPrefix(reader, "")
}
func (t *ID3DecisionTree) LoadWithPrefix(reader *base.ClassifierDeserializer, prefix string) error {
t.Root = &DecisionTreeNode{}
return t.Root.LoadWithPrefix(reader, "")
}