-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQP_solver.py
97 lines (74 loc) · 2.76 KB
/
QP_solver.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
import pandas as pd
import numpy as np
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import cvxopt
import cvxopt.solvers
from cvxopt import solvers
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/ionosphere/ionosphere.data", header = None)
labels = df[df.columns[34]]
datas = df[df.columns[0:34]]
#encoding labels into numerical values
enc = LabelEncoder()
enc.fit(labels)
labels = enc.transform(labels)
datas = np.mat(datas)
labels = (np.mat(labels)).T
for i in range (len(df)):
if (np.asscalar(labels[i,:]) == 0):
labels[i,:] = -1
df = pd.DataFrame(np.column_stack((datas,labels)))
train, test = train_test_split(df,train_size = 0.5)
X_train = np.mat(train[train.columns[0:34]])
y_train = np.mat(train[train.columns[34]]).T
X_test = np.mat(test[test.columns[0:34]])
y_test = np.mat(test[test.columns[34]]).T
size_train, dim_train = train.shape
#formulating the inputs for QP solver
h = -(np.mat(np.ones((size_train,1))))
q = np.mat(np.zeros((35,1)))
p = np.eye(35)
p[34,34] = 0
G = y_train
row = X_train[0,:]*np.asscalar(y_train[0,:])
for i in range (1,size_train):
y = np.asscalar(y_train[i,:])
temp = (X_train[i,:] * y)
row = np.mat(np.vstack((row,temp)))
G = -(np.mat(np.column_stack((row,G))))
#transfer the inputs to the required format
P = cvxopt.matrix(p,tc='d')
q = cvxopt.matrix(q,tc='d')
G = cvxopt.matrix(G,tc='d')
h = cvxopt.matrix(h,tc='d')
#solve the QP
sol = solvers.qp(P,q,G,h)
#get the weight vector and intercept from the QP result
w = np.mat(sol['x'])
b = np.asscalar(w[34,:])
w = w [0:34,:]
#Classifaction of the train set
X_train = np.mat(X_train)
y_predict_train = (X_train*w)+b
for i in range (len(y_predict_train)):
yi = np.asscalar(y_predict_train[i,:])
if (yi >= 0):
y_predict_train[i,:] = 1
elif (yi < 0):
y_predict_train[i,:] = -1
#Prediction of the test set
y_predict_test = (X_test*w)+b
for i in range (len(y_predict_test)):
yi = np.asscalar(y_predict_test[i,:])
if (yi >= 0):
y_predict_test[i,:] = 1
elif (yi < 0):
y_predict_test[i,:] = -1
accuracy_train = ((len(train))-np.count_nonzero(y_train - y_predict_train))/(len(train))
accuracy_test = ((len(test))-np.count_nonzero(y_test - y_predict_test))/(len(test))
correct_train = (len(train))-np.count_nonzero(y_train - y_predict_train)
correct_test = (len(test))-np.count_nonzero(y_test - y_predict_test)
print(correct_train, " out of ", len(train), " were classified correctly in the train set")
print(correct_test, " out of ", len(test), " were classified correctly in the test set")
print("The model can classify the data withe accuracy of ", accuracy_train, " for the training set, and ", accuracy_test, " for the test set.")