-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvm_regression.py
More file actions
268 lines (226 loc) · 7.79 KB
/
Copy pathsvm_regression.py
File metadata and controls
268 lines (226 loc) · 7.79 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
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
import pandas as pd
import cPickle
import gzip
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn import preprocessing
import matplotlib.pyplot as plt
import csv
import os
####################################################
# import some data to play with
path = '/Users/Mickey/Desktop/Group/All_Data/'
all_Feat = pd.read_csv(path+'T1_T2_Vol.csv')
all_Feat = all_Feat[all_Feat.columns[1:]]
labels = pd.read_csv(path+'BirthGA.csv')
labels = labels[labels.columns[2]]
#####################################################
## FEATURES EXTRACTION ####################
#Change in code for the input data as features from either Adaboost or Random Forest
'''
path = '/Users/Mickey/Desktop/Group/ADA_FULL_REG.csv'
RF_sf = pd.read_csv(path,names = ['Region','Weight','Type'])
# 50 most important regions - features
print RF_sf
raw_input()
RF_imregion= RF_sf.Region[1:66]
RF_imtype = RF_sf.Type[1:66]
RF_imweight = RF_sf.Weight[1:66]
print RF_imtype
print RF_imweight
print np.shape(RF_imtype)
print np.shape(RF_sf)
path1 = '/Users/Mickey/Desktop/Group/All_Data/'
T1 = pd.read_csv(path1+'T1.csv')
print T1
T1 = T1[T1.columns[1:]]
T2 = pd.read_csv(path1+'T2.csv')
T2 = T2[T2.columns[1:]]
vol = pd.read_csv(path1+'Volume.csv')
vol= vol[vol.columns[1:]]
print np.shape(T1)
print T1
print np.shape(T2)
print np.shape(vol)
print '##############################'
data_im = pd.DataFrame(index=range(len(T1)), columns=range(65))
data_im = data_im.fillna(0)
print np.shape(data_im)
print RF_imweight
raw_input()
for x in range(len(RF_imregion)):
reg = RF_imtype[x+1]
print reg
print RF_imregion[x+1]
print int(RF_imregion[x+1])
if reg == 'T1':
#for i in range(len(T1)):
currT1 = T1[T1.columns[int(RF_imregion[x+1])-1]]
data_im[data_im.columns[x]] = currT1
print currT1
#pd.data_im.concat([data_im, currT1], axis=1)
if reg == 'T2':
#for i in range(len(T1)):
currT2 = T2[T2.columns[int(RF_imregion[x+1])-1]]
data_im[data_im.columns[x]] = currT2
#pd.data_im.concat([data_im, currT2], axis=1)
if reg == 'Vol':
#for i in range(len(T1)):
currVol = vol[vol.columns[int(RF_imregion[x+1])-1]]
data_im[data_im.columns[x]] = currVol
#pd.data_im.concat([data_im, currVol], axis=1)
data_im = data_im[data_im.columns[:65]]
###############################
'''
x_all = all_Feat
y_all = labels
########################################################
#TRAINING AND TESTING DATA :
# X - Trainin data
# X_test - testing data
# y - traininglabels
# y_test - testing labels
X, X_test, y, y_test = train_test_split(x_all, y_all,train_size=0.5,random_state = 42)
y = np.ravel(y)
y_test = np.ravel(y_test)
###############################################################################
#Initialise all parameters
gamma = [1e-3, 1e-2, 1e-1, 1, 1e1, 1e2,1e3]
kernel = ['linear','rbf']
C = [1, 10, 100,1000]
curr_max = -10000
curr_min = 10000
all_gamma = []
all_C = []
all_kernel = []
all_scores = []
all_all_cscores = []
all_cscores = []
all_std = []
#Obtain optimum parameters
for x in range(len(kernel)):
for j in range(len(C)):
if kernel[x] == 'linear':
if C[j] < 1000:
print kernel[x], C[j]
svm = SVR(kernel= kernel[x], C=C[j])
svm.fit(X,y)
pre = svm.predict(X_test)
curr_score = svm.score(X_test, y_test)
curr_cross_score = cross_val_score(svm, x_all, y_all)
curr_mean = np.mean(curr_cross_score)
curr_std = np.std(curr_cross_score)
print curr_score
print curr_mean
print curr_std
all_all_cscores.append(curr_cross_score[0])
all_all_cscores.append(curr_cross_score[1])
all_all_cscores.append(curr_cross_score[2])
all_scores.append(curr_score)
all_cscores.append(curr_mean)
all_std.append(curr_std)
if curr_mean > curr_max:
#Store parameters of maximum cross val score
curr_max = curr_mean
best_cross = curr_mean
best_score = curr_score
best_std = curr_std
best_C = C[j]
best_gamma = []
best_kernel = kernel[x]
if curr_mean < curr_min:
#Store parameters of minimum cross val score
curr_min = curr_mean
worst_cross = curr_mean
worst_score = curr_score
worst_std = curr_std
worst_C = C[j]
worst_gamma = []
worst_kernel = kernel[x]
if kernel[x] == 'rbf':
for i in range(len(gamma)):
print kernel[x], C[j], gamma[i]
svm = SVR(kernel='rbf', C=C[j],gamma=gamma[i])
svm.fit(X,y)
pre = svm.predict(X_test)
curr_score = svm.score(X_test, y_test)
curr_cross_score = cross_val_score(svm, x_all, y_all)
all_all_cscores.append(curr_cross_score[0])
all_all_cscores.append(curr_cross_score[1])
all_all_cscores.append(curr_cross_score[2])
curr_mean = np.mean(curr_cross_score)
curr_std = np.std(curr_cross_score)
curr_mean = np.mean(curr_cross_score)
curr_std = np.std(curr_cross_score)
print curr_score
print curr_mean
print curr_std
all_scores.append(curr_score)
all_cscores.append(curr_mean)
all_std.append(curr_std)
if curr_mean > curr_max:
#Store parameters of maximum cross val score
best_cross = curr_mean
curr_max = curr_mean
best_score = curr_score
best_std = curr_std
best_C = C[j]
best_gamma = gamma[i]
best_kernel = kernel[x]
if curr_mean< curr_min:
#Store parameters of minimum cross val score
curr_min = curr_mean
worst_cross = curr_mean
worst_score = curr_score
worst_std = curr_std
worst_C = C[j]
worst_gamma = gamma[i]
worst_kernel = kernel[x]
print ' '
print '########################################################################'
print 'BEST AND WORST'
print best_cross
print best_std
print best_score
print best_kernel
print best_C
print best_gamma
print worst_cross
print worst_std
print worst_score
print worst_kernel
print worst_C
print worst_gamma
print '########################################################################'
print ' '
###############################################################################
# Plot the result
predw = []
predb = []
### BEST
svm_best = SVR(kernel=best_kernel, C=best_C,gamma=best_gamma)
svm_best.fit(X,y)
pre_best = svm_best.predict(X_test)
lw = 2
#WORST
svm_worst = SVR(kernel=worst_kernel, C=worst_C,gamma=worst_gamma)
svm_worst.fit(X,y)
pre_worst = svm_worst.predict(X_test)
lw = 2
plt.subplot(1,2,1)
plt.scatter(y_test, pre_best, color='navy', lw=lw)
plt.plot([30, 50], [30, 50], '--k')
plt.xlabel('Data')
plt.ylabel('Predicted')
plt.title('Support Vector Regression - Best Parameters')
plt.legend()
plt.subplot(1,2,2)
plt.scatter(y_test, pre_worst, color='navy', lw=lw)
plt.plot([30, 50], [30, 50], '--k')
plt.xlabel('Data')
plt.ylabel('Predicted')
plt.title('Support Vector Regression - Worst Parameters')
plt.legend()
plt.show()