Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

confusion matrix python

from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
sns.heatmap(conf_mat, square=True, annot=True, cmap='Blues', fmt='d', cbar=False)
Comment

python plot_confusion_matrix

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(test_Y, predictions_dt)
cm
# after creating the confusion matrix, for better understaning plot the cm.
import seaborn as sn
plt.figure(figsize = (10,8))
# were 'cmap' is used to set the accent colour
sn.heatmap(cm, annot=True, cmap= 'flare',  fmt='d', cbar=True)
plt.xlabel('Predicted_Label')
plt.ylabel('Truth_Label')
plt.title('Confusion Matrix - Decision Tree')
Comment

confusion matrix python

By definition, entry i,j in a confusion matrix is the number of 
observations actually in group i, but predicted to be in group j. 
Scikit-Learn provides a confusion_matrix function:

from sklearn.metrics import confusion_matrix
y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
confusion_matrix(y_actu, y_pred)
# Output
# array([[3, 0, 0],
#        [0, 1, 2],
#        [2, 1, 3]], dtype=int64)
Comment

confusion matrix python code

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_predicted)
cm
# after creating the confusion matrix, for better understaning plot the cm.
import seaborn as sn
plt.figure(figsize = (10,7))
sn.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')
Comment

confusion matrix python

from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report, confusion_matrix

print(confusion_matrix(y_test, y_pred_test.round()))
print(classification_report(y_test, y_pred_test.round()))

# Output:
[[99450   250]
 [ 4165 11192]]
              precision    recall  f1-score   support

           0       0.96      1.00      0.98     99700
           1       0.98      0.73      0.84     15357

    accuracy                           0.96    115057
   macro avg       0.97      0.86      0.91    115057
weighted avg       0.96      0.96      0.96    115057
Comment

how to get confusion matrix in python

from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
Comment

confusion matrix python

df_confusion = pd.crosstab(y_actu, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)
Comment

Create confusion matrix manually using python

import matplotlib.pyplot as plt
import numpy
from sklearn import metrics

confusion_matrix = numpy.array([[  6,  94, 10],[ 80, 821 , 100], [ 80, 821 , 10]])

cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = confusion_matrix, display_labels = ['Sat', 'Sun', 'Mon'])

cm_display.plot()
plt.show()
Comment

compute confusion matrix using python

import numpy as np

currentDataClass = [1, 3, 3, 2, 5, 5, 3, 2, 1, 4, 3, 2, 1, 1, 2]
predictedClass = [1, 2, 3, 4, 2, 3, 3, 2, 1, 2, 3, 1, 5, 1, 1]

def comp_confmat(actual, predicted):

    classes = np.unique(actual) # extract the different classes
    matrix = np.zeros((len(classes), len(classes))) # initialize the confusion matrix with zeros

    for i in range(len(classes)):
        for j in range(len(classes)):

            matrix[i, j] = np.sum((actual == classes[i]) & (predicted == classes[j]))

    return matrix

comp_confmat(currentDataClass, predictedClass)

array([[3., 0., 0., 0., 1.],
       [2., 1., 0., 1., 0.],
       [0., 1., 3., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 1., 1., 0., 0.]])

Comment

PREVIOUS NEXT
Code Example
Python :: python get system information 
Python :: python type hint for a string 
Python :: python df select first x columns 
Python :: python iterar diccionario 
Python :: python read string from file 
Python :: no migrations to apply django 
Python :: binary string to hex python 
Python :: get biggest value in array python3 
Python :: change freq of date index in pandas 
Python :: how to rename columns in python 
Python :: python create list with n elements 
Python :: python find closest value in list 
Python :: auto python to exe 
Python :: python testing machine learning 
Python :: python get response headers 
Python :: how to playsound in python 
Python :: Python __gt__ magic method 
Python :: print a text in python 
Python :: slack send message python 
Python :: colab pip 
Python :: does np.random.randint have a seed 
Python :: wikipedia python 
Python :: change working directory python 
Python :: merge dataframe 
Python :: python extract text from image 
Python :: python rsa 
Python :: how to run django tests 
Python :: python os remove extension 
Python :: change default option optionmenu tkinter 
Python :: how to check nth prime in python 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =