-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
196 lines (140 loc) · 5.34 KB
/
code.py
File metadata and controls
196 lines (140 loc) · 5.34 KB
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 4 14:41:45 2017
@author: micheles
"""
import numpy as np
import matplotlib.pyplot as plt
import sklearn
import time
# Load database
import scipy.io as sio
database = sio.loadmat('./data/mnist-original.mat')
all_data = database['data'].T
GT = database['label'].T
# What we have?
n_samples, m_features = all_data.shape
print 'Shape: ' + str(all_data.shape)
print 'GT values: ' + str(np.unique(GT))
one_sample = np.reshape(all_data[12,:],(int(np.sqrt(m_features)),int(np.sqrt(m_features))))
plt.imshow(one_sample)
print 'sample range is: MAX=%d, min=%d' % (np.max(one_sample),np.min(one_sample))
how_many_samples_per_class = plt.hist(GT)
print 'How many samples per class: ' + str(how_many_samples_per_class[0])
# Split data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
all_data, GT, test_size=0.50, random_state=42)
X_test, X_valid, y_test, y_valid = train_test_split(
X_test, y_test, test_size=0.50, random_state=42)
y_train = y_train.reshape(-1)
y_valid = y_valid.reshape(-1)
y_test = y_test.reshape(-1)
print 'Shape (train): ' + str(X_train.shape)
print 'Shape (valid): ' + str(X_valid.shape)
print 'Shape (test): ' + str(X_test.shape)
# Fast SVM trial
from sklearn.svm import LinearSVC
clf = LinearSVC()
t = time.time()
print 'Training linear SVM...'
clf.fit(X_train,y_train)
print 'Done in (sec): %.3f' % (time.time() - t)
y_valid_pred = clf.predict(X_valid)
from sklearn.metrics import classification_report
print(classification_report(y_valid, y_valid_pred, target_names=['Class_' + str(i) for i in np.unique(GT)]))
print 'Total accuracy (%%): %.3f' % (clf.score(X_valid,y_valid)*100)
# Let's scale the data first
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)
print 'Shape (train): ' + str(X_train.shape)
print 'Shape (valid): ' + str(X_valid.shape)
print 'Shape (test): ' + str(X_test.shape)
# SVM trial after scaling
t = time.time()
print 'Training linear SVM...'
clf.fit(X_train,y_train)
print 'Done in (sec): %.3f' % (time.time() - t)
y_valid_pred = clf.predict(X_valid)
from sklearn.metrics import classification_report
print(classification_report(y_valid, y_valid_pred, target_names=['Class_' + str(i) for i in np.unique(GT)]))
print 'Total accuracy (%%): %.3f' % (clf.score(X_valid,y_valid)*100)
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X_train, y_train)
y_valid_pred = clf.predict(X_valid)
print(classification_report(y_valid, y_valid_pred, target_names=['Class_' + str(i) for i in np.unique(GT)]))
print 'Total accuracy (%%): %.3f' % (clf.score(X_valid,y_valid)*100)
from skopt import BayesSearchCV
from skopt.space import Real, Categorical, Integer
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
opt = BayesSearchCV(SVC(), { 'C': Real(1e-6, 1e+6, prior='log-uniform'),
'gamma': Real(1e-6, 1e+1, prior='log-uniform'),
'degree': Integer(1,8), 'kernel': Categorical(['linear', 'poly', 'rbf']), },
n_iter=32, n_jobs=-1)
t = time.time()
opt.fit(X_train, y_train)
print 'Optimisation of SVM done in (sec): %.3f' % (time.time() - t)
#print(opt.score(X_test, y_test))
# The show begin
from sklearn.pipeline import Pipeline
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from skopt.space import Real, Categorical, Integer
from skopt import BayesSearchCV # !!! It needs SKOPT 0.4
# used to try different model classes
pipe = Pipeline([
('model', SVC())
])
# single categorical value of 'model' parameter is used to set the model class
lin_search = {
'model': Categorical([LinearSVC()]),
'model__C': Real(1e-6, 1e+6, prior='log-uniform'),
}
knn_search = {
'model': Categorical([KNeighborsClassifier()]),
'model__n_neighbors': Integer(1, 10),
}
rfc_search = {
'model': Categorical([RandomForestClassifier()]),
'model__max_depth': Integer(1, 10),
'model__n_estimators': Integer(1, 20),
'model__max_features': Integer(1, 5),
}
mlp_search = {
'model': Categorical([MLPClassifier()]),
'model__alpha': Integer(1, 10),
}
nb_search = {
'model': Categorical([GaussianNB()])
}
ab_search = {
'model': Categorical([AdaBoostClassifier()])
}
dtc_search = {
'model': Categorical([DecisionTreeClassifier()]),
'model__max_depth': Integer(1, 32),
'model__min_samples_split': Real(1e-3, 1.0, prior='log-uniform'),
}
svc_search = {
'model': Categorical([SVC()]),
'model__C': Real(1e-6, 1e+6, prior='log-uniform'),
'model__gamma': Real(1e-6, 1e+1, prior='log-uniform'),
'model__degree': Integer(1, 8),
'model__kernel': Categorical(['linear', 'poly', 'rbf']),
}
opt = BayesSearchCV(pipe,
[(lin_search, 16), (dtc_search, 24), (svc_search, 64),
(nb_search, 1), (ab_search, 1), (knn_search, 16),
(rfc_search, 32), (mlp_search, 16)], # (parameter space, # of evaluations)
n_jobs=20)
opt.fit(X_train, GT_train)