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

plotting confusion matrix

from sklearn.metrics import confusion_matrix
matrix_confusion = confusion_matrix(y_test, y_pred)
sns.heatmap(matrix_confusion, square=True, annot=True, cmap='Blues', fmt='d', cbar=False
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 :: neat python full form 
Python :: how to cycle through panes in tmux 
Python :: install pyaudio linux 
Python :: browser pop up yes no selenium python 
Python :: Jupyter notebook: let a user inputs a drawing 
Python :: python close input timeout 
Python :: list to tensor 
Python :: error 401 unauthorized "Authentication credentials were not provided." 
Python :: replacing values in pandas dataframe 
Python :: How to convert a string to a dataframe in Python 
Python :: python pip fix 
Python :: how to get the index of a value in pandas dataframe 
Python :: python console command 
Python :: schedule task to midnight python 
Python :: pandas diff between dates 
Python :: combining list of list to single list python 
Python :: installing fastapi 
Python :: matplotlib ticksize 
Python :: pandas query variable count 
Python :: Change the year in 2nd line to get the answer for the year you want. Ex: year=2010 
Python :: how to get current page url in django template 
Python :: tkinter bold text 
Python :: django datetimefield default 
Python :: undo cell delete kaggle 
Python :: how to create a custom callback function in keras while training the model 
Python :: unpack dictionaryp 
Python :: calcolatrice 
Python :: plt axis tick color 
Python :: convert 2d list to 1d python 
Python :: pygame doesnt dedect collision between sprite and image 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =