0
votes

I am attempting to train a support vector machine with scikit learn using these docs but I am getting an error message I do not understand. Am I doing something wrong?

Here is my script. The idea is I have a file where each line is of the form "label data". The data is a string of zeros and ones.

svm-learn.py

import os
import re
from sklearn import svm

classifier = svm.SVC()

data = open("sd19-train-binary.txt", "r")

labels = []
training_data = [] 
i = 0

for line in data:
    match = re.search("^(\S+) (\d+)", line)
    label = match.group(1)
    vector = list(match.group(2))
    vector = [int(x) for x in vector]
    labels.append(label)
    training_data.append([vector])
    i += 1
    if i > 100:
        break

classifier.fit(training_data, labels)   

When I run it, this happens:

$ python svm-learn.py
  Traceback (most recent call last):
    File "svm-learn.py", line 26, in <module>
      classifier.fit(training_data, labels)
    File "/Library/Python/2.7/site-packages/scikit_learn-0.14_git-py2.7-macosx-10.8-intel.egg/sklearn/svm/base.py", line 184, in fit
      fit(X, y, sample_weight, solver_type, kernel)
    File "/Library/Python/2.7/site-packages/scikit_learn-0.14_git-py2.7-macosx-10.8-intel.egg/sklearn/svm/base.py", line 228, in _dense_fit
      max_iter=self.max_iter)
    File "libsvm.pyx", line 53, in sklearn.svm.libsvm.fit (sklearn/svm/libsvm.c:1660)
  ValueError: Buffer has wrong number of dimensions (expected 2, got 3)

A single line in my input file looks like this:

W 1111111111111100001111111100011111111111100011111110011111000111111111110111111111

This is for glyph recognition on the nist special database 19

1

1 Answers

0
votes

Fixed, problem was in the append. Should be

training_data.append(vector)

instead of

training_data.append([vector])