Lukman Budiman
Lukman Budiman

Reputation: 3

ValueError: The number of classes has to be greater than one; got 1 class

SVM = svm.SVC(C=1.0, kernel='linear', degree=3, gamma='auto')
SVM.fit(Train_X_Tfidf,Train_Y)

predictions_SVM = SVM.predict(Test_X_Tfidf)

print("SVM Accuracy Score -> ",accuracy_score(predictions_SVM, Test_Y)*100)

And Error : ValueError Traceback (most recent call last) in () 1 SVM = svm.SVC(C=1.0, kernel='linear', degree=3, gamma='auto') ----> 2 SVM.fit(Train_X_Tfidf,Train_Y) 3 # predict the labels on validation dataset 4 predictions_SVM = SVM.predict(Test_X_Tfidf) 5 # Use accuracy_score function to get the accuracy

1 frames /usr/local/lib/python3.6/dist-packages/sklearn/svm/_base.py in _validate_targets(self, y) 529 raise ValueError( 530 "The number of classes has to be greater than one; got %d" --> 531 " class" % len(cls)) 532 533 self.classes_ = cls

ValueError: The number of classes has to be greater than one; got 1 class

How to solve that?

Upvotes: 0

Views: 2006

Answers (1)

abc
abc

Reputation: 11929

This means that you only have one class in your training data. This is what internally the code does in the fit method

self.classes_ = np.unique(y)

and it then validates it by checking if there are less than 2 classes.
You can confirm it by doing np.unique(Train_Y)

Upvotes: 0

Related Questions