Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

accuracy for each class

from sklearn.metrics import confusion_matrix
import numpy as np

y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']

#Get the confusion matrix
cm = confusion_matrix(y_true, y_pred)
#array([[1, 0, 0],
#   [1, 0, 0],
#   [0, 1, 2]])

#Now the normalize the diagonal entries
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
#array([[1.        , 0.        , 0.        ],
#      [1.        , 0.        , 0.        ],
#      [0.        , 0.33333333, 0.66666667]])

#The diagonal entries are the accuracies of each class
cm.diagonal()
#array([1.        , 0.        , 0.66666667])
Comment

accuracy for each class


from sklearn.metrics import confusion_matrix
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]
matrix = confusion_matrix(y_true, y_pred)
matrix.diagonal()/matrix.sum(axis=1)

Comment

PREVIOUS NEXT
Code Example
Python :: pandas get attribute of object 
Python :: plt.semilogx 
Python :: python type hints list of class 
Python :: correlation meaning 
Python :: open file in python 
Python :: how to add legend on side of the chart python 
Python :: for in print 
Python :: python list pop 
Python :: pydrive download by url 
Python :: use get method request html page python 
Python :: np array size 
Python :: are tuples in python mutable 
Python :: using hashlib module in python 
Python :: Lucky four codechef solution 
Python :: ssl socket python 
Python :: flask windows auto reload 
Python :: image data generator keras with tf.data.Data.from_generator 
Python :: python flatten a list of lists 
Python :: Changing default fonts in matploitlibrc file 
Python :: python variables 
Python :: semicolon python 
Python :: for in print pyhton 
Python :: how to run python in the browser 
Python :: reload class module python 
Python :: export postgres database to heroku 
Python :: inheritance in python 3 example 
Python :: parallel iteration python 
Python :: python inline if 
Python :: python list intersection 
Python :: keep the user logged in even though user changes password django 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =