来自Scikit的隐秘错误消息了解Svm Fit

时间:2013-04-07 16:51:42

标签: python image-recognition scikit-learn handwriting svm

我正在尝试使用these docs训练支持向量机与scikit学习,但我收到一条我不明白的错误信息。我做错了吗?

这是我的脚本。我的想法是我有一个文件,其中每一行都是“标签数据”的形式。数据是一串零和一。

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)   

当我运行它时,会发生这种情况:

$ 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)

输入文件中的一行如下所示:

W 1111111111111100001111111100011111111111100011111110011111000111111111110111111111

这是用于nist特殊数据库19上的字形识别

1 个答案:

答案 0 :(得分:0)

已修复问题在附加中。应该是

training_data.append(vector)

而不是

training_data.append([vector])