forked from sjwhitworth/golearn
/
instances.go
561 lines (501 loc) · 15.8 KB
/
instances.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
package base
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math/rand"
"github.com/gonum/matrix/mat64"
)
// SortDirection specifies sorting direction...
type SortDirection int
const (
// Descending says that Instances should be sorted high to low...
Descending SortDirection = 1
// Ascending states that Instances should be sorted low to high...
Ascending SortDirection = 2
)
const highBit int64 = -1 << 63
// Instances represents a grid of numbers (typed by Attributes)
// stored internally in mat.DenseMatrix as float64's.
// See docs/instances.md for more information.
type Instances struct {
storage *mat64.Dense
attributes []Attribute
Rows int
Cols int
ClassIndex int
}
func xorFloatOp(item float64) float64 {
var ret float64
var tmp int64
buf := bytes.NewBuffer(nil)
binary.Write(buf, binary.LittleEndian, item)
binary.Read(buf, binary.LittleEndian, &tmp)
tmp ^= -1 << 63
binary.Write(buf, binary.LittleEndian, tmp)
binary.Read(buf, binary.LittleEndian, &ret)
return ret
}
func printFloatByteArr(arr [][]byte) {
buf := bytes.NewBuffer(nil)
var f float64
for _, b := range arr {
buf.Write(b)
binary.Read(buf, binary.LittleEndian, &f)
f = xorFloatOp(f)
fmt.Println(f)
}
}
// Sort does an in-place radix sort of Instances, using SortDirection
// direction (Ascending or Descending) with attrs as a slice of Attribute
// indices that you want to sort by.
//
// IMPORTANT: Radix sort is not stable, so ordering outside
// the attributes used for sorting is arbitrary.
func (inst *Instances) Sort(direction SortDirection, attrs []int) {
// Create a buffer
buf := bytes.NewBuffer(nil)
ds := make([][]byte, inst.Rows)
rs := make([]int, inst.Rows)
for i := 0; i < inst.Rows; i++ {
byteBuf := make([]byte, 8*len(attrs))
for _, a := range attrs {
x := inst.storage.At(i, a)
binary.Write(buf, binary.LittleEndian, xorFloatOp(x))
}
buf.Read(byteBuf)
ds[i] = byteBuf
rs[i] = i
}
// Sort viua
valueBins := make([][][]byte, 256)
rowBins := make([][]int, 256)
for i := 0; i < 8*len(attrs); i++ {
for j := 0; j < len(ds); j++ {
// Address each row value by it's ith byte
b := ds[j]
valueBins[b[i]] = append(valueBins[b[i]], b)
rowBins[b[i]] = append(rowBins[b[i]], rs[j])
}
j := 0
for k := 0; k < 256; k++ {
bs := valueBins[k]
rc := rowBins[k]
copy(ds[j:], bs)
copy(rs[j:], rc)
j += len(bs)
valueBins[k] = bs[:0]
rowBins[k] = rc[:0]
}
}
for _, b := range ds {
var v float64
buf.Write(b)
binary.Read(buf, binary.LittleEndian, &v)
}
done := make([]bool, inst.Rows)
for index := range rs {
if done[index] {
continue
}
j := index
for {
done[j] = true
if rs[j] != index {
inst.swapRows(j, rs[j])
j = rs[j]
} else {
break
}
}
}
if direction == Descending {
// Reverse the matrix
for i, j := 0, inst.Rows-1; i < j; i, j = i+1, j-1 {
inst.swapRows(i, j)
}
}
}
// NewInstances returns a preallocated Instances structure
// with some helful values pre-filled.
func NewInstances(attrs []Attribute, rows int) *Instances {
rawStorage := make([]float64, rows*len(attrs))
return NewInstancesFromRaw(attrs, rows, rawStorage)
}
// CheckNewInstancesFromRaw checks whether a call to NewInstancesFromRaw
// is likely to produce an error-free result.
func CheckNewInstancesFromRaw(attrs []Attribute, rows int, data []float64) error {
size := rows * len(attrs)
if size < len(data) {
return errors.New("base: data length is larger than the rows * attribute space")
} else if size > len(data) {
return errors.New("base: data is smaller than the rows * attribute space")
}
return nil
}
// NewInstancesFromRaw wraps a slice of float64 numbers in a
// mat64.Dense structure, reshaping it with the given number of rows
// and representing it with the given attrs (Attribute slice)
//
// IMPORTANT: if the |attrs| * |rows| value doesn't equal len(data)
// then panic()s may occur. Use CheckNewInstancesFromRaw to confirm.
func NewInstancesFromRaw(attrs []Attribute, rows int, data []float64) *Instances {
rawStorage := mat64.NewDense(rows, len(attrs), data)
return NewInstancesFromDense(attrs, rows, rawStorage)
}
// NewInstancesFromDense creates a set of Instances from a mat64.Dense
// matrix
func NewInstancesFromDense(attrs []Attribute, rows int, mat *mat64.Dense) *Instances {
return &Instances{mat, attrs, rows, len(attrs), len(attrs) - 1}
}
// InstancesTrainTestSplit takes a given Instances (src) and a train-test fraction
// (prop) and returns an array of two new Instances, one containing approximately
// that fraction and the other containing what's left.
//
// IMPORTANT: this function is only meaningful when prop is between 0.0 and 1.0.
// Using any other values may result in odd behaviour.
func InstancesTrainTestSplit(src *Instances, prop float64) (*Instances, *Instances) {
trainingRows := make([]int, 0)
testingRows := make([]int, 0)
numAttrs := len(src.attributes)
src.Shuffle()
for i := 0; i < src.Rows; i++ {
trainOrTest := rand.Intn(101)
if trainOrTest > int(100*prop) {
trainingRows = append(trainingRows, i)
} else {
testingRows = append(testingRows, i)
}
}
rawTrainMatrix := mat64.NewDense(len(trainingRows), numAttrs, make([]float64, len(trainingRows)*numAttrs))
rawTestMatrix := mat64.NewDense(len(testingRows), numAttrs, make([]float64, len(testingRows)*numAttrs))
for i, row := range trainingRows {
rowDat := src.storage.RowView(row)
rawTrainMatrix.SetRow(i, rowDat)
}
for i, row := range testingRows {
rowDat := src.storage.RowView(row)
rawTestMatrix.SetRow(i, rowDat)
}
trainingRet := NewInstancesFromDense(src.attributes, len(trainingRows), rawTrainMatrix)
testRet := NewInstancesFromDense(src.attributes, len(testingRows), rawTestMatrix)
return trainingRet, testRet
}
// CountAttrValues returns the distribution of values of a given
// Attribute.
// IMPORTANT: calls panic() if the attribute index of a cannot be
// determined. Call GetAttrIndex(a) and check for a -1 return value.
func (inst *Instances) CountAttrValues(a Attribute) map[string]int {
ret := make(map[string]int)
attrIndex := inst.GetAttrIndex(a)
if attrIndex == -1 {
panic("Invalid attribute")
}
for i := 0; i < inst.Rows; i++ {
sysVal := inst.Get(i, attrIndex)
stringVal := a.GetStringFromSysVal(sysVal)
ret[stringVal]++
}
return ret
}
// CountClassValues returns the class distribution of this
// Instances set
func (inst *Instances) CountClassValues() map[string]int {
a := inst.GetAttr(inst.ClassIndex)
return inst.CountAttrValues(a)
}
// DecomposeOnAttributeValues divides the instance set depending on the
// value of a given Attribute, constructs child instances, and returns
// them in a map keyed on the string value of that Attribute.
// IMPORTANT: calls panic() if the attribute index of at cannot be determined.
// Use GetAttrIndex(at) and check for a non-zero return value.
func (inst *Instances) DecomposeOnAttributeValues(at Attribute) map[string]*Instances {
// Find the attribute we're decomposing on
attrIndex := inst.GetAttrIndex(at)
if attrIndex == -1 {
panic("Invalid attribute index")
}
// Construct the new attribute set
newAttrs := make([]Attribute, 0)
for i := range inst.attributes {
a := inst.attributes[i]
if a.Equals(at) {
continue
}
newAttrs = append(newAttrs, a)
}
// Create the return map, several counting maps
ret := make(map[string]*Instances)
counts := inst.CountAttrValues(at) // So we know what to allocate
rows := make(map[string]int)
for k := range counts {
tmp := NewInstances(newAttrs, counts[k])
ret[k] = tmp
}
for i := 0; i < inst.Rows; i++ {
newAttrCounter := 0
classVar := at.GetStringFromSysVal(inst.Get(i, attrIndex))
dest := ret[classVar]
destRow := rows[classVar]
for j := 0; j < inst.Cols; j++ {
a := inst.attributes[j]
if a.Equals(at) {
continue
}
dest.Set(destRow, newAttrCounter, inst.Get(i, j))
newAttrCounter++
}
rows[classVar]++
}
return ret
}
func (inst *Instances) GetClassDistributionAfterSplit(at Attribute) map[string]map[string]int {
ret := make(map[string]map[string]int)
// Find the attribute we're decomposing on
attrIndex := inst.GetAttrIndex(at)
if attrIndex == -1 {
panic("Invalid attribute index")
}
// Get the class index
classAttr := inst.GetAttr(inst.ClassIndex)
for i := 0; i < inst.Rows; i++ {
splitVar := at.GetStringFromSysVal(inst.Get(i, attrIndex))
classVar := classAttr.GetStringFromSysVal(inst.Get(i, inst.ClassIndex))
if _, ok := ret[splitVar]; !ok {
ret[splitVar] = make(map[string]int)
i--
continue
}
ret[splitVar][classVar]++
}
return ret
}
// Get returns the system representation (float64) of the value
// stored at the given row and col coordinate.
func (inst *Instances) Get(row int, col int) float64 {
return inst.storage.At(row, col)
}
// Set sets the system representation (float64) to val at the
// given row and column coordinate.
func (inst *Instances) Set(row int, col int, val float64) {
inst.storage.Set(row, col, val)
}
// GetRowVector returns a row of system representation
// values at the given row index.
func (inst *Instances) GetRowVector(row int) []float64 {
return inst.storage.RowView(row)
}
// GetRowVectorWithoutClass returns a row of system representation
// values at the given row index, excluding the class attribute
func (inst *Instances) GetRowVectorWithoutClass(row int) []float64 {
rawRow := make([]float64, inst.Cols)
copy(rawRow, inst.GetRowVector(row))
return append(rawRow[0:inst.ClassIndex], rawRow[inst.ClassIndex+1:inst.Cols]...)
}
// GetClass returns the string representation of the given
// row's class, as determined by the Attribute at the ClassIndex
// position from GetAttr
func (inst *Instances) GetClass(row int) string {
attr := inst.GetAttr(inst.ClassIndex)
val := inst.Get(row, inst.ClassIndex)
return attr.GetStringFromSysVal(val)
}
// GetClassDistribution returns a map containing the count of each
// class type (indexed by the class' string representation)
func (inst *Instances) GetClassDistribution() map[string]int {
ret := make(map[string]int)
attr := inst.GetAttr(inst.ClassIndex)
for i := 0; i < inst.Rows; i++ {
val := inst.Get(i, inst.ClassIndex)
cls := attr.GetStringFromSysVal(val)
ret[cls]++
}
return ret
}
func (inst *Instances) GetClassAttrPtr() *Attribute {
attr := inst.GetAttr(inst.ClassIndex)
return &attr
}
func (inst *Instances) GetClassAttr() Attribute {
return inst.GetAttr(inst.ClassIndex)
}
//
// Attribute functions
//
// GetAttributeCount returns the number of attributes represented.
func (inst *Instances) GetAttributeCount() int {
// Return the number of attributes attached to this Instance set
return len(inst.attributes)
}
// SetAttrStr sets the system-representation value of row in column attr
// to value val, implicitly converting the string to system-representation
// via the appropriate Attribute function.
func (inst *Instances) SetAttrStr(row int, attr int, val string) {
// Set an attribute on a particular row from a string value
a := inst.attributes[attr]
sysVal := a.GetSysValFromString(val)
inst.storage.Set(row, attr, sysVal)
}
// GetAttrStr returns a human-readable string value stored in column `attr'
// and row `row', as determined by the appropriate Attribute function.
func (inst *Instances) GetAttrStr(row int, attr int) string {
// Get a human-readable value from a particular row
a := inst.attributes[attr]
usrVal := a.GetStringFromSysVal(inst.Get(row, attr))
return usrVal
}
// GetAttr returns information about an attribute at given index
// in the attributes slice.
func (inst *Instances) GetAttr(attrIndex int) Attribute {
// Return a copy of an attribute attached to this Instance set
return inst.attributes[attrIndex]
}
// GetAttrIndex returns the offset of a given Attribute `a' to an
// index in the attributes slice
func (inst *Instances) GetAttrIndex(of Attribute) int {
// Finds the offset of an Attribute in this instance set
// Returns -1 if no Attribute matches
for i, a := range inst.attributes {
if a.Equals(of) {
return i
}
}
return -1
}
// ReplaceAttr overwrites the attribute at `index' with `a'
func (inst *Instances) ReplaceAttr(index int, a Attribute) {
// Replace an Attribute at index with another
// DOESN'T CONVERT ANY EXISTING VALUES
inst.attributes[index] = a
}
//
// Printing functions
//
// RowStr returns a human-readable representation of a given row.
func (inst *Instances) RowStr(row int) string {
// Prints a given row
var buffer bytes.Buffer
for j := 0; j < inst.Cols; j++ {
val := inst.storage.At(row, j)
a := inst.attributes[j]
postfix := " "
if j == inst.Cols-1 {
postfix = ""
}
buffer.WriteString(fmt.Sprintf("%s%s", a.GetStringFromSysVal(val), postfix))
}
return buffer.String()
}
func (inst *Instances) String() string {
var buffer bytes.Buffer
buffer.WriteString("Instances with ")
buffer.WriteString(fmt.Sprintf("%d row(s) ", inst.Rows))
buffer.WriteString(fmt.Sprintf("%d attribute(s)\n", inst.Cols))
buffer.WriteString(fmt.Sprintf("Attributes: \n"))
for i, a := range inst.attributes {
prefix := "\t"
if i == inst.ClassIndex {
prefix = "*\t"
}
buffer.WriteString(fmt.Sprintf("%s%s\n", prefix, a))
}
buffer.WriteString("\nData:\n")
maxRows := 30
if inst.Rows < maxRows {
maxRows = inst.Rows
}
for i := 0; i < maxRows; i++ {
buffer.WriteString("\t")
for j := 0; j < inst.Cols; j++ {
val := inst.storage.At(i, j)
a := inst.attributes[j]
buffer.WriteString(fmt.Sprintf("%s ", a.GetStringFromSysVal(val)))
}
buffer.WriteString("\n")
}
missingRows := inst.Rows - maxRows
if missingRows != 0 {
buffer.WriteString(fmt.Sprintf("\t...\n%d row(s) undisplayed", missingRows))
} else {
buffer.WriteString("All rows displayed")
}
return buffer.String()
}
// SelectAttributes returns a new instance set containing
// the values from this one with only the Attributes specified
func (inst *Instances) SelectAttributes(attrs []Attribute) *Instances {
ret := NewInstances(attrs, inst.Rows)
attrIndices := make([]int, 0)
for _, a := range attrs {
attrIndex := inst.GetAttrIndex(a)
attrIndices = append(attrIndices, attrIndex)
}
for i := 0; i < inst.Rows; i++ {
for j, a := range attrIndices {
ret.Set(i, j, inst.Get(i, a))
}
}
return ret
}
// GeneratePredictionVector generates a new set of Instances
// with the same number of rows, but only this Instance set's
// class Attribute.
func (inst *Instances) GeneratePredictionVector() *Instances {
attrs := make([]Attribute, 1)
attrs[0] = inst.GetClassAttr()
ret := NewInstances(attrs, inst.Rows)
return ret
}
// Shuffle randomizes the row order in place
func (inst *Instances) Shuffle() {
for i := 0; i < inst.Rows; i++ {
j := rand.Intn(i + 1)
inst.swapRows(i, j)
}
}
// SampleWithReplacement returns a new set of Instances of size `size'
// containing random rows from this set of Instances.
//
// IMPORTANT: There's a high chance of seeing duplicate rows
// whenever size is close to the row count.
func (inst *Instances) SampleWithReplacement(size int) *Instances {
ret := NewInstances(inst.attributes, size)
for i := 0; i < size; i++ {
srcRow := rand.Intn(inst.Rows)
for j := 0; j < inst.Cols; j++ {
ret.Set(i, j, inst.Get(srcRow, j))
}
}
return ret
}
// Equal checks whether a given Instance set is exactly the same
// as another: same size and same values (as determined by the Attributes)
//
// IMPORTANT: does not explicitly check if the Attributes are considered equal.
func (inst *Instances) Equal(other *Instances) bool {
if inst.Rows != other.Rows {
return false
}
if inst.Cols != other.Cols {
return false
}
for i := 0; i < inst.Rows; i++ {
for j := 0; j < inst.Cols; j++ {
if inst.GetAttrStr(i, j) != other.GetAttrStr(i, j) {
return false
}
}
}
return true
}
func (inst *Instances) swapRows(r1 int, r2 int) {
row1buf := make([]float64, inst.Cols)
row2buf := make([]float64, inst.Cols)
row1 := inst.storage.RowView(r1)
row2 := inst.storage.RowView(r2)
copy(row1buf, row1)
copy(row2buf, row2)
inst.storage.SetRow(r1, row2buf)
inst.storage.SetRow(r2, row1buf)
}