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 plot confusion matrix

import seaborn as sns
from sklearn.metrics import confusion_matrix
# y_test  : actual labels or target
# y_preds : predicted labels or target
sns.heatmap(confusion_matrix(y_test, y_preds),annot=True);
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 :: django add middleware 
Python :: if else one line python 
Python :: python pyqt5 
Python :: python if not null or empty 
Python :: suppress python 
Python :: python create a pinging sound 
Python :: python dict get random key 
Python :: How to efficiently calculate the nth Catalan number, in Python? 
Python :: tuple plot python 
Python :: python get architecture 
Python :: python generator 
Python :: pytube sample script 
Python :: python pandas column where 
Python :: fasttext python 
Python :: numpy randint 
Python :: python list for all months including leap years 
Python :: delete a column in pandas 
Python :: matplotlib animate 
Python :: add column to start of dataframe pandas 
Python :: minmaxscaler python 
Python :: download from colab to local drive 
Python :: import python script from another directory 
Python :: how to convert .ui file to .py 
Python :: pandas count nans in column 
Python :: draw circle pygame 
Python :: django environment variables 
Python :: primary key auto increment python django 
Python :: print in python 
Python :: Returns the first n rows 
Python :: python message from teams 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =