Python Forum

Full Version: SVM using cvxopt solvers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using python(x,y) python 2.7.10.0

I want to build a SVM using cvxopt solvers but

"sol=solvers.qp(Q,q,A,b)
^
SyntaxError: invalid syntax
>>> Traceback (most recent call last):"

is shown when the solvers is called. Please kindly help solving the problem. Many thanks.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
from cvxopt import solvers
from cvxopt import matrix
from cvxopt.solvers import qp

X1=np.array([[3,4],[2,3]])
X2=np.array([[10,10], [11,11]])
X=np.concatenate((X1,X2), axis=0)

Y1=np.concatenate((np.ones((2,1)),X1), axis=1)
Y2=np.concatenate((np.ones((2,1))*-1,-X2), axis=1)

A=matrix(np.concatenate((Y1,Y2), axis=0),tc='d')

b=matrix(-1*np.ones((4,1)),tc='d')

Q=matrix(2*np.eye(3),tc='d')
q=matrix(np.zeros((3,1)),tc='d'

sol=solvers.qp(Q,q,A,b)
a_con=sol['x']
x=np.arange(0,100,15)
y=-(a_con[0]+a_con[1]*x)/a_con[2]
axes = plt.gca()
axes.set_xlim([0,20])
axes.set_ylim([0,20])
plt.scatter(X[[0,1],0], X[[0,1],1], color='k')
plt.scatter(X[[2,3],0], X[[2,3],1], color='r')
plt.plot(x,y)
plt.show()
you omitted parentheses in line 20.