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

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

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 :: how to login using email in django 
Python :: how to use %s python 
Python :: python 2.7 venv 
Python :: Python3 seconds to datetime 
Python :: python backslash in string 
Python :: gridsearch cv 
Python :: formate a phonenumber in phonenumber package with phonenumberformat 
Python :: Jinja for items in list 
Python :: check if an object has an attribute in Python 
Python :: ValueError: cannot convert float NaN to integer 
Python :: python datetime minus datetime 
Python :: how to split string by list of indexes python 
Python :: Check if file already existing 
Python :: NumPy unique Example Get unique values from a 1D Numpy array 
Python :: appending to a list python 
Python :: re.match python 
Python :: scree plot sklearn 
Python :: python loop backwards 
Python :: sudoku solver py 
Python :: pytorch cuda tensor in module 
Python :: module.__dict__ python 
Python :: class indexing 
Python :: download unsplash images code 
Python :: initialize np array 
Python :: python garbaze collection 
Python :: re python3 
Python :: how to count number of records in json 
Python :: copy dataframe columns names 
Python :: python get first occurrence in list 
Python :: How to Add Elements To a Set using add() method in python 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =