Support vector machine with multioutput ValueError: The number of classes has to be greater than one; got 1 class

Viewed 112

I am stuck on an error when trying to run a multioutput classifier that is using a support vector machine.

The test set y_train_svm looks like this:

array([[1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       ...,
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 0, 1]], dtype=int8)

I already checked the number of classes through np.unique(y_train_svm), which returned array([0, 1]). That should mean that there are actually two classes available?

This is the code for the classifier that returns the error message:

svm_clf = SVC(kernel="linear", random_state=42)
multioutput_classifier = MultiOutputClassifier(estimator=svm_clf)
multioutput_classifier.fit(X_train_svm, y_train_svm)

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

1 Answers

Try to cast the values of your train/test-arrays as integer like y_train_svm.astype(np.int8), then run your training-code where you use fit() again.

Related