-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
149 lines (131 loc) · 5.54 KB
/
utils.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
import os
import sys
import glob
import numpy as np
from pylab import *
import pandas as pd
import random
import seaborn as sn
import json
from matplotlib.lines import Line2D
from sklearn.manifold import TSNE
from itertools import groupby, islice, cycle
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans
from sklearn import metrics
from sklearn import datasets, svm, preprocessing
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.model_selection import *
from sklearn.metrics import *
sn.set_context('paper')
# decorating plots
def list_markers():
markers = []
for m in Line2D.markers:
try:
if len(m) == 1 and m != ' ':
markers.append(m)
except TypeError:
pass
styles = markers + [
r'$\lambda$',
r'$\bowtie$',
r'$\circlearrowleft$',
r'$\clubsuit$',
r'$\checkmark$']
return markers
markers_ = list_markers()
markers = markers_ + markers_[:30-len(markers_)]
pal = sn.color_palette("Set1", n_colors=30, desat=.5)
# SVM cross val
def run_svc_pipeline_doubleCV(X, y, dev_split=5, C_=0.015, n_splits_=10,
param_search=False, n_jobs_=18, verbose = False):
# use different splits with different random states for CV-param search
svc = svm.SVC(kernel='linear', C = C_)
#svc = svm.LinearSVC(C = C_)
pipeline_estimators = [('scale', preprocessing.StandardScaler()), ('svm',
svc) ]
svc_pipeline = Pipeline(pipeline_estimators)
if param_search:
if verbose: print('param search is ONNN!')
C_search = sorted( list(np.logspace(-5,0,10)) + [0.1,5,10,20,50,100] )
param_grid = dict( scale=[None], svm__C=C_search )
sk_folds = StratifiedKFold(n_splits=dev_split, shuffle=False,
random_state=1964)
grid_search = GridSearchCV(svc_pipeline, param_grid=param_grid,
n_jobs=n_jobs_, cv=sk_folds.split(X,y),
verbose=verbose)
grid_search.fit(X, y)
# find the best C value
which_C = np.argmax(grid_search.cv_results_['mean_test_score'])
best_C = C_search[which_C]
else:
best_C = C_
print('estimated the best C for svm to be', best_C)
sk_folds = StratifiedKFold(n_splits=n_splits_, shuffle=False, random_state=320)
all_scores = []
all_y_test = []
all_pred = []
for train_index, test_index in sk_folds.split(X, y):
if verbose: print 'run -',
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
svc_pipeline = Pipeline(pipeline_estimators)
svc_pipeline.named_steps['svm'].C = best_C
svc_pipeline.fit(X_train, y_train)
y_pred = svc_pipeline.predict(X_test)
score = svc_pipeline.score(X_test, y_test)
if verbose: print score
all_y_test.append(y_test)
all_pred.append(y_pred)
all_scores.append(score)
return all_y_test, all_pred, all_scores
def create_svc_pipeline(X, y, X_test, y_test, dev_split=10, C_=0.015, n_splits_=10,
param_search=False, n_jobs_=18, verbose = False):
# use different splits with different random states for CV-param search
svc = svm.SVC(kernel='linear', C = C_)
#svc = svm.LinearSVC(C = C_)
pipeline_estimators = [('scale', preprocessing.StandardScaler()), ('svm',
svc) ]
svc_pipeline = Pipeline(pipeline_estimators)
if param_search:
if verbose: print('param search is ONNN!')
C_search = sorted( list(np.logspace(-5,0,10)) + [0.1,5,10,20,50,100] )
param_grid = dict( scale=[None], svm__C=C_search )
sk_folds = StratifiedKFold(n_splits=dev_split, shuffle=False,
random_state=1964)
grid_search = GridSearchCV(svc_pipeline, param_grid=param_grid,
n_jobs=n_jobs_, cv=sk_folds.split(X,y),
verbose=verbose)
grid_search.fit(X, y)
# find the best C value
which_C = np.argmax(grid_search.cv_results_['mean_test_score'])
best_C = C_search[which_C]
else:
best_C = C_
svc_pipeline.named_steps['svm'].C = best_C
print('estimated the best C for svm to be', best_C)
# after param search retrain with all data!
svc_pipeline.fit(X, y)
y_pred = svc_pipeline.predict(X_test)
perf_report = classification_report(y_test, y_pred)
print(perf_report)
return svc_pipeline, y_test, y_pred, perf_report
def rcycle(iterable):
#http://davidaventimiglia.com/python_generators.html
# this is itertools.cycle but shuffle from the second cycle onwards
saved = [] # In-memory cache
for element in iterable:
yield element
saved.append(element)
while saved:
random.shuffle(saved) # Shuffle every batch
for element in saved:
yield element
# read wavfile - and rearrange (n_modalities, batch_size, channel, frames, num_mels)
WavLoad = lambda f: \
np.asarray([[np.load(j.replace('.wav', '.npy')) for j in i] for i in f]).swapaxes(0,1)
#np.asarray( [[wavfile_to_examples(j) for j in i] for i in f] ).swapaxes(0,1)
# read wavfile - in form (batch_size, channel, frames, num_mels)
WavLoad_OneBranch = lambda f: \
np.asarray([ np.load(j.replace('.wav', '.npy')) for j in f ])