I am using sklearn.metrics.confusion_matrix(y_actual, y_predict)
to extract tn, fp, fn, tp and most of the time it works perfectly.
from sklearn.metrics import confusion_matrix
y_actual, y_predict = [1,1,1,1], [0,0,0,0]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [0 0 4 0] # ok
y_actual, y_predict = [1,1,1,1],[0,1,0,1]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [0 0 2 2] # ok
However, in some cases the confusion_matrix() doesn't always return those info and I would get ValueError as shown below.
from sklearn.metrics import confusion_matrix
y_actual, y_predict = [0,0,0,0],[0,0,0,0]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [4] # ValueError: not enough values to unpack (expected 4, got 1)
y_actual, y_predict = [1,1,1,1],[1,1,1,1]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [4] # ValueError: not enough values to unpack (expected 4, got 1)
My temporary solution is to write my own function to extract those info. Is there any way I can force the confusion_matrix()
to always return the tn, fp, fn, tp output?
Thanks
By definition a confusion matrix is such that C i , j is equal to the number of observations known to be in group and predicted to be in group . Thus in binary classification, the count of true negatives is C 0 , 0 , false negatives is C 1 , 0 , true positives is C 1 , 1 and false positives is C 0 , 1 .
In order to create the confusion matrix we need to import metrics from the sklearn module. Once metrics is imported we can use the confusion matrix function on our actual and predicted values. To create a more interpretable visual display we need to convert the table into a confusion matrix display.
This issue has to do with the number of unique labels that are included in your input matrices. In your second block of examples, it is (correctly) building a confusion matrix with just one class, either 0 or 1, respectively.
To force it to output both classes even when one of them is not predicted, use the label
attribute.
y_actual, y_predict = [0,0,0,0],[0,0,0,0]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict, labels=[0,1]).ravel()
>> array([[4, 0],
[0, 0]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With