1

When I use Opencv3 with Python2, my code is to do something with SVM.

But an Error is shown:

svm.train(trainData,responses,params = svm_params) TypeError: only length-1 arrays can be converted to Python scalars

Tes3awy
  • 2,166
  • 5
  • 29
  • 51
L. Wang
  • 11
  • 1

1 Answers1

0

This error occurred because the function was expecting a single array object and trainData variable contained multiple array objects. There are several ways to solve this, one of them is, say if your input object is:

# Used for creating training samples for a logic gate (eg: xor) NN
trainData = np.random.randint(2,size=2)
# array([ ..some values.. ])

then add [np.newaxis] to it

np.random.randint(2,size=2)[np.newaxis]
# array([[ ..some values.. ]])

See: numpy newaxis, numpy broadcasting

Community
  • 1
  • 1
Abhijay Ghildyal
  • 4,044
  • 6
  • 33
  • 54
  • Tanks for your reply. But my trainData is got like this: deskewed = [map(deskew,row) for row in train_cells] hogdata = [map(hog,row) for row in deskewed] trainData = np.float32(hogdata).reshape(-1,64) responses = np.float32(np.repeat(np.arange(10),250)[:,np.newaxis]) The problem is still exit. Can you help me solve this problem? – L. Wang Aug 15 '16 at 01:41
  • Here is another answer which may be helpful: http://stackoverflow.com/questions/36110924/how-can-i-format-my-list-to-give-it-as-input-to-svm-train-in-opencv3-0 – Abhijay Ghildyal Aug 15 '16 at 20:24