0

I am doing a classification task in Python to classify audio files of different musical instrument into their respective class, in my case there are 4 class, which are Brass, String, Percussion, and Woodwind. I used 3 different algorithms, k-Nearest Neighbor, Naive Bayes, and SVM. My code looks a bit like this:

#X is feature matrix, y is class vector
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

#k-NN Classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train,y_train)
knn_pred = knn.predict(X_test)
print(metrics.classification_report(y_test,knn_pred)

#Naive Bayes Classifier
nb = GaussianNB()
nb.fit(X_train,y_train)
nb_pred = nb.predict(X_test)
print(metrics.classification_report(y_test,nb_pred)

#SVM Classifier
svm = SVC()
svm.fit(X_train,y_train)
svm_pred = knn.predict(X_test)
print(metrics.classification_report(y_test,svm_pred)

When I try to run this code, I got problem with my SVM Classifier. The error code looks like this:

      precision    recall  f1-score   support

  Brass       1.00      0.21      0.34        72
Percussion    0.38      1.00      0.55       279
 String       1.00      0.15      0.26       276
 Woodwind     0.00      0.00      0.00       156

 avg / total  0.58      0.43      0.32       783

C:\Users\Anaconda3\lib\site-packages\sklearn\metrics\classification.py:1135: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.

When I checked my predicted labels from SVM classifier(svm_pred), there are no Woodwind class predicted which results in the value of Precision and F1-Score for Woodwind class being 0.0. This problem only occurs for SVM classifier although I used the same training set for all 3 algorithms. Anyone know how to get around this problem?

Note: My dataset for all 4 classes are imbalanced which I suspect might triggered this error

Akhmad Zaki
  • 419
  • 7
  • 23
  • 2
    Possible Duplicate of [UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples](https://stackoverflow.com/questions/43162506/undefinedmetricwarning-f-score-is-ill-defined-and-being-set-to-0-0-in-labels-wi) – Flika205 Mar 03 '18 at 22:44

1 Answers1

0

i think that i know what is your problem. Probably it's because the SVM classifier do not classiffy any instance for Woodwind class correctly . Then, when he tries to calculate the precision of the Woodwind class he get 0, because to calculate the precision he do something like that:

true positives divided by (true positives+false positives).

But the value of the true positives is equals to 0, then he cannot calculate 0 divided by something. So that's why the value of the precision score is 0. The same thing happens with the recall and F1.

Remember that true positives is the number of instances for the Woodwind class that are correctly classified by the Classifier.