I'm trying to use the CVXOPT qp solver to compute the Lagrange Multipliers for a Support Vector Machine
def svm(X, Y, c):
m = len(X)
P = matrix(np.dot(Y, Y.T) * np.dot(X, X.T))
q = matrix(np.ones(m) * -1)
g1 = np.asarray(np.diag(np.ones(m) * -1))
g2 = np.asarray(np.diag(np.ones(m)))
G = matrix(np.append(g1, g2, axis=0))
h = matrix(np.append(np.zeros(m), (np.ones(m) * c), axis =0))
A = np.reshape((Y.T), (1,m))
b = matrix([0])
print (A).shape
A = matrix(A)
sol = solvers.qp(P, q, G, h, A, b)
print sol
Here X
is a 1000 X 2
matrix and Y
has the same number of labels. The solver throws the following error:
$ python svm.py
(1, 1000)
Traceback (most recent call last):
File "svm.py", line 35, in <module>
svm(X, Y, 50)
File "svm.py", line 29, in svm
sol = solvers.qp(P, q, G, h, A, b)
File "/usr/local/lib/python2.7/site-packages/cvxopt/coneprog.py", line 4468, in qp
return coneqp(P, q, G, h, None, A, b, initvals, options = options)
File "/usr/local/lib/python2.7/site-packages/cvxopt/coneprog.py", line 1914, in coneqp
%q.size[0])
TypeError: 'A' must be a 'd' matrix with 1000 columns
I printed the shape of A and it's a (1,1000)
matrix after reshaping from a vector. What exactly is causing this error?
Your matrix elements have to be of the floating-point type as well. So the error is removed by using A = A.astype('float')
to cast it.