Skip to content

Instantly share code, notes, and snippets.

@aniruddha27
Created April 17, 2020 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniruddha27/9a50f7c746eec245c92d9af649b4e630 to your computer and use it in GitHub Desktop.
Save aniruddha27/9a50f7c746eec245c92d9af649b4e630 to your computer and use it in GitHub Desktop.
# confusion matrix in sklearn
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
# actual values
actual = ['a','b','c','a','b','c','a','b','c']
# predicted values
predicted = ['a','c','b','b','b','c','a','c','c']
# confusion matrix
matrix = confusion_matrix(actual,predicted, labels=['a','b','c'])
print('Confusion matrix : \n',matrix)
# outcome values order in sklearn
c00, c01, c02, c10, c11, c12, c20, c21, c22 = confusion_matrix(actual,predicted,labels=['a','b','c']).reshape(-1)
# values for class 'a'
tp1 = c00
fp1 = c10 + c20
fn1 = c01 + c02
tn1 = c11 + c12 + c21 + c22
print('Values for class "a" :','\n',tp1,fp1,fn1,tn1)
# classification report for precision, recall f1-score and accuracy
matrix = classification_report(actual,predicted,labels=['a','b','c'])
print('Classification report : \n',matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment