我正在尝试使用这些文档与scikit学习训练支持向量机,但我得到一个我不理解的错误消息。我做错了什么吗?
这是我的脚本。这个想法是我有一个文件,其中每行都是"标签数据"的形式。数据是由0和1组成的字符串。
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上的字形识别
修复,问题是在追加。应该是
training_data.append(vector)
代替
training_data.append([vector])