-
Notifications
You must be signed in to change notification settings - Fork 92
/
featurematrix.go
481 lines (403 loc) · 11.3 KB
/
featurematrix.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
package CloudForest
import (
"archive/zip"
"encoding/csv"
"fmt"
"io"
"log"
"math/rand"
"os"
"strings"
)
//FeatureMatrix contains a slice of Features and a Map to look of the index of a feature
//by its string id.
type FeatureMatrix struct {
Data []Feature
Map map[string]int
CaseLabels []string
}
func (fm *FeatureMatrix) StripStrings(target string) {
for i := range fm.CaseLabels {
fm.CaseLabels[i] = fmt.Sprintf("%v", i)
}
for i, f := range fm.Data {
if f.GetName() == target {
fm.Data[i] = fm.Data[0]
fm.Data[0] = f
break
}
}
fm.Map = make(map[string]int)
for i, f := range fm.Data {
switch f.(type) {
case *DenseNumFeature:
name := fmt.Sprintf("N:%v", i)
fm.Map[name] = i
f.(*DenseNumFeature).Name = name
case *DenseCatFeature:
name := fmt.Sprintf("C:%v", i)
fm.Map[name] = i
f.(*DenseCatFeature).Name = name
f.(*DenseCatFeature).Map = make(map[string]int)
for j, v := range f.(*DenseCatFeature).Back {
v = fmt.Sprintf("%v", j)
f.(*DenseCatFeature).Back[j] = v
f.(*DenseCatFeature).Map[v] = i
}
}
}
return
}
func (fm *FeatureMatrix) EncodeToNum() *FeatureMatrix {
out := &FeatureMatrix{make([]Feature, 0, len(fm.Data)),
make(map[string]int),
fm.CaseLabels}
for _, f := range fm.Data {
switch f.(type) {
case NumFeature:
out.Map[f.GetName()] = len(out.Map)
out.Data = append(out.Data, f)
case CatFeature:
fns := f.(CatFeature).EncodeToNum()
for _, fn := range fns {
out.Map[fn.GetName()] = len(out.Map)
out.Data = append(out.Data, fn)
}
}
}
return out
}
func (fm *FeatureMatrix) OneHot() *FeatureMatrix {
out := &FeatureMatrix{make([]Feature, 0, len(fm.Data)),
make(map[string]int),
fm.CaseLabels}
for _, f := range fm.Data {
switch f.(type) {
case NumFeature:
out.Map[f.GetName()] = len(out.Map)
out.Data = append(out.Data, f)
case CatFeature:
fns := f.(CatFeature).OneHot()
for _, fn := range fns {
out.Map[fn.GetName()] = len(out.Map)
out.Data = append(out.Data, fn)
}
}
}
return out
}
//WriteCases writes a new feature matrix with the specified cases to the the provided writer.
func (fm *FeatureMatrix) WriteCases(w io.Writer, cases []int) (err error) {
vals := make([]string, 0, len(cases)+1)
//print header
vals = append(vals, ".")
for i := 0; i < len(cases); i++ {
vals = append(vals, fm.CaseLabels[cases[i]])
}
_, err = fmt.Fprintln(w, strings.Join(vals, "\t"))
if err != nil {
return
}
for i := 0; i < len(fm.Data); i++ {
vals = vals[0:0]
vals = append(vals, fm.Data[i].GetName())
for j := 0; j < len(cases); j++ {
vals = append(vals, fm.Data[i].GetStr(cases[j]))
}
_, err = fmt.Fprintln(w, strings.Join(vals, "\t"))
if err != nil {
return
}
}
return
}
/*
BestSplitter finds the best splitter from a number of candidate features to slit on by looping over
all features and calling BestSplit.
leafSize specifies the minimum leafSize that can be be produced by the split.
Vet specifies weather feature splits should be penalized with a randomized version of themselves.
allocs contains pointers to reusable structures for use while searching for the best split and should
be initialized to the proper size with NewBestSplitAlocs.
*/
func (fm *FeatureMatrix) BestSplitter(target Target,
cases *[]int,
candidates *[]int,
mTry int,
oob *[]int,
leafSize int,
force bool,
vet bool,
evaloob bool,
extraRandom bool,
allocs *BestSplitAllocs,
nConstantsBefore int) (bestFi int, bestSplit interface{}, impurityDecrease float64, nConstants int) {
impurityDecrease = minImp
var f Feature
var inerImp float64
var vetImp float64
var split interface{}
var constant bool
if vet {
target.(Feature).CopyInTo(allocs.ContrastTarget.(Feature))
}
parentImp := target.Impurity(cases, allocs.Counter)
nConstants = nConstantsBefore
if parentImp <= minImp && parentImp >= 0 {
return
}
var randi, lastSample, swap int
cans := *candidates
lcans := len(cans)
nDrawnConstants := 0
//Move constants to the right but don't touch the ones that are there (for siblings)
//Return the number of constants after moving for children
//Keep searching if
//have looked at >= mTry but haven't found a split with impurity decrease
//haven't looked at at least one non constant feature
//haven't looked at mTry features
for j := 0; (force && j >= mTry && j <= nDrawnConstants) || j < mTry; j++ {
//Break early if there aren't any more nonconstant features
if nConstants >= lcans {
return
}
//make sure there isn't only one non constant left to draw
if lcans > nDrawnConstants+lastSample {
randi = lastSample
randi += allocs.Rnd.Intn(lcans - nDrawnConstants - lastSample)
//randi = lastSample + rand.Intn(nnonconstant-lastSample)
if randi >= lcans-nConstants {
nDrawnConstants++
continue
}
//cans[randi], cans[lastSample] = cans[lastSample], cans[randi]
swap = cans[randi]
cans[randi] = cans[lastSample]
cans[lastSample] = swap
}
i := cans[lastSample]
f = fm.Data[i]
split, inerImp, constant = f.BestSplit(target, cases, parentImp, leafSize, extraRandom, allocs)
if constant {
nConstants++
nDrawnConstants++
cans[lcans-nConstants], cans[lastSample] = cans[lastSample], cans[lcans-nConstants]
} else {
lastSample++
}
if evaloob && inerImp > impurityDecrease {
//spliter := f.DecodeSplit(split)
l, r, m := f.Split(split, *oob) //spliter.Split(fm, *oob)
inerImp = target.Impurity(oob, allocs.Counter) - target.SplitImpurity(&l, &r, &m, allocs)
}
if vet && inerImp > impurityDecrease {
casept := cases
if evaloob {
casept = oob
}
allocs.ContrastTarget.(Feature).ShuffleCases(casept, allocs)
_, vetImp, _ = f.BestSplit(allocs.ContrastTarget, casept, parentImp, leafSize, extraRandom, allocs)
inerImp = inerImp - vetImp
}
if inerImp > impurityDecrease {
bestFi = i
impurityDecrease = inerImp
bestSplit = split
}
}
// if impurityDecrease <= minImp {
// bestFi = -1
// impurityDecrease = 0.0
// bestSplit = nil
// }
return
}
/*
AddContrasts appends n artificial contrast features to a feature matrix. These features
are generated by randomly selecting (with replacement) an existing feature and
creating a shuffled copy named featurename:SHUFFLED.
These features can be used as a contrast to evaluate the importance score's assigned to
actual features.
*/
func (fm *FeatureMatrix) AddContrasts(n int) {
nrealfeatures := len(fm.Data)
for i := 0; i < n; i++ {
//generate a shuffled copy
orig := fm.Data[rand.Intn(nrealfeatures)]
fake := orig.ShuffledCopy()
fm.Map[fake.GetName()] = len(fm.Data)
fm.Data = append(fm.Data, fake)
}
}
/*
ContrastAll adds shuffled copies of every feature to the feature matrix. These features
are generated by randomly selecting (with replacement) an existing feature and
creating a shuffled copy named featurename:SHUFFLED.
These features can be used as a contrast to evaluate the importance score's assigned to
actual features. ContrastAll is particularly useful vs AddContrast when one wishes to
identify [pseudo] unique identifiers that might lead to over fitting.
*/
func (fm *FeatureMatrix) ContrastAll() {
nrealfeatures := len(fm.Data)
for i := 0; i < nrealfeatures; i++ {
fake := fm.Data[i].ShuffledCopy()
fm.Map[fake.GetName()] = len(fm.Data)
fm.Data = append(fm.Data, fake)
}
}
/*
ImputeMissing imputes missing values in all features to the mean or mode of the feature.
*/
func (fm *FeatureMatrix) ImputeMissing() {
for _, f := range fm.Data {
f.ImputeMissing()
}
}
//LoadCases will load data stored case by case from a cvs reader into a
//feature matrix that has allready been filled with the coresponding empty
//features. It is a lower level method generally called after inital setup to parse
//a fm, arff, csv etc.
func (fm *FeatureMatrix) LoadCases(data *csv.Reader, rowlabels bool) {
count := 0
for {
record, err := data.Read()
if err == io.EOF {
break
} else if err != nil {
log.Print("Error:", err)
break
}
caselabel := fmt.Sprintf("%v", count)
if rowlabels {
caselabel = record[0]
record = record[1:]
}
fm.CaseLabels = append(fm.CaseLabels, caselabel)
for i, v := range record {
fm.Data[i].Append(v)
}
count++
}
}
//Parse an AFM (annotated feature matrix) out of an io.Reader
//AFM format is a tsv with row and column headers where the row headers start with
//N: indicating numerical, C: indicating categorical or B: indicating boolean
//For this parser features without N: are assumed to be categorical
func ParseAFM(input io.Reader) *FeatureMatrix {
data := make([]Feature, 0, 100)
lookup := make(map[string]int, 0)
tsv := csv.NewReader(input)
tsv.Comma = '\t'
headers, err := tsv.Read()
if err == io.EOF {
return &FeatureMatrix{data, lookup, headers[1:]}
} else if err != nil {
log.Print("Error:", err)
return &FeatureMatrix{data, lookup, headers[1:]}
}
headers = headers[1:]
if len(headers[0]) > 1 {
sniff := headers[0][:2]
if sniff == "N:" || sniff == "C:" || sniff == "B:" {
//features in cols
for i, label := range headers {
if label[:2] == "N:" {
data = append(data, &DenseNumFeature{
make([]float64, 0, 0),
make([]bool, 0, 0),
label,
false})
} else {
data = append(data, &DenseCatFeature{
&CatMap{make(map[string]int, 0),
make([]string, 0, 0)},
make([]int, 0, 0),
make([]bool, 0, 0),
label,
false,
false})
}
lookup[label] = i
}
fm := &FeatureMatrix{data, lookup, make([]string, 0, 0)}
fm.LoadCases(tsv, true)
return fm
}
}
//features in rows
count := 0
for {
record, err := tsv.Read()
if err == io.EOF {
break
} else if err != nil {
log.Print("Error:", err)
break
}
data = append(data, ParseFeature(record))
lookup[record[0]] = count
count++
}
return &FeatureMatrix{data, lookup, headers}
}
//LoadAFM loads a, possible zipped, FeatureMatrix specified by filename
func LoadAFM(filename string) (fm *FeatureMatrix, err error) {
r, err := zip.OpenReader(filename)
if err == nil {
rc, err := r.File[0].Open()
if err == nil {
fm = ParseAFM(rc)
rc.Close()
return fm, err
}
}
datafile, err := os.Open(filename)
if err != nil {
return
}
switch {
case strings.HasSuffix(filename, ".arff"):
fm = ParseARFF(datafile)
case strings.HasSuffix(filename, ".libsvm"):
fm = ParseLibSVM(datafile)
default:
fm = ParseAFM(datafile)
}
datafile.Close()
return
}
//ParseFeature parses a Feature from an array of strings and a capacity
//capacity is the number of cases and will usually be len(record)-1 but
//but doesn't need to be calculated for every row of a large file.
//The type of the feature us inferred from the start of the first (header) field
//in record:
//"N:"" indicating numerical, anything else (usually "C:" and "B:") for categorical
func ParseFeature(record []string) Feature {
capacity := len(record)
switch record[0][0:2] {
case "N:":
f := &DenseNumFeature{
nil,
make([]bool, 0, capacity),
record[0],
false}
f.NumData = make([]float64, 0, capacity)
for i := 1; i < len(record); i++ {
f.Append(record[i])
}
return f
default:
f := &DenseCatFeature{
&CatMap{make(map[string]int, 0),
make([]string, 0, 0)},
nil,
make([]bool, 0, capacity),
record[0],
false,
false}
f.CatData = make([]int, 0, capacity)
for i := 1; i < len(record); i++ {
f.Append(record[i])
}
return f
}
}