-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm.run
78 lines (56 loc) · 1.45 KB
/
svm.run
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
# OTDM Lab 2: run file
# Marcel, Mengxue
# Autumn 2021
# To run this file:
# $> ampl: include svm.run;
#########
# TRAIN #
#########
# Solve the primal
reset;
print "SVM_PRIMAL:";
model svm-primal.mod;
data "./data/size2000-seed75421.dat"; #spambase #size100-seed66407 #size2000-seed75421
option solver cplex; #gurobi
problem SVM_PRIMAL: w, gamma, s, primal, c1, c2;
solve SVM_PRIMAL;
display w, gamma, s;
# Solve the dual
reset;
print "SVM_DUAL:";
model svm-dual.mod;
data "./data/size2000-seed75421.dat";
option solver cplex;
problem SVM_DUAL: lambda, dual, c1;
solve SVM_DUAL;
display lambda;
# Compute w, gamma from the dual solution
param w {1..n};
let {j in {1..n}} w[j] := sum{i in {1..m}} lambda[i]*y_train[i]*A_train[i,j];
display w;
param gamma;
for {i in {1..m}} {
if lambda[i] > 0.01 and lambda[i] < nu*0.99 then {
# A support vector point was found
let gamma := 1/y_train[i] - sum{j in {1..n}} w[j]*A_train[i,j];
break;
}
}
display gamma;
########
# TEST #
########
# Predict values with the test dataset
param y_pred {1..m};
let {i in {1..m}} y_pred[i] := gamma + sum{j in {1..n}}w[j]*A_test[i,j];
let {i in {1..m}} y_pred[i] := if y_pred[i] <= 0 then -1 else 1;
display y_pred;
# Check misclassifications
param misclassifications default 0;
for {i in {1..m}} {
if y_pred[i] != y_test[i] then
let misclassifications := misclassifications + 1;
}
display misclassifications;
param accuracy = (m - misclassifications) / m;
display accuracy;