sklearn LinearSVC-每个样本X具有1个功能;期待5 [英] sklearn LinearSVC - X has 1 features per sample; expecting 5

查看:71
本文介绍了sklearn LinearSVC-每个样本X具有1个功能;期待5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试预测测试数组的类,但是出现以下错误以及堆栈跟踪:

I'm trying to predict the class of a test array, but I'm getting the below error, along with the stack trace:

Traceback (most recent call last):
  File "/home/radu/PycharmProjects/Recommender/Temporary/classify_dict_test.py", line 24, in <module>
    print classifier.predict(test)
  File "/home/radu/.local/lib/python2.7/site-packages/sklearn/linear_model/base.py", line 215, in predict
    scores = self.decision_function(X)
  File "/home/radu/.local/lib/python2.7/site-packages/sklearn/linear_model/base.py", line 196, in decision_function
    % (X.shape[1], n_features))
ValueError: X has 1 features per sample; expecting 5

生成此代码的代码是:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC

corpus = [
    "I am super good with Java and JEE",
    "I am super good with .NET and C#",
    "I am really good with Python and R",
    "I am really good with C++ and pointers"
    ]

classes = ["java developer", ".net developer", "data scientist", "C++ developer"]

test = ["I think I'm a good developer with really good understanding of .NET"]

tvect = TfidfVectorizer(min_df=1, max_df=1)

X = tvect.fit_transform(corpus)

classifier = LinearSVC()
classifier.fit(X, classes)

print classifier.predict(test)

我尝试查看 LinearSVC文档,以获取有关可能引发此错误的准则或提示,但我无法弄清楚.

I've tried looking into the LinearSVC documentation for guidelines or hints as to what might throw this error, but I can't figure it out.

任何帮助将不胜感激!

推荐答案

变量test是一个字符串-SVC需要具有与X相同维数的特征向量.您必须将测试字符串转换为特征向量在将其馈送到SVC之前,使用相同的矢量化器实例:

The variable test is a string - the SVC needs a feature vector with the same number of dimensions as X. You have to transform the test string to a feature vector using the same vectorizer instance, before you feed it to the SVC:

X_test=tvect.transform(test)
classifier.predict(X_test)

这篇关于sklearn LinearSVC-每个样本X具有1个功能;期待5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆