14

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?

Utumbu
  • 432
  • 3
  • 7
  • 18

4 Answers4

19

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.

divenex
  • 15,176
  • 9
  • 55
  • 55
Haochen Wu
  • 1,753
  • 1
  • 17
  • 24
4

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!

Joice Chan
  • 41
  • 1
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.

alfonso.kim
  • 2,844
  • 4
  • 32
  • 37
Priya
  • 31
  • 1
0

To convert CVXOPT matrix items to floats:

A = A * 1.0
Mohammad Rahmani
  • 364
  • 1
  • 3
  • 11