-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClasificacionSVM3.py
314 lines (260 loc) · 10.1 KB
/
ClasificacionSVM3.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
import pywt
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.model_selection import learning_curve
from sklearn.metrics import confusion_matrix
import pickle
filename0 = "/home/usr3/Documents/UTFSM/1erSemestre/MachineLearning/Proyecto/Data/Hojas1.txt"
filename1 = "/home/usr3/Documents/UTFSM/1erSemestre/MachineLearning/Proyecto/Data/Hojas2.txt"
filename2 = "/home/usr3/Documents/UTFSM/1erSemestre/MachineLearning/Proyecto/Data/Hojas3.csv"
##Constants
NBR_TST_DATA = 7
lol=[]
##Main
def main():
##Variables
ftrVct0 = [] #feature vector 0
ftrVct1 = [] #feature vector 1
rawData = [] #raw Data
trnData = [] #data used to train
tstData = [] #data to test
lbls = []
# =============================================================================
# IMPORTANTE PARA CLASIFICAR
# =============================================================================
##Read firts data
rawData0 = readFile(filename0)
rawData0 = rawData0[:,0:850] # Recortando las bandas
ftrVct0 = featureExtraction(*rawData0) # 23 features
## ftrVct0 = rawData0 #### VA CON TODOS LOS DATOS (TODAS LAS BANDAS)
##Read second data
rawData1 = readFile(filename1)
rawData1 = rawData1[:,0:850]
ftrVct1 = featureExtraction(*rawData1)
## ftrVct1 = rawData1 #### VA CON TODOS LOS DATOS
## Read Third data
rawData2 = readFile(filename2)
rawData2 = rawData2[:,0:850]
ftrVct2 = featureExtraction(*rawData2)
## ftrVct2 = rawData2 #### VA CON TODOS LOS DATOS
rawData = np.concatenate((ftrVct0,ftrVct1,ftrVct2),axis = 0)
trnData = normalizeData(*rawData)
lbls = np.concatenate((np.zeros((len(ftrVct0),1)),np.ones((len(ftrVct1),1)), 2*np.ones((len(ftrVct2),1))),axis=0)
X_train, X_test, y_train, y_test = train_test_split(trnData, lbls, test_size=0.2, random_state=0)
y_true = y_test; # Almacenando los labels anteriores
#C_range = np.logspace(-2, 10, 20)
#gamma_range = np.logspace(-9, 3, 20)
#param_grid = dict(gamma=gamma_range, C=C_range)
#cv = StratifiedShuffleSplit(n_splits=10, test_size=0.2, random_state=42)
#grid = GridSearchCV(SVC(), param_grid=param_grid, cv=cv)
#grid.fit(X_train,y_train.ravel())
# fijo
grid = SVC(kernel='rbf', random_state=0, gamma=1, C=1)
grid.fit(X_train,y_train.ravel())
# C = grid.best_estimator_.C
# print("Best C: %0.5f"%C)
# g = grid.best_estimator_.gamma
# print("Best gamma: %0.5f"%g)
trnScore = grid.score(X_train, y_train.ravel())
print("Score (trainData): %0.5f"%trnScore)
tstScore = grid.score(X_test, y_test.ravel())
print("Score (testData): %0.5f"%tstScore)
tstPredict = grid.predict(X_test)
a = confusion_matrix(y_true,tstPredict)
print(a)
# =============================================================================
# GUARDAR EL CLASIFICADOR
# =============================================================================
# now you can save it to a file
with open('filename.pkl', 'wb') as f:
pickle.dump(grid, f)
# =============================================================================
# LEER EL CLASIFICADOR
# =============================================================================
# with open('filename.pkl', 'rb') as f:
# clf = pickle.load(f)
#clf = svm.SVC(C=float(C), kernel = 'rbf', gamma = float(g))
#clf.fit(trnData,lbls.ravel())
# predicted0 = grid.predict(X_train)
# predicted1 = grid.predict(X_test)
#title = "Learning Curves (SVM, RBF kernel, $\gamma=0.001$)"
# SVC is more expensive so we do a lower number of CV iterations:
#estimator = svm.SVC(C=float(C), kernel = 'rbf', gamma = float(g))
#plot_learning_curve(estimator, title, trnData, lbls.ravel(), (0.7, 1.01), cv=cv, n_jobs=4)
#plt.show()
#Lbls for all data
#lbls = np.concatenate((np.zeros((len(ftrVct0),1)),np.ones((len(ftrVct1),1))),axis=0)
#fullData = np.concatenate((ftrVct0,ftrVct1),axis = 0)
#predicted = cross_val_predict(clf, fullData, lbls, cv=10)
# for i in range(0,int(len(ftrVct0)+len(ftrVct1))):
# if grid.predict(trnData[i].reshape(1,-1)) == 0:
# print('Es la hoja 1', 'Deberia ser:', lbls[i]+1)
# else:
# print('Es la hoja 2', 'Deberia ser:', lbls[i]+1)
############################
# FUNCTIONS
############################
##Plot Learning Functions
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
"""
Generate a simple plot of the test and training learning curve.
Parameters
----------
estimator : object type that implements the "fit" and "predict" methods
An object of that type which is cloned for each validation.
title : string
Title for the chart.
X : array-like, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape (n_samples) or (n_samples, n_features), optional
Target relative to X for classification or regression;
None for unsupervised learning.
ylim : tuple, shape (ymin, ymax), optional
Defines minimum and maximum yvalues plotted.
cv : int, cross-validation generator or an iterable, optional
Determines the cross-validation splitting strategy.
Possible inputs for cv are:
- None, to use the default 3-fold cross-validation,
- integer, to specify the number of folds.
- An object to be used as a cross-validation generator.
- An iterable yielding train/test splits.
For integer/None inputs, if ``y`` is binary or multiclass,
:class:`StratifiedKFold` used. If the estimator is not a classifier
or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.
Refer :ref:`User Guide <cross_validation>` for the various
cross-validators that can be used here.
n_jobs : integer, optional
Number of jobs to run in parallel (default 1).
"""
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(
estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Cross-validation score")
plt.legend(loc="best")
return plt
#Read files
def readFile(filename):
data = []
outVct = []
f = open(filename,"r")
ctr0 = 0
for line in f:
data.append(line)
outVct.append(data[ctr0].split(","))
ctr0 +=1
for i in range(0, len(outVct)):
for j in range(0, len(outVct[0])):
outVct[i][j] = float(outVct[i][j])
outVct = np.transpose(outVct)
return outVct
def normalizeData(*rawData):
[rows, cols] = np.asarray(rawData).shape
normData = []
for i in range(0, rows):
tmpVct = rawData[i]
max_abs_scaler = preprocessing.MaxAbsScaler()
tmpVct = max_abs_scaler.fit_transform(tmpVct.reshape(-1,1))
#print('Shape rawData: ', np.asarray(tmpVct).shape)
normData.append(tmpVct[:,0])
return normData
#Extract Features
def featureExtraction(*outVct):
##Local Variables
detailVct = []
statFeatVct = []
##Subband descomposition
for i in range(0, len(outVct)):
(cA1, cD1) = pywt.dwt(outVct[i],'db3')
(cA2, cD2) = pywt.dwt(cA1,'db3')
(cA3, cD3) = pywt.dwt(cA2,'db3')
(cA4, cD4) = pywt.dwt(cA3,'db3')
(cA5, cD5) = pywt.dwt(cA4,'db3')
detailVct.append([cD1,cD2,cD3,cD4,cD5,cA5])
# print(cA5)
##Statistical Features
for i in range(0, len(outVct)):
cD1mean = np.mean(np.absolute(detailVct[i][0]))
cD1AvPow = np.mean(np.power(detailVct[i][0],2)/np.size(detailVct[i][0]))
cD1Std = np.std(detailVct[i][0])
#
cD2mean = np.mean(np.absolute(detailVct[i][1]))
cD2AvPow = np.mean(np.power(detailVct[i][1],2)/np.size(detailVct[i][1]))
cD2Std = np.std(detailVct[i][1])
#
cD3mean = np.mean(np.absolute(detailVct[i][2]))
cD3AvPow = np.mean(np.power(detailVct[i][2],2)/np.size(detailVct[i][2]))
cD3Std = np.std(detailVct[i][2])
#
cD4mean = np.mean(np.absolute(detailVct[i][3]))
cD4AvPow = np.mean(np.power(detailVct[i][3],2)/np.size(detailVct[i][3]))
cD4Std = np.std(detailVct[i][3])
#
cD5mean = np.mean(np.absolute(detailVct[i][4]))
cD5AvPow = np.mean(np.power(detailVct[i][4],2)/np.size(detailVct[i][4]))
cD5Std = np.std(detailVct[i][4])
#
cA5mean = np.mean(np.absolute(detailVct[i][5]))
cA5AvPow = np.mean(np.power(detailVct[i][5],2)/np.size(detailVct[i][5]))
cA5Std = np.std(detailVct[i][5])
#
cD1ratio = cD1mean/cD2mean
cD2ratio = cD2mean/cD3mean
cD3ratio = cD3mean/cD4mean
cD4ratio = cD4mean/cD5mean
cD5ratio = cD5mean/cD4mean
#
statFeatVct.append([cD1mean, cD1AvPow, cD1Std, cD1ratio, cD2mean, cD2AvPow, cD2Std, cD2ratio, cD3mean, cD3AvPow, cD3Std, cD3ratio, cD4mean, cD4AvPow, cD4Std, cD4ratio, cD5mean, cD5AvPow, cD5Std, cD5ratio, cA5mean, cA5AvPow, cA5Std])
#
max_abs_scaler = preprocessing.MaxAbsScaler()
return max_abs_scaler.fit_transform(statFeatVct) ##Return feature vector scaled
if __name__ == '__main__':
main()
#print(cA)
#print(cD)
#plt.subplot(231)
#plt.plot(cA5)
#plt.title('CA5')
#plt.subplot(232)
#plt.plot(cD1)
#plt.title('CD1')
#plt.subplot(233)
#plt.plot(cD2)
#plt.title('CD2')
#plt.subplot(234)
#plt.plot(cD3)
#plt.title('CD3')
#plt.subplot(235)
#plt.plot(cD4)
#plt.title('CD4')
#plt.subplot(236)
#plt.plot(cD5)
#plt.title('CD5')
#plt.show()
##plt.plot(cA, cD)
##plt.ylabel('wavelets')
##plt.show()