I used SVM.SVC function to classify. But when I wanted to calculate the weighted and unweighted average accuracy I couldn't access the confusion matrix. Because of svm.SVC.score only provides a classifier accuracy percentage. How can I calculate WAR and UAR?

you can find part of my script below :


    scaler = StandardScaler()
    trainXsc = scaler.transform(trainX)
    testXsc = scaler.transform(testX)

    pca = KernelPCA(n_components=j, kernel="sigmoid", random_state=1)  

    pca.fit(trainXsc)     # fit pca kernel with train data

    trainXtr    = pca.transform(trainXsc) # transform FV with PCA and dimension reduction
    testXtr     = pca.transform(testXsc)

    svmObject   = svm.SVC(C=2.0, kernel='rbf', degree=3, gamma='scale', coef0=0.0, shrinking=True,
                  probability=False, tol=0.001, cache_size=200, class_weight=None,
                  verbose=False, max_iter=-1, decision_function_shape='ovo', random_state=None)
                                        # SVM Kernel Function

    svmObject.fit(trainXtr, trainY)      # train SVM kernel with train FV

    result = svmObject.score(testXtr, testY) 



1 Answer 1


Instead of using the score method on your trained model, you should use the predict method.

You can then pass the results into the confusion matrix function from sklearn:

from sklearn.metrics import confusion_matrix
y_pred = svmObject.predict(X)
cm = confusion_matrix(y_true, y_pred, sample_weight=sample_weight,
                      labels=labels, normalize=normalize)

There is also a nice function called plot_confusion_matrix:

from sklearn.metrics import plot_confusion_matrix

plot_confusion_matrix(svmObject, testXtr, testY)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.