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 type double as well. So just using A=A.astype('double')
to cast it.
i have try A=A.astype(double)
to solve it, but it is invalid, since python doesn't know what double is or A has no method astype.
therefore
via using
A = matrix(A, (1, m), 'd')
could actually solve this problem!
The error - "TypeError: 'A' must be a 'd' matrix with 1000 columns:"
has two condition namely:
- if the type code is not equal to '
d
' - if the
A.size[1] != c.size[0]
.
Check for these conditions.
来源:https://stackoverflow.com/questions/36510859/cvxopt-qp-solver-typeerror-a-must-be-a-d-matrix-with-1000-columns