Skip to content

Instantly share code, notes, and snippets.

@csazevedo
Last active March 29, 2020 22:17
Show Gist options
  • Save csazevedo/96f3ccb947c3694409f7faacf33e7c5c to your computer and use it in GitHub Desktop.
Save csazevedo/96f3ccb947c3694409f7faacf33e7c5c to your computer and use it in GitHub Desktop.
example on computing the confusion matrix using sklearn
import numpy as np
from sklearn.metrics import confusion_matrix
# The true labels we have in the plot orange:positive, blue:negative
true_label = np.array(
[0., 0., 0., 0., 0.,
0., 0., 0., 0., 0.,
1., 1., 1., 1., 1.,
1., 1., 1., 1., 1.])
# The predicted score of some model
prediction = np.array(
[-0.53844931, -1.63710635, -1.05165483, -1.39822259, -0.86465741,
-0.82362094, -0.59935865, -0.70585773, -1.26440723, -1.45471162,
1.46155069, 0.36289365, 0.94834517, 0.60177741, 1.13534259,
1.17637906, 1.40064135, 1.29414227, 0.73559277, 0.54528838])
# The threshold to transform the continuous prediction into binary prediction
threshold = 1
# Transform continuous predictions into binary predictions
prediction[prediction >= threshold] = 1
prediction[prediction < threshold] = 0
print(confusion_matrix(true_label, prediction))
# output:
# [[10 0]
# [ 5 5]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment