CVXOPT QP解算器:TypeError:'A'必须是具有1000列的'd'矩阵 [英] CVXOPT QP Solver: TypeError: 'A' must be a 'd' matrix with 1000 columns
问题描述
我正在尝试使用CVXOPT qp解算器来计算支持向量机的拉格朗日乘数
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
此处 X
是 1000 X 2
矩阵和 Y
具有相同数量的标签。求解器抛出以下错误:
$ python svm.py
(1,1000)
Traceback(最近一次调用为最新):
File svm .py,第35行,在< module>中
svm(X,Y,50)
文件 svm.py,第29行,在svm中
sol = resolvers.qp(P,q,G,h,A,b)
文件 /usr/local/lib/python2.7/site-packages/cvxopt/coneprog.py,第4468行,在qp
中,它会返回锥体qp(P,q,G,h,None, A,b,initvals,options = options)
文件 /usr/local/lib/python2.7/site-packages/cvxopt/coneprog.py,行1914,在coneqp
%q中。 size [0])
TypeError:'A'必须是具有1000列的'd'矩阵
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
我打印了形状从向量重塑后,它是一个(1,1000)
矩阵。究竟是什么引起此错误?
I printed the shape of A and it's a (1,1000)
matrix after reshaping from a vector. What exactly is causing this error?
推荐答案
您的矩阵元素也必须是浮点类型。因此,可以通过使用 A = A.astype('float')
进行强制转换来消除该错误。
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.
这篇关于CVXOPT QP解算器:TypeError:'A'必须是具有1000列的'd'矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!