Emma Lim
Emma Lim

Reputation: 161

Precision and F-score are ill-defined warning while using Python machine learning model

from sklearn import OneClassSVM

check = OneClassSVM(kernel='rbf', gamma='scale')
check.fit(X_train, y_train)
check.predict(X_test)
check.decision_function(X_train)

I am currently using OneClassSVM model. However, I made some changes to this in order to combine with other machine learning models. Originally, OneClassSVM model returns label values of -1 and 1, but I modified them to return 0 and 1. However, although there is no other error, it prints out the Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. 'precision', 'predicted', average, warn_for error.

classification.py

def classification_report(y_true, y_pred, labels=None, target_names=None,
                      sample_weight=None, digits=2, output_dict=False):
y_type, y_true, y_pred = _check_targets(y_true, y_pred)

labels_given = True
if labels is None:
    labels = unique_labels(y_true, y_pred)
    labels_given = False

else:
    labels = np.asarray(labels)

   labels = [0 if i==-1 else i for i in labels]

I don't understand why the error occurs because there is no special error. Personally, I think there might be a problem with labeling. However, I definitely need labels of 0 and 1, not -1 and 1 in the Oneclass model. I also checked that that error could be caused by insufficient data sets, so I doubled the data sets, but still get errors.

result

            precision    recall     f1-score   support
       A     0.0000      0.0000     0.0000     185698
       B     0.0059      0.7211     0.0117      735
accuracy     0.0059      0.0028     0.0038     186433
macro avg    0.0030      0.3605     0.0059     186433
weighted avg 0.0000      0.0028     0.0000     186433

The results I got are as above.

Upvotes: 0

Views: 206

Answers (1)

Antoine Dubuis
Antoine Dubuis

Reputation: 5304

It appears that you have a problem with your labels. There are some labels in y_true, which do not appear in y_pred and hence it is ill-defined.

You should double-check that both your y_true and y_pred use the same labels. From your snippet, it looks like you are modifying the labels array, not the y_pred.

Upvotes: 2

Related Questions