-
Notifications
You must be signed in to change notification settings - Fork 3
/
RSVM.py
403 lines (288 loc) · 15 KB
/
RSVM.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 22 09:51:39 2016
@author: Melvin
"""
#Implementation of MIQP Ramp loss SVM using IBM CPLEX 12.6.3 Python API
#Algorithm from :
#J. Paul Brooks, (2011) Support Vector Machines with the Ramp Loss and the Hard Margin Loss. Operations Research 52(2):467-479
#Same notations as in paper
from __future__ import print_function
import numpy as np
import cplex
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics.pairwise import rbf_kernel
from sklearn.metrics.pairwise import polynomial_kernel
from sklearn import cross_validation
#Import following package to avoid scikit's deprecation warnings
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
from compiler.ast import flatten
#Compiler package is deprecated and removed in Python 3.x
#Labels preprocessing - all the target values need to be equal to 1 or -1
def PreprocessLabel(y_set):
for i in range(y_set.shape[0]):
if y_set[i] == 0:
y_set[i] = -1
return y_set
#Soft normalization - subtract the mean of the values and divide by twice the standard deviation
def PreprocessData(X_set):
for i in range(X_set.shape[0]):
for j in range(X_set.shape[1]):
X_set[i, j] = (X_set[i, j] - np.mean(X[j])) / 2 * np.std(X[j])
return X_set
#*******************************Dataset setting*******************************
#Simulated more or less noisy data
X, y = make_classification(n_samples= 100, n_features=3, n_redundant=0, n_informative=3,
n_clusters_per_class=2, random_state=123)
plt.scatter(X[:, 0], X[:, 1], marker='o', c=y)
PreprocessData(X)
PreprocessLabel(y)
#Use sklearn to split the dataset to a training set and a test set.
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.4, random_state=1238)
#****************************Parameters setting********************************
#set_dual (bool) : Whether or not set the data to solve dual problem
#set_C (int/float) : Trade-off parameter
#set_kernel (string) : Positive semi-definite kernel = 'poly' for polynomial or 'rbf' for Radial Basis Function or 'linear'
#set_degree (int) : Degree of polynomial kernel function
#set_gamma (int): Parameter of radial basis function
#set_localimplied(value): Instructs CPLEX whether or not to generate locally valid implied bound cuts for the model.
# value = -1 -> Do not generate locally valid implied bound cuts
# value = 0 -> Automatic: let CPLEX choose
# value = 1 -> Generate locally valid implied bound cuts moderately
# value = 2 -> Generate locally valid implied bound cuts aggressively
# value = 3 -> Generate locally valid implied bound cuts very aggressively
#set_timelimit: Sets the maximum CPU time, in seconds, for a call to CPLEX
set_kernel = 'linear'
set_C = 10
set_degree = 2
if set_kernel == 'linear':
set_degree = 1
set_gamma = 1
set_dual = True
set_localimplied = 3
set_timelimit = 10
def MatrixToList(Mat):
MatList = []
for i in range(Mat.shape[1]):
MatList.append([range(Mat.shape[1]), (Mat[i]).tolist()])
return MatList
#Compute Gram matrix associed to chosen kernel : Kij = k(xi, xj)
# X_set = all the dataset to project in higher features space
# degree = set degree for polynomial kernel
# gamma = set gamma parameter for rbf kernel
def Gram_Matrix(Kernel, X_set, Degree, Gamma):
print("Computing Gram Matrix...")
Gram = np.zeros(shape = (X_set.shape[0], X_set.shape[0]))
for i in range(0, (X_set.shape[0])):
for j in range(0, (X_set.shape[0])):
if Kernel == 'poly':
Gram[i, j] = polynomial_kernel(X_set[i], X_set[j], Degree)
elif Kernel == 'rbf':
Gram[i, j] = rbf_kernel(X_set[i], X_set[j], Gamma)
elif Kernel == 'linear':
Gram[i, j] = polynomial_kernel(X_set[i], X_set[j], Degree, coef0=0)
#Use following instruction to fix Gram matrix symmetric problem
Gram = np.maximum(Gram, Gram.transpose())
#Use following instruction to fix CPLEX Error 5002 (objective is not convex)
if set_kernel == 'poly' or set_kernel == 'rbf':
Gram = Gram + np.identity(Gram.shape[1])
print("Done")
return Gram
#FUNCTION : setproblemdata(Arguments)
#Arguments :
#Dual (bool) : Whether or not set the data to solve dual problem
#C (int/float) : Trade-off parameter
#kernel (string) : Positive semi-definite kernel = 'poly' for polynomial or 'rbf' for Radial Basis Function
#degree (int) : Degree of polynomial kernel function
#gamma (int): Parameter of radial basis function
#Parameters need to be tuned when calling the function by SVMIP1_RL or SVMIP2_RL
def setproblemdata(p, Dual=set_dual, C=set_C, kernel=set_kernel, degree=set_degree, gamma=set_gamma):
if Dual == False:
print("Setting primal problem")
p.set_problem_name("SVMIP1_RL")
p.objective.set_sense(p.objective.sense.minimize)
my_colnames = [["w" + str(i) for i in range(1, X_train.shape[1] + 1)], ["b"],
["E" + str(i) for i in range(1, X_train.shape[0] + 1)],
["z" + str(i) for i in range(1, X_train.shape[0] + 1)]]
p.variables.add(types = [p.variables.type.continuous] * len(my_colnames[0]),
names = my_colnames[0], lb=[- cplex.infinity]*len(my_colnames[0]))
qmat = MatrixToList(np.identity(X_train.shape[1]))
p.objective.set_quadratic(qmat)
p.variables.add(obj=[0], types = p.variables.type.continuous, names="b",
lb=[- cplex.infinity])
p.variables.add(obj=[C] * len(my_colnames[2]),
types = [p.variables.type.continuous] * len(my_colnames[2]), names = my_colnames[2],
lb=[0] * len(my_colnames[2]), ub=[2] * len(my_colnames[2]))
p.variables.add(obj=[2*C] * len(my_colnames[3]),
types = [p.variables.type.binary] * len(my_colnames[3]),
names = my_colnames[3])
coefs = []
for i in range(X_train.shape[0]):
coefs.append([y_train[i] * X_train[i], y_train[i], 1.0])
coefs[i][0] = coefs[i][0].tolist()
wlist = my_colnames[0]
Elist = my_colnames[2]
for n in range(X_train.shape[0]):
inds = flatten([wlist, "b", Elist[n]])
fcoefs = flatten(coefs[n])
p.indicator_constraints.add(indvar= my_colnames[3][n], complemented=1,
rhs=1.0, sense='G',
lin_expr=cplex.SparsePair(ind=inds, val=fcoefs))
elif Dual == True:
print("Setting dual problem")
p.set_problem_name("SVMIP2_RL")
p.objective.set_sense(p.objective.sense.minimize)
my_colnames = [["a" + str(i) for i in range(1, X_train.shape[0] + 1)], ["b"],
["E" + str(i) for i in range(1, X_train.shape[0] + 1)],
["z" + str(i) for i in range(1, X_train.shape[0] + 1)]]
p.variables.add(types = [p.variables.type.continuous] * len(my_colnames[0]),
names = my_colnames[0], lb = [0]* len(my_colnames[0]),
ub = [C]* len(my_colnames[0]))
Kmat = Gram_Matrix(Kernel=kernel, X_set=X_train, Degree=degree, Gamma=set_gamma)
Q = np.zeros(shape = (Kmat.shape[0], Kmat.shape[1]))
for i in range(Q.shape[0]):
for j in range(Q.shape[1]):
Q[i, j] = y_train[i] * y_train[j] * Kmat[i, j]
qmat = MatrixToList(Q)
p.objective.set_quadratic(qmat)
p.variables.add(obj=[0], types = p.variables.type.continuous, names="b",
lb=[- cplex.infinity])
p.variables.add(obj=[C] * len(my_colnames[2]),
types = [p.variables.type.continuous] * len(my_colnames[2]), names = my_colnames[2],
lb=[0] * len(my_colnames[2]), ub=[2] * len(my_colnames[2]))
p.variables.add(obj=[2*C] * len(my_colnames[3]),
types = [p.variables.type.binary] * len(my_colnames[3]),
names = my_colnames[3])
coefs = []
for i in range(X_train.shape[0]):
coefs.append([y_train[i] * Kmat[i] * y_train, y_train[i], 1])
coefs[i][0] = coefs[i][0].tolist()
alist = my_colnames[0]
Elist = my_colnames[2]
for n in range(X_train.shape[0]):
inds = flatten([alist, "b", Elist[n]])
fcoefs = flatten(coefs[n])
p.indicator_constraints.add(indvar= my_colnames[3][n], complemented=1,
rhs=1.0, sense='G',
lin_expr=cplex.SparsePair(ind=inds, val=fcoefs))
def Predict(p, Test_set, label_test, Dual=set_dual):
Test_set = X_test
label_test = y_test
sol = p.solution
global test_predicted
test_predicted = np.zeros(shape=label_test.shape[0])
if Dual == False:
sol_vals = []
for i in range(X_train.shape[1] + 1):
sol_vals.append(sol.get_values(i))
w = np.asarray(sol_vals[0:len(sol_vals)-1])
b = sol_vals[len(sol_vals)-1]
for j in range(Test_set.shape[0]):
test_predicted[j] = np.sign(np.inner(w, X_test[j]) + b)
if Dual == True:
sol_vals = []
for i in range(X_train.shape[0]+1):
sol_vals.append(sol.get_values(i))
a = np.asarray(sol_vals[0:len(sol_vals)-1])
b = sol_vals[len(sol_vals)-1]
a_nonzero = []
for i in range(len(a)):
if a[i] != 0:
a_nonzero.append([i, a[i]])
if len(a_nonzero) == 0:
print("No nonzero solution for dual variables")
a_nonzero = np.asarray(a_nonzero)
a_nonzero_index = a_nonzero[:, 0].astype(int)
a_nonzero = a_nonzero[:,1]
for i in range(a_nonzero.shape[0]):
a_nonzero[i] = y_train[a_nonzero_index[i]] * a_nonzero[i]
kernel_mat = np.zeros(shape=(a_nonzero.shape[0], Test_set.shape[0]))
X_critical = []
for i in range(a_nonzero.shape[0]):
X_critical.append(X_train[a_nonzero_index[i]])
if set_kernel == 'poly':
for i in range(len(X_critical)):
for j in range(Test_set.shape[0]):
kernel_mat[i, j] = polynomial_kernel(X_critical[i], Test_set[j], set_degree)
if set_kernel == 'rbf':
for i in range(len(X_critical)):
for j in range(Test_set.shape[0]):
kernel_mat[i, j] = rbf_kernel(X_critical[i], Test_set[j], set_gamma)
if set_kernel == 'linear':
for i in range(len(X_critical)):
for j in range(Test_set.shape[0]):
kernel_mat[i, j] = polynomial_kernel(X_critical[i], Test_set[j], set_degree, coef0=0)
for j in range(Test_set.shape[0]):
test_predicted[j] = np.sign(np.inner(a_nonzero, kernel_mat[:,j]) + b)
#Compute confusion matrix
TP = np.zeros(shape=label_test.shape[0])
TN = np.zeros(shape=label_test.shape[0])
FP = np.zeros(shape=label_test.shape[0])
FN = np.zeros(shape=label_test.shape[0])
for i in range(label_test.shape[0]):
if label_test[i] == 1 and test_predicted[i] == 1:
TP[i] = 1
elif label_test[i] == 1 and test_predicted[i] == -1:
FN[i] = 1
elif label_test[i] == -1 and test_predicted[i] == 1:
FP[i] = 1
elif label_test[i] == -1 and test_predicted[i] == -1:
TN[i] = 1
Confusion_matrix = [[np.sum(TP), np.sum(FN)], [np.sum(FP), np.sum(TN)]]
print("Confusion matrix = ([TP, FN], [FP, TN]) = ", Confusion_matrix)
Sensitivity = Confusion_matrix[0][0] / (Confusion_matrix[0][0] + Confusion_matrix[0][1])
Precision = Confusion_matrix[0][0] / (Confusion_matrix[0][0] + Confusion_matrix[1][0])
Accuracy = (Confusion_matrix[0][0] + Confusion_matrix[1][1]) / (Confusion_matrix[0][0] +
Confusion_matrix[1][1] + Confusion_matrix[0][1] + Confusion_matrix[1][0])
print("Classifier Accuracy = ", Accuracy )
print("Precision = ", Precision)
print("Sensitivity = ", Sensitivity)
return test_predicted
def SVMIP1_RL():
p = cplex.Cplex()
setproblemdata(p, Dual=False)
p.write("SVMIP1_RL.lp")
p.parameters.timelimit.set(set_timelimit)
p.parameters.mip.cuts.localimplied.set(set_localimplied)
print("Solving Ramp Loss SVM primal problem")
p.solve()
sol = p.solution
sol.write("Primal_Solution.lp")
# solution.get_status() returns an integer code
print("Solution status = ", sol.get_status(), ":", end=' ')
# the following line prints the corresponding string
print(sol.status[sol.get_status()])
print("Solution value = ", sol.get_objective_value())
numcols = p.variables.get_num()
for j in range(numcols):
print("Column %d: Value = %10f" % (j, sol.get_values(j)))
print("Test set accuracy")
Y_pred = Predict(p, X_test, y_test)
def SVMIP2_RL():
p = cplex.Cplex()
setproblemdata(p, Dual=True)
p.parameters.timelimit.set(set_timelimit)
p.parameters.mip.cuts.localimplied.set(set_localimplied)
p.write("SVMIP2_RL.lp")
print("Solving Ramp Loss SVM dual problem")
p.solve()
sol = p.solution
sol.write("Dual_Solution.lp")
# solution.get_status() returns an integer code
print("Solution status = ", sol.get_status(), ":", end=' ')
# the following line prints the corresponding string
print(sol.status[sol.get_status()])
print("Solution value = ", sol.get_objective_value())
numcols = p.variables.get_num()
for j in range(numcols):
print("Column %d: Value = %10f" % (j, sol.get_values(j)))
print("Test set accuracy")
Y_pred = Predict(p, X_test, y_test)
if __name__ == "__main__" and set_dual==False:
SVMIP1_RL()
elif __name__ == "__main__" and set_dual==True:
SVMIP2_RL()
else:
print("Error: set Dual value to True or False to run the program")