Search code examples
pythonmachine-learningscikit-learnprecision-recall

precision recall pos_label python for one-class


Goal: Obtain precision and recall for one-class(y_true = 1)

Background: I checked http://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_curve.html#sklearn.metrics.precision_recall_curve and it states that pos_label is the label for the positive class, and is set to 1 by default.

Questions:

1) If I only want the precision and recall for my positive class (y_true = 1 in this case) should I keep pos_label = 1 or should I change it to pos_label = 0?

2) Or is there a better way to accomplish my Goal?

Below I am showing code when pos_label = 0

import numpy as np
from sklearn.metrics import precision_recall_fscore_support
y_true = np.array(['0', '1', '1', '0', '1'])
y_pred = np.array(['1', '0', '1', '0', '1'])
out = precision_recall_fscore_support(y_true, y_pred, average='weighted', pos_label = 0) 

Solution

  • import numpy as np
    from sklearn.metrics import precision_recall_fscore_support
    y_true = np.array(['0', '1', '1', '0', '1'])
    y_pred = np.array(['1', '0', '1', '0', '1'])
    
    #keep 1's
    y_true, y_pred = zip(*[[ytrue[i], ypred[i]] for i in range(len(ytrue)) if ytrue[i]=="1"])
    
    out = precision_recall_fscore_support(y_true, y_pred, average='micro')