-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomparison_wine_optimizationParameters.py
213 lines (154 loc) · 5.96 KB
/
comparison_wine_optimizationParameters.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
#------------------------------------------------------------------------------
#
# Advanced Machine Learning - SVR vs. RVR
#
# Author: Lukas Huber
#
#
# Supervisor: Billard Aude
#
#------------------------------------------------------------------------------
print('Start program')
# Machine learnging
from sklearn.utils.estimator_checks import check_estimator
from sklearn.grid_search import GridSearchCV
#from sklearn.model_selection.GridSearchCV import GridSearchCV
from sklearn.cross_validation import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.svm import SVR
from sklearn.preprocessing import normalize
from skbayes.rvm_ard_models import RegressionARD,ClassificationARD,RVR,RVC
# Figure library
import matplotlib.pyplot as plt
# Math library
import numpy as np
from scipy.stats import norm # Calculate norm
from memory_profiler import memory_usage # Memory usage of function
import time
import urllib # Read from link
import csv
import pandas as pd # Import datafiles
# Personal libraries
from ML_treatment import componentAnalysis, normalizeData, componentAnalysisLast
print('Libaries loaded')
# Close all open windows
plt.close()
dataset = np.loadtxt("Datasets/winequality-white.csv",delimiter=';',skiprows=1) #"Users/groenera/Desktop/file.csv"
dataName = 'wineQuality_'
# Crop for easy calcualtion
analysisDim = -1
componentAnalysisLast(dataset[1:10000,:], 'energyData_raw', analysisDim) #plot preprocessing
normalizeData(dataset)
#dataset = dataset[0:1000,:] # crop for ease of calculation
print('Data set loaded with shape: {}'.format(dataset.shape))
plt.show()
# Define Parameters
tt_ratios = np.linspace(0.1,0.9, num=2)
tt_ratios = np.array(0.5)
gammaVals = np.logspace(0.001,100,num=6)
Cvals = np.logspace(0.001, 100, num = 6)
# Inititialize lists
rvr_errs = []
svr_errs = []
t_train_svr = []
t_train_rvr = []
t_test_svr = []
t_test_rvr = []
n_svr = []
n_rvr = []
boxLabels = []
N_crossValid = 1 # no iteration
for itVal in range(tt_ratios.shape[0]):
rvr_errs.append([])
svr_errs.append([])
t_train_svr.append([])
t_train_rvr.append([])
t_test_svr.append([])
t_test_rvr.append([])
n_svr.append([])
n_rvr.append([])
boxLabels.append(str(int(tt_ratios[itVal]*100)) + '%')
for Ncv in range(N_crossValid):
tt_ratio = 0.5
# generate data set
Xc = np.array(dataset[:,0:dataset.shape[1]-1])
Yc = np.array(dataset[:,dataset.shape[1]-1])
X,x,Y,y = train_test_split(Xc,Yc,test_size = tt_ratio, random_state = int(time.time()*10000)%4294967295)
print('Number of training samples {}'.format(X.shape[0]))
# Define hyperparameters
gammaVal = 0.1
C_vals = np.logspace(0.001, 100, 5)
### RVR
# train rvr
rvr = GridSearchCV(RVR(kernel = 'rbf'), param_grid={'gamma',:gammaVals}, cv = 10)
t1 = time.time()
mem_us_rvr = memory_usage((rvr.fit,(X,Y)),interval=0.1)
#rvr.fit(X,Y)
t2 = time.time()
t_train_rvr[itVal].append(t2-t1)
minMem_rvr = min(mem_us_rvr)
maxMem_rvr = max(mem_us_rvr)
# test rvr
t1= time.time()
rvr_errs[itVal].append(mean_squared_error(rvr.predict(x),y))
t2 = time.time()
t_test_rvr[itVal].append(t2-t1)
rvr_s = np.sum(rvr.active_)
n_rvr[itVal].append(rvr_s)
print "RVR -- NMSR {0}, # SV {1}, train time {2}, test time {3}, min Memroy {4}, max Memory {5}".format(rvr_errs[itVal][Ncv], rvr_s,t_train_rvr[itVal][Ncv], t2 - t1,minMem_rvr, maxMem_rvr)
### SVR
# train svr
svr = GridSearchCV(SVR(kernel = 'rbf', gamma = gammaVal), param_grid = {'C':C_vals},cv = 10)
t1 = time.time()
mem_us_svr = memory_usage((svr.fit,(X,Y)),interval=0.1)
#svr.fit(X,Y)
t2 = time.time()
t_train_svr[itVal].append(t2-t1)
minMem_svr = min(mem_us_svr)
maxMem_svr = max(mem_us_svr)
# test svr
t1 = time.time()
svr_errs[itVal].append(mean_squared_error(svr.predict(x),y))
t2 = time.time()
t_test_svr[itVal].append(t2-t1)
svs = svr.best_estimator_.support_vectors_.shape[0]
n_svr[itVal].append(svs)
print "SVR -- NMSR {0}, # SV {1}, train time {2}, test time {3}, min Memory {4}, max. Memory {5}".format(svr_errs[itVal][Ncv], svs, t_train_svr[itVal][Ncv],t2-t1, minMem_svr, maxMem_svr)
plt.figure(figsize = (8,5))
plt.boxplot(svr_errs, 0, 'gD', labels = boxLabels)
name = 'tt_ratios'
plt.title('NMSE - SVR')
plt.savefig('Fig/crossValid_SVR_' + dataName + name + '.png')
plt.figure(figsize = (8,5))
plt.boxplot(rvr_errs, 0, 'gD', labels = boxLabels)
name = 'tt_ratios'
plt.title('NMSE - RVR')
plt.savefig('Fig/crossValid_RVR_' + dataName + name + '.png')
name = 'trainTime'
plt.figure(figsize = (8,5))
plt.boxplot(t_train_svr, 0, 'gD', labels = boxLabels)
plt.title('train time - SVR')
plt.savefig('Fig/crossValid_SVR_' + dataName + name + '.png')
plt.figure(figsize = (8,5))
plt.boxplot(t_train_rvr, 0, 'gD', labels = boxLabels)
plt.title('train time- RVR')
plt.savefig('Fig/crossValid_RVR_' + dataName + name + '.png')
name = 'testTime'
plt.figure(figsize = (8,5))
plt.boxplot(t_test_svr, 0, 'gD', labels = boxLabels)
plt.title('test time - SVR')
plt.savefig('Fig/crossValid_SVR_' + dataName + name + '.png')
plt.figure(figsize = (8,5))
plt.boxplot(t_test_rvr, 0, 'gD', labels = boxLabels)
plt.title('test time - RVR')
plt.savefig('Fig/crossValid_RVR_' + dataName + name + '.png')
name = 'supportVectors'
plt.figure(figsize = (8,5))
plt.boxplot(n_svr, 0, 'gD', labels = boxLabels)
plt.title('suppport Vectors - SVR')
plt.savefig('Fig/crossValid_SVR_' + dataName + name + '.png')
plt.figure(figsize = (8,5))
plt.boxplot(n_rvr, 0, 'gD', labels = boxLabels)
plt.title('suport vectors - RVR')
plt.savefig('Fig/crossValid_RVR_' + dataName + name + '.png')
print('Program terminated')