Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sklearn plot confusion matrix

import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, plot_confusion_matrix

clf = # define your classifier (Decision Tree, Random Forest etc.)
clf.fit(X, y) # fit your classifier

# make predictions with your classifier
y_pred = clf.predict(X)         
# optional: get true negative (tn), false positive (fp)
# false negative (fn) and true positive (tp) from confusion matrix
M = confusion_matrix(y, y_pred)
tn, fp, fn, tp = M.ravel() 
# plotting the confusion matrix
plot_confusion_matrix(clf, X, y)
plt.show()
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

import sklearn.metrics from plot_confusion_matrix

from sklearn.metrics import plot_confusion_matrix
Comment

sklearn plot confusion matrix

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import  plot_confusion_matrix
clf = LogisticRegression()
clf.fit(X_train,y_train)
disp = plot_confusion_matrix(clf,X_test,y_test,cmap="Blues",values_format='.3g')
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
Comment

python confusion matrix without sklearn

import numpy as np

def compute_confusion_matrix(true, pred):
  '''Computes a confusion matrix using numpy for two np.arrays
  true and pred.

  Results are identical (and similar in computation time) to: 
    "from sklearn.metrics import confusion_matrix"

  However, this function avoids the dependency on sklearn.'''

  K = len(np.unique(true)) # Number of classes 
  result = np.zeros((K, K))

  for i in range(len(true)):
    result[true[i]][pred[i]] += 1

  return result
Comment

plot confusion matrix scikit learn

from sklearn import metrics
metrics.ConfusionMatrixDisplay.from_predictions(true_y, predicted_y).plot()
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

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 with labels sklearn

import pandas as pd
y_true = pd.Series([2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2])
y_pred = pd.Series([0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2])

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

confusion matrix with labels sklearn

Predicted  0  1  2  All
True                   
0          3  0  0    3
1          0  1  2    3
2          2  1  3    6
All        5  2  5   12
Comment

PREVIOUS NEXT
Code Example
Python :: tensorflow mnist dataset import 
Python :: python distance between coordinates 
Python :: setwd python 
Python :: convert pdf to base64 python 
Python :: dataframe from two series 
Python :: tk table python 
Python :: keyerror dislike_count pafy 
Python :: pygame draw line 
Python :: tkinter change label text color 
Python :: how to install python3 on ubuntu 
Python :: python count words in file 
Python :: run celery on windows 
Python :: python code to convert all keys of dict into lowercase 
Python :: How to print list without for loop python 
Python :: how to generate a random number python 
Python :: pyyaml install 
Python :: pandas columns starting with 
Python :: django created at field 
Python :: cannot remove column in pandas 
Python :: Install requests-html library in python 
Python :: print upto 1 decimal place python 
Python :: python split range equally 
Python :: python utf 8 encoding 
Python :: bmi python 
Python :: convert text file into list 
Python :: filter with different operator in django 
Python :: xarray add coordinate 
Python :: edit json file python 
Python :: how to get all links text from a website python beautifulsoup 
Python :: converting string array to int array python 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =