CVXOPT QP Solver: TypeError: 'A' must be a 'd' matrix with 1000 columns

限于喜欢 提交于 2019-12-01 16:14:27

问题


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?


回答1:


Your matrix elements have to be of type double as well. So just using A=A.astype('double') to cast it.




回答2:


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!




回答3:


The error - "TypeError: 'A' must be a 'd' matrix with 1000 columns:" has two condition namely:

  1. if the type code is not equal to 'd'
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!