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
The code which is generating this is:
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)
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.
Any help is greatly appreciated!
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With