Last active
January 15, 2020 06:30
-
-
Save puraminy/ac032938e318c45c2bfa34b5c6f91cb2 to your computer and use it in GitHub Desktop.
Confusion Matrix Plot (Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import seaborn as sn | |
from numpy import newaxis | |
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, f1_score, average_precision_score, recall_score | |
from sklearn.metrics import precision_recall_fscore_support as score | |
import matplotlib.pyplot as plt | |
from sklearn import preprocessing | |
from sklearn.model_selection import train_test_split | |
def plot_heat_map(ax, X, Y, Z, xlabel, ylabel, format='d', title='Heat Map'): | |
sn.set(font_scale=1.4) # for label size | |
sn.heatmap(Z, annot=True,fmt=format, annot_kws={"size": 12}, cmap="YlGnBu", | |
xticklabels=X, yticklabels=Y, ax=ax) # font size | |
ax.set_title(title) | |
ax.set_xlabel(xlabel) | |
ax.set_ylabel(ylabel) | |
def plot_confusion_matrix(ax, cm, class_names, normalize= False, title='Confusion Matrix'): | |
format = 'd' | |
if normalize: | |
cm = cm.astype('float') / cm.sum(axis=1)[:, newaxis] | |
format = '.2f' | |
plot_heat_map(ax, class_names, class_names, cm, 'Predicted', 'True Classes', format, title) | |
# evalute classifer | |
def eval_conf(classifier, x_train, y_train, x_test, y_test, plot_conf=False, class_names=[], title = "", ax = None, normalized_conf=True, figsize=(10,8)): | |
classifier.fit(x_train, y_train) | |
y_pred = classifier.predict(x_test) | |
acc = accuracy_score(y_test, y_pred) | |
cr = classification_report(y_test, y_pred) | |
# plot confusion matrix | |
if (plot_conf): | |
cm = confusion_matrix(y_test, y_pred) | |
precision,recall,fscore,support=score(y_test,y_pred,average='macro') | |
if ax == None: | |
fig, ax = plt.subplots(figsize=figsize) | |
if title == "": | |
title=f"Confusion matrix" | |
title += f", acc-{acc.round(2)} pr-{precision.round(2)} re-{recall.round(2)} f1-{fscore.round(2)}" | |
plot_confusion_matrix(ax, cm, class_names, normalized_conf, title) | |
return acc, cr | |
def evalsplit_conf(classifier, x_train, y_train, test_size, plot_conf=False, class_names=[], title = "", ax = None, normalized_conf=True, figsize=(10,8)): | |
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=test_size, random_state=42) | |
acc, cr = eval_conf(classifier, x_train, y_train, x_test, y_test, plot_conf, class_names, title, ax, normalized_conf,figsize) | |
return acc, cr | |
def plot_dict(dict, ax=None): | |
if ax == None: | |
fig, ax = plt.subplots(figsize=(10,8)) | |
lists = sorted(dict.items()) # sorted by key, return a list of tuples | |
x, y = zip(*lists) # unpack a list of pairs into two tuples | |
ax.plot(x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment