使用scikit进行SVM训练时输入形状错误

m_a*_*ber 3 python machine-learning svm scikit-learn

我对scikit和ML有点新鲜感.我试图训练一个SVM分类器用于一个与所有分类.我使用以下代码.

g=list()
for i in range(0,120):
    g.append(1)
for i in range(120,240):
    g.append(2)

u=set(g)
numclasses=len(u)

lin_clf = svm.LinearSVC()
lin_clf.fit(features,u)
Run Code Online (Sandbox Code Playgroud)

功能是72900*120阵列.我从不同的python代码获取功能并在此处调用它.它会抛出以下警告和错误.

/usr/lib/python2.7/dist-packages/scipy/misc/pilutil.py:279: 
DeprecationWarning: fromstring() is deprecated. Please call frombytes() instead.
image = Image.fromstring(mode, shape, strdata)
Run Code Online (Sandbox Code Playgroud)

错误

ValueError: bad input shape ()
Run Code Online (Sandbox Code Playgroud)

如果您需要特征提取代码,请发表评论.先感谢您.

YS-*_*S-L 9

哪行代码抛出错误?是lin_clf.fit(features,u)吗?

根据LinearSVC 的文档,争论的依据fit(X,y)

X:{array-like,sparse matrix},shape = [n_samples,n_features]

训练向量,其中n_samples的样本数和n_features是特征的数量.

y:类似数组,shape = [n_samples]

相对于X的目标向量

但是,u你的代码中是一个python set.它应该是一个长度为72900的numpy数组.