Classification Error while using SVC

Uma_Sushmitha_Guntur: <pre><code class="lang-auto">Crosval_num = 5
Accuracy_svc, Accuracy_knn, Accuracy_dt, Accuracy_rf, Accuracy_ada, Accuracy_lda = [], [], [], [], [], []
Recall_svc, Recall_knn, Recall_dt, Recall_rf, Recall_ada, Recall_lda = [], [], [], [], [], []
Precise_svc, Precise_knn, Precise_dt, Precise_rf, Precise_ada, Precise_lda = [], [], [], [], [], []
F1_svc, F1_knn, F1_dt, F1_rf, F1_ada, F1_lda = [], [], [], [], [], []
Xtrainval, ytrainval = np.concatenate((Xtrain,Xtest)), np.concatenate((ytrain, ytest))
Ntrain = Xtrainval.shape[0]
Index_trval = list(range(Xtrainval.shape[0]))
for cv_i in range(Crosval_num):
    Index_val = list(range(cv_i*Ntrain//Crosval_num, (cv_i+1)*Ntrain//Crosval_num))
    Index_train = list(set(Index_trval).difference(set(Index_val)))
    Xtrain, Xtest = Xtrainval[Index_train,:], Xtrainval[Index_val,:]
    ytrain, ytest = ytrainval[Index_train], ytrainval[Index_val]


    from sklearn.svm import SVC
    clf = SVC(kernel='linear', gamma='auto')
    clf.fit(Xtrain, ytrain)
    y_pred = clf.predict(Xtest)
    accu_svc, recall_svc, precise_svc, f1_svc = accuracy_score(ytest, y_pred), \
        recall_score(ytest, y_pred), precision_score(ytest, y_pred), f1_score(ytest, y_pred)
    Accuracy_svc.append(accu_svc)
    Recall_svc.append(recall_svc)
    Precise_svc.append(precise_svc)
    F1_svc.append(f1_svc)    
    
    
    from sklearn.neighbors import KNeighborsClassifier
    clf = KNeighborsClassifier(3)
    clf.fit(Xtrain, ytrain)
    y_pred = clf.predict(Xtest)
    accu_knn, recall_knn, precise_knn, f1_knn = accuracy_score(ytest, y_pred), \
        recall_score(ytest, y_pred), precision_score(ytest, y_pred), f1_score(ytest, y_pred)
    
    Accuracy_knn.append(accu_knn)
    Recall_knn.append(recall_knn)
    Precise_knn.append(precise_knn)
    F1_knn.append(f1_knn)
    
    
    
    from sklearn.tree import DecisionTreeClassifier
    clf =  DecisionTreeClassifier(max_depth=10)
    clf.fit(Xtrain, ytrain)
    y_pred = clf.predict(Xtest)
    accu_dt, recall_dt, precise_dt, f1_dt = accuracy_score(ytest, y_pred), \
        recall_score(ytest, y_pred), precision_score(ytest, y_pred), f1_score(ytest, y_pred)
    
    Accuracy_dt.append(accu_dt)
    Recall_dt.append(recall_dt)
    Precise_dt.append(precise_dt)
    F1_dt.append(f1_dt)
    
        
    from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier    
    clf = RandomForestClassifier(max_depth=10, n_estimators=10, max_features=1)
    clf.fit(Xtrain, ytrain)
    y_pred = clf.predict(Xtest)
    accu_rf, recall_rf, precise_rf, f1_rf = accuracy_score(ytest, y_pred), \
        recall_score(ytest, y_pred), precision_score(ytest, y_pred), f1_score(ytest, y_pred)
    
    Accuracy_rf.append(accu_rf)
    Recall_rf.append(recall_rf)
    Precise_rf.append(precise_rf)
    F1_rf.append(f1_rf)
    
    
    
    clf =  AdaBoostClassifier()
    clf.fit(Xtrain, ytrain)
    y_pred = clf.predict(Xtest)
    accu_ada, recall_ada, precise_ada, f1_ada = accuracy_score(ytest, y_pred), \
        recall_score(ytest, y_pred), precision_score(ytest, y_pred), f1_score(ytest, y_pred)
    
    Accuracy_ada.append(accu_ada)
    Recall_ada.append(recall_ada)
    Precise_ada.append(precise_ada)
    F1_ada.append(f1_ada)
    
    
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
    clf =  LinearDiscriminantAnalysis()
    clf.fit(Xtrain, ytrain)
    y_pred = clf.predict(Xtest)
    accu_lda, recall_lda, precise_lda, f1_lda = accuracy_score(ytest, y_pred), \
        recall_score(ytest, y_pred), precision_score(ytest, y_pred), f1_score(ytest, y_pred)

    
    Accuracy_lda.append(accu_lda)
    Recall_lda.append(recall_lda)
    Precise_lda.append(precise_lda)
    F1_lda.append(f1_lda)


Accu_svc_mean = np.mean(Accuracy_svc)
Recall_svc_mean = np.mean(Recall_svc)
Precise_svc_mean = np.mean(Precise_svc)
F1_svc_mean = np.mean(F1_svc)

Accu_knn_mean = np.mean(Accuracy_knn)
Recall_knn_mean = np.mean(Recall_knn)
Precise_knn_mean = np.mean(Precise_knn)
F1_knn_mean = np.mean(F1_knn)

Accu_dt_mean = np.mean(Accuracy_dt)
Recall_dt_mean = np.mean(Recall_dt)
Precise_dt_mean = np.mean(Precise_dt)
F1_dt_mean = np.mean(F1_dt)

Accu_rf_mean = np.mean(Accuracy_rf)
Recall_rf_mean = np.mean(Recall_rf)
Precise_rf_mean = np.mean(Precise_rf)
F1_rf_mean = np.mean(F1_rf)

Accu_ada_mean = np.mean(Accuracy_ada)
Recall_ada_mean = np.mean(Recall_ada)
Precise_ada_mean = np.mean(Precise_ada)
F1_ada_mean = np.mean(F1_ada)

Accu_lda_mean = np.mean(Accuracy_lda)
Recall_lda_mean = np.mean(Recall_lda)
Precise_lda_mean = np.mean(Precise_lda)
F1_lda_mean = np.mean(F1_lda)

print("Result for SVC is {} \t {} \t {} \t {}".format(Accu_svc_mean, Recall_svc_mean, Precise_svc_mean, F1_svc_mean))
print("Result for KNN is {} \t {} \t {} \t {}".format(Accu_knn_mean, Recall_knn_mean, Precise_knn_mean, F1_knn_mean))
print("Result for DT is {} \t {} \t {} \t {}".format(Accu_dt_mean, Recall_dt_mean, Precise_dt_mean, F1_dt_mean))
print("Result for RF is {} \t {} \t {} \t {}".format(Accu_rf_mean, Recall_rf_mean, Precise_rf_mean, F1_rf_mean))
print("Result for Ada is {} \t {} \t {} \t {}".format(Accu_ada_mean, Recall_ada_mean, Precise_ada_mean, F1_ada_mean))
print("Result for LDA is {} \t {} \t {} \t {}".format(Accu_lda_mean, Recall_lda_mean, Precise_lda_mean, F1_lda_mean))
</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">
ValueError                                Traceback (most recent call last)
Input In [20], in &lt;cell line: 15&gt;()
     22 from sklearn.svm import SVC
     23 clf = SVC(kernel='linear', gamma='auto')
---&gt; 24 clf.fit(Xtrain, ytrain)
     25 y_pred = clf.predict(Xtest)
     26 accu_svc, recall_svc, precise_svc, f1_svc = accuracy_score(ytest, y_pred), \
     27     recall_score(ytest, y_pred), precision_score(ytest, y_pred), f1_score(ytest, y_pred)

File ~/Documents/NSTSC/.env/lib/python3.10/site-packages/sklearn/svm/_base.py:182, in BaseLibSVM.fit(self, X, y, sample_weight)
    172 else:
    173     X, y = self._validate_data(
    174         X,
    175         y,
   (...)
    179         accept_large_sparse=False,
    180     )
--&gt; 182 y = self._validate_targets(y)
    184 sample_weight = np.asarray(
    185     [] if sample_weight is None else sample_weight, dtype=np.float64
    186 )
    187 solver_type = LIBSVM_IMPL.index(self._impl)

File ~/Documents/NSTSC/.env/lib/python3.10/site-packages/sklearn/svm/_base.py:739, in BaseSVC._validate_targets(self, y)
    737 self.class_weight_ = compute_class_weight(self.class_weight, classes=cls, y=y_)
    738 if len(cls) &lt; 2:
--&gt; 739     raise ValueError(
    740         "The number of classes has to be greater than one; got %d class"
    741         % len(cls)
    742     )
    744 self.classes_ = cls
    746 return np.asarray(y, dtype=np.float64, order="C")

ValueError: The number of classes has to be greater than one; got 1 class
</code></pre>

ptrblck: <p>Your use case doesn’t seem to be PyTorch-related, so you might get a better and faster answer in a scikit-learn-specific discussion board. In any case, the error seems to be raised in the <code>fit()</code> operation as your <code>SVC</code> expects to be trained on more than a single class (otherwise it will just predict this single class index and would be useless).</p>