-
Notifications
You must be signed in to change notification settings - Fork 2
/
VI-permutation.py
720 lines (445 loc) · 20.7 KB
/
VI-permutation.py
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# coding: utf-8
# In[1]:
from __future__ import division
from datetime import datetime
import numpy as np
import pandas as pd
import os
import pickle
import sys
from collections import OrderedDict
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn import preprocessing
from sklearn.preprocessing import RobustScaler
from sklearn.model_selection import KFold
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn import svm
from sklearn.tree import DecisionTreeRegressor
from rf_perm_feat_import import PermutationImportance
#Constant Variables
kGroup = sys.argv[1]
kInstrument = sys.argv[1]
kSampleRate = 44100
kN = 100
kType = 'mono'
kFeatures = 1812 #Find a way to not hard-code this.
path = './Music/Data/MedleyDB/Features/%s/%s/' % (sys.argv[2], kType)
#Loads all global variables.
def dumpPickle(d, name):
#path = './Music/Data/MedleyDB/Features/Arrays_original_1812/%s/' % (kType)
with open(path + name, 'wb') as output:
# Pickle dictionary using protocol 0.
pickle.dump(d, output)
print '%s Saved' % (name)
def loadPickle(name):
#path = './Music/Data/MedleyDB/Features/Arrays_original_1812/%s/' % (kType)
# load data from pkl file
with open(path + name, "rb") as fp:
loaded_data1 = pickle.load(fp)
print '%s loaded, %s ' % (name, type(loaded_data1))
return loaded_data1
#%%
#Only if you initialized Variables.
X = loadPickle('X.pkl')
y = loadPickle('y.pkl')
XRaw = loadPickle('XRaw.pkl')
yRaw = loadPickle('yRaw.pkl')
XStem = loadPickle('XStem.pkl')
yStem = loadPickle('yStem.pkl')
gListTracks = loadPickle('gListTracks.pkl')
gNameFeatures = loadPickle('gNameFeatures.pkl')
gImportances = loadPickle('gImportances.pkl')
gPreSelectedImportances = loadPickle('gPreSelectedImportances.pkl')
gInterpretationOOBError = loadPickle('gInterpretationOOBError.pkl')
gInterpretationIdx = loadPickle('gInterpretationIdx.pkl')
gInterpretationFeatures = loadPickle('gInterpretationFeatures.pkl')
gPredictionOOBError = loadPickle('gPredictionOOBError.pkl')
gPredictionIdx = loadPickle('gPredictionIdx.pkl')
gPredictionOOBErrorGain = loadPickle('gPredictionOOBErrorGain.pkl')
gPreSelectedFeatures = loadPickle('gPreSelectedFeatures.pkl')
gPredictionFeatures = loadPickle('gPredictionFeatures.pkl')
# Definition of functions.
# Loads numpy array from Instrument name and path. It adds the to the global variables.
def loadsNumpyArray(name, _path = path):
startTime = datetime.now()
if not os.path.exists(_path):
print 'WRONG PATH'
XRaw[name] = np.load(_path + name + '_XRaw.npy')
yRaw[name] = np.load(_path + name + '_yRaw.npy')
XStem[name] = np.load(_path + name + '_XStem.npy')
yStem[name] = np.load(_path + name + '_yStem.npy')
X[name] = np.concatenate((XRaw[name], XStem[name]), axis = 0)
y[name] = np.concatenate((yRaw[name], yStem[name]), axis = 0)
gNameFeatures[name] = np.load(_path + name + '_gNameFeatures.npy').tolist()
gListTracks[name] = np.load(_path + name + '_gListTracks.npy').tolist()
#gReducedFeatures[name] = gNameFeatures[name][:]
print '\nExecuted in: %s. \n %d stems and raw %s tracks with %d features each were loaded.' % (str(datetime.now() - startTime), len(X[name]), name, len(gNameFeatures[name]))
# Create arrays for an specific group instruments. It adds them to the global variables.
def createGroupInstruments(name, list_instruments):
_X = X[list_instruments[0]][:]
_Xr = XRaw[list_instruments[0]][:]
_Xs = XStem[list_instruments[0]][:]
_y = y[list_instruments[0]][:]
_yr = yRaw[list_instruments[0]][:]
_ys = yStem[list_instruments[0]][:]
_gListTracks = gListTracks[list_instruments[0]][:]
gNameFeatures[name] = gNameFeatures[list_instruments[0]][:]
#gReducedFeatures[name] = gReducedFeatures[list_instruments[0]][:]
del list_instruments[0]
for inst in list_instruments:
_X = np.concatenate((_X, X[inst]), axis = 0)
_y = np.concatenate((_y, y[inst]), axis = 0)
_gListTracks = _gListTracks + gListTracks[inst]
X[name] = _X
y[name] = _y
XRaw[name] = _Xr
yRaw[name] = _yr
XStem[name] = _Xs
yStem[name] = _ys
gListTracks[name] = _gListTracks
#RAMDON FOREST FUNCTIONS:
# Check OOB ERROR against number of trees.
def getNumberTrees(_X, _y, min_estimators = 10, max_estimators = 1000, epoch = 10):
startTime = datetime.now()
forest = RandomForestClassifier(n_estimators=10, warm_start=True, bootstrap = True,
oob_score=True,
max_features="sqrt", random_state=None, n_jobs = -1)
oob_error_ntrees = []
for i in range(min_estimators, max_estimators + 1):
for j in range(epoch):
oob = []
forest.set_params(n_estimators=i)
forest.fit(_X, _y)
# Record the OOB error for each `n_estimators=i` setting.
_oob_error = 1 - forest.oob_score_
oob.append(_oob_error)
oob_error_ntrees.append((i, np.mean(oob)))
print '\nExecuted in: %s. \n' % (str(datetime.now() - startTime))
# Generate the "OOB error rate" vs. "n_estimators" plot.
#plotOOBError(oob_error_ntrees, xlabel = 'n_estimators - %s' % kGroup)
n = np.argmin((np.asarray(oob_error_ntrees))[:,1]) + min_estimators
return oob_error_ntrees, n
def getNameFeaturesIdx(idx, nameFeatures):#, NameFeatures = gNameFeatures):
a = []
for i in idx:
_a = (nameFeatures[i])
print _a
a.append(_a)
return a
# Random Forest Classifier that does N epochs, and calculates mean OOB error and mean Importances indices
def RFmeanOOBerror(data, label, nameFeatures, epoch = 100, nEstimators = 100,
nFeatures = 10):
# iterates epoch time to have mean OOB and mean relevance of features.
startTime = datetime.now()
_importances = []
_error = []
forest = RandomForestClassifier(n_estimators=nEstimators, warm_start=True, bootstrap = True,
oob_score=True,
max_features='sqrt', random_state=None, n_jobs = -1)
for i in range(epoch):
forest = forest.fit(data, label)
_importances.append(forest.feature_importances_)
_error.append(1 - forest.oob_score_)
_error_mean = np.mean(_error, axis = 0)
_importances = np.mean(_importances, axis = 0)
_indices = np.argsort(_importances)[::-1]
print '\n Indices : \n'
print _indices
print '\n %d Relevant Features: \n ' % nFeatures
getNameFeaturesIdx(_indices[0:nFeatures], nameFeatures)
print '\n OOB error: %f \n' % (_error_mean)
print '\nExecuted in: %s. \n ' % (str(datetime.now() - startTime))
# Gives approximate OOB error usign different types of scalling
def RFmeanScallingError(data, label, nEstimators = 100, epoch = 10):
A = data[:]
X_rs = robust_scaler.fit_transform(A)
X_n = preprocessing.normalize(A)
X_mm = min_max_scaler.fit_transform(A)
X_ma = min_abs_scaler.fit_transform(A)
X_mars = min_abs_scaler.fit_transform(X_rs)
_error = []
for n,i in enumerate([A,X_rs, X_n, X_mm, X_ma, X_mars]):
forest = RandomForestClassifier(n_estimators=nEstimators, warm_start=False, bootstrap = True,
oob_score=True, max_features="sqrt",
random_state=None, n_jobs = -1)
for j in range(epoch):
forest.fit(i, label)
# Record the OOB error for each `n_estimators=i` setting.
_error.append(1 - forest.oob_score_)
print n, np.mean(_error)
print '\n 0 - data\n 1 - robust scaler\n 2 - normalize\n 3 - min-max scaler\n 4 - min-abs scaler\n 5 - min-abs-robust scaler'
# Returns the array containing selected features (from deleted and reduced),
# number = 0 means it will return from the minimum oob_error. Returns pd Dataframe as well.
def getArraysOfRelevantFeatures(error,
deleted,
reduced,
number = 0, type = 1):
_X = X[kGroup][:]
_y = y[kGroup]
__X = []
if type == 2:
error = gOOBError[kGroup+'2']
deleted = gDeletedFeatures[kGroup+'2']
reduced = gReducedFeatures[kGroup+'2']
elif type == 3:
error = gOOBError[kGroup+'3']
deleted = gDeletedFeatures[kGroup+'3']
reduced = gReducedFeatures[kGroup+'3']
elif type == 4:
error = gOOBError[kGroup+'4']
deleted = gDeletedFeatures[kGroup+'4']
reduced = gReducedFeatures[kGroup+'4']
if number == 0:
idx = np.argmin(np.asarray(error)[:,1])
else:
idx = -1*(number - 1)
features = deleted[idx::]
features.append(reduced[0])
features = list(reversed(features))
idx = []
for name in features:
idx.append(gNameFeatures[kGroup].index(name))
for i in idx:
__X.append(_X[:,i])
__X = np.asarray(__X).T
df = pd.DataFrame(__X, columns = features)
df = df.assign(stem = _y)
return __X, features, df
#Saves in global dict variables the results from the 3 iterations.
def saveDictsinGVariables(data):
X = loadPickle('X.pkl')
y = loadPickle('y.pkl')
X[kGroup.upper()] = data[:]
y[kGroup.upper()] = y[kGroup]
dumpPickle(X, 'X.pkl')
dumpPickle(y, 'y.pkl')
GImportances = loadPickle('gImportances.pkl')
GPreSelectedImportances = loadPickle('gPreSelectedImportances.pkl')
GInterpretationOOBError = loadPickle('gInterpretationOOBError.pkl')
GInterpretationIdx = loadPickle('gInterpretationIdx.pkl')
GInterpretationFeatures = loadPickle('gInterpretationFeatures.pkl')
GPredictionOOBError = loadPickle('gPredictionOOBError.pkl')
GPredictionIdx = loadPickle('gPredictionIdx.pkl')
GPredictionOOBErrorGain = loadPickle('gPredictionOOBErrorGain.pkl')
GPreSelectedFeatures = loadPickle('gPreSelectedFeatures.pkl')
GPredictionFeatures = loadPickle('gPredictionFeatures.pkl')
GImportances[kGroup] = gImportances[kGroup]
GPreSelectedImportances[kGroup] = gPreSelectedImportances[kGroup]
GImportances[kGroup.upper()] = gImportances[kGroup.upper()]
GInterpretationOOBError[kGroup] = gInterpretationOOBError[kGroup]
GInterpretationIdx[kGroup] = gInterpretationIdx[kGroup]
GInterpretationFeatures[kGroup] = gInterpretationFeatures[kGroup]
GPredictionOOBError[kGroup] = gPredictionOOBError[kGroup]
GPredictionIdx[kGroup] = gPredictionIdx[kGroup]
GPredictionOOBErrorGain[kGroup] = gPredictionOOBErrorGain[kGroup]
GPreSelectedFeatures[kGroup] = gPreSelectedFeatures[kGroup]
GPredictionFeatures[kGroup] = gPredictionFeatures[kGroup]
dumpPickle(GPreSelectedFeatures, 'gPreSelectedFeatures.pkl')
dumpPickle(GPredictionFeatures, 'gPredictionFeatures.pkl')
dumpPickle(GImportances, 'gImportances.pkl')
dumpPickle(GPreSelectedImportances, 'gPreSelectedImportances.pkl')
dumpPickle(GInterpretationOOBError, 'gInterpretationOOBError.pkl')
dumpPickle(GInterpretationIdx, 'gInterpretationIdx.pkl')
dumpPickle(GInterpretationFeatures, 'gInterpretationFeatures.pkl')
dumpPickle(GPredictionOOBError, 'gPredictionOOBError.pkl')
dumpPickle(GPredictionIdx, 'gPredictionIdx.pkl')
dumpPickle(GPredictionOOBErrorGain, 'gPredictionOOBErrorGain.pkl')
# Handy functions:
robust_scaler = RobustScaler()
min_max_scaler = preprocessing.MinMaxScaler()
min_abs_scaler = preprocessing.MaxAbsScaler()
_A = X[kGroup][:]
#_A = robust_scaler.fit_transform(_A)
label = y[kGroup]
startTime2 = datetime.now()
startTime = datetime.now()
nEstimators = 2000
_iterations = 20
epoch = 1
_type = 'Boot'
_indices = []
oob_error = []
importances = []
for i in range(_iterations):
_epoch = epoch
_importances = []
_error = []
if _type == 'No boot':
forest = RandomForestClassifier(n_estimators=nEstimators,
warm_start=False,
bootstrap = False,
oob_score=False,
max_features=0.3,
random_state=None,
n_jobs = -1)
oobC = PermutationImportance()
# forest2 = RandomForestClassifier(n_estimators=nEstimators, warm_start=False, bootstrap = True,
# oob_score=True, max_features=0.3, random_state=None, n_jobs = -1)
for j in range(_epoch):
forest = forest.fit(_A, label)
feature_importances = oobC.featureImportances(forest,
_A,
label,
10)
print "Weighted Avg Information Gain feature importances:"
print forest.feature_importances_
print "Permutation importances:"
print feature_importances
importances.append(feature_importances)
elif _type == 'Boot':
forest = RandomForestClassifier(n_estimators=nEstimators,
warm_start=False,
bootstrap = True,
oob_score=True,
max_features=0.3,
random_state=None,
n_jobs = -1)
oobC = PermutationImportance()
# forest2 = RandomForestClassifier(n_estimators=nEstimators, warm_start=False, bootstrap = True,
# oob_score=True, max_features=0.3, random_state=None, n_jobs = -1)
for j in range(_epoch):
forest = forest.fit(_A, label)
print "Weighted Avg Information Gain feature importances:"
print forest.feature_importances_
print "Permutation importances:"
feature_importances = oobC.featureImportances(forest,
_A,
label,
10)
print feature_importances
importances.append(feature_importances)
print '\n iteration # %d\n' % i
print '\nExecuted in: %s. \n ' % (str(datetime.now() - startTime))
meanImportances = np.mean(importances, axis = 0)
stdImportances = np.std(importances, axis = 0)
kN = 500
gImportances[kGroup] = np.vstack((meanImportances, stdImportances))
indices = np.argsort(meanImportances)[::-1]
gImportances[kGroup.upper()] = importances
gImportances[kGroup] = gImportances[kGroup][:,indices]
#plt.plot(gImportances[kGroup][0][0:kN], 'k^', gImportances[kGroup][0][0:kN], 'k:',label=kGroup+'_mean' )
#plt.plot(gImportances[kGroup][1][0:kN], 'r^', gImportances[kGroup][1][0:kN], 'r:',label=kGroup+'_std' )
kN = 500
# Create a random dataset
X_stdTrain = np.arange(0,kN,1).reshape(-1,1)
y_stdTrain = gImportances[kGroup][1][0:kN][:].reshape(-1,1)
# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=3)
regr_2 = DecisionTreeRegressor(max_depth=3)
regr_3 = DecisionTreeRegressor()
regr_1.fit(X_stdTrain, y_stdTrain)
regr_2.fit(X_stdTrain, y_stdTrain)
regr_3.fit(X_stdTrain, y_stdTrain)
# # Predict
X_test = np.arange(0.0, kN, 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)
y_3 = regr_3.predict(X_test)
threshold = np.min(y_1[y_1>0.0001])
print '\n TH - STD - %f' % threshold
gPreSelectedImportances[kGroup] = gImportances[kGroup][0][gImportances[kGroup][0]>threshold]
#plt.plot(gPreSelectedImportances[kGroup], 'k^', gPreSelectedImportances[kGroup], 'k:',label=kGroup+'_pre_selected')
#plt.plot(X_test,y_1,'c')
idx = indices[0:len(gPreSelectedImportances[kGroup])]
gPreSelectedFeatures[kGroup] = getNameFeaturesIdx(idx, gNameFeatures[kGroup])
XPreSelected = []
for i in idx:
XPreSelected.append(_A[:,i])
XPreSelected = np.asarray(XPreSelected).T
startTime = datetime.now()
oob_error = []
nEstimators = 2000
for i in range(len(idx)):
_epoch = 20
_error = []
_A = XPreSelected[:,:i+1]
print _A.shape
forest = RandomForestClassifier(n_estimators=nEstimators,
warm_start=False,
bootstrap = True,
oob_score=True,
max_features=0.3,
random_state=None,
n_jobs = -1)
for j in range(_epoch):
forest = forest.fit(_A, label)
_error.append(1 - forest.oob_score_)
_error_mean = np.mean(_error, axis = 0)
oob_error.append(_error_mean)
print 'Batch # %d - OOB error: %f \n' % (i, _error_mean)
print '\nExecuted in: %s. \n ' % (str(datetime.now() - startTime))
# In[65]:
gInterpretationOOBError[kGroup] = oob_error
print 'min_oob error at idx: %d\n' % np.argmin(oob_error)
gInterpretationFeatures[kGroup] = gPreSelectedFeatures[kGroup][:np.argmin(oob_error)+1]
print '\n Interpretation Features: %s' % gInterpretationFeatures[kGroup]
try:
thresholdP = (1/(len(gPreSelectedFeatures[kGroup])-len(gInterpretationFeatures[kGroup])))
_th = []
for j in np.arange(len(gInterpretationFeatures[kGroup]),len(gPreSelectedFeatures[kGroup])-1,1):
_th.append(np.abs(oob_error[j+1] - oob_error[j]))
thresholdP = thresholdP * np.sum(_th)
except:
thresholdP=0.001
print '\n TH - Error Gain - %f' % thresholdP
startTime = datetime.now()
oob_error = []
oob_error_total = []
nEstimators = 2000
predIdx = idx.tolist()
errorGain = []
for i in range(len(idx)):
_epoch = 20
_error = []
#_A = XPreSelected[:,:i+1]
if i > 0:
_A = np.hstack((_A,XPreSelected[:,i].reshape(-1,1)))
else:
_A = XPreSelected[:,:i+1]
print _A.shape
forest = RandomForestClassifier(n_estimators=nEstimators,
warm_start=False,
bootstrap = True,
oob_score=True,
max_features=0.3,
random_state=None,
n_jobs = -1)
for j in range(_epoch):
forest = forest.fit(_A, label)
_error.append(1 - forest.oob_score_)
_error_mean = np.mean(_error, axis = 0)
oob_error.append(_error_mean)
oob_error_total.append(_error_mean)
if i > 0:
_errorGain = oob_error[-2] - oob_error[-1]
if _errorGain > thresholdP:
print '\n Feature - %s, Idx - %d - Added' % (gPreSelectedFeatures[kGroup][i],i)
else:
_A = np.delete(_A, np.s_[-1], 1)
predIdx.remove(idx[i])
del oob_error[-1]
print '\n Feature - %s, Idx - %d - Eliminated' % (gPreSelectedFeatures[kGroup][i],i)
else:
_errorGain = 0
print '\n Feature - %s, Idx - %d - Added' % (gPreSelectedFeatures[kGroup][i],i)
#print _errorGain
errorGain.append(_errorGain)
print '\n Batch # %d - OOB error gain: %f \n' % (i, _errorGain)
print '\nExecuted in: %s. \n ' % (str(datetime.now() - startTime))
# In[98]:
gPredictionFeatures[kGroup] = getNameFeaturesIdx(predIdx, gNameFeatures[kGroup])
gInterpretationIdx[kGroup] = idx.tolist()
gPredictionIdx[kGroup] = predIdx
gPredictionOOBError[kGroup] = oob_error_total
gPredictionOOBErrorGain[kGroup] = errorGain
print '\n Prediction Features: %s' % gPredictionFeatures[kGroup]
A = []
for i in predIdx:
A.append(X[kGroup][:,i])
A = np.asarray(A).T
saveDictsinGVariables(A)
print '\nExecuted in: %s. \n ' % (str(datetime.now() - startTime2))