Moved Confusion Matrix function to utils_nlp/eval and addressed PR comments
"""Utilities functions for computing general model evaluation metrics."""
-from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
+from sklearn.metrics import (
+ accuracy_score,
+ precision_score,
+ recall_score,
+ f1_score,
+ confusion_matrix,
+)
from numpy import corrcoef
from matplotlib import pyplot
@@ -49,7 +55,15 @@ def compute_correlation_coefficients(x, y=None):
return pd.DataFrame(corrcoef(x, y))
-def plot_confusion_matrix(y_true, y_pred,labels, normalize=False, title="Confusion matrix",plot_size=(8,5),font_scale=1.1):
+def plot_confusion_matrix(
+ y_true,
+ y_pred,
+ labels,
+ normalize=False,
+ title="Confusion matrix",
+ plot_size=(8, 5),
+ font_scale=1.1,
+):
"""Function that prints out a graphical representation of confusion matrix using Seaborn Heatmap
Args:
@@ -63,10 +77,12 @@ def plot_confusion_matrix(y_true, y_pred,labels, normalize=False, title="Confusi
"""
conf_matrix = np.array(confusion_matrix(y_true, y_pred))
if normalize:
- conf_matrix = np.round(conf_matrix.astype("float") / conf_matrix.sum(axis=1)[:, np.newaxis], 3)
+ conf_matrix = np.round(
+ conf_matrix.astype("float") / conf_matrix.sum(axis=1)[:, np.newaxis], 3
+ )
conf_dataframe = pd.DataFrame(conf_matrix, labels, labels)
fig, ax = pyplot.subplots(figsize=plot_size)
sn.set(font_scale=font_scale)
ax.set_title(title)
- ax=sn.heatmap(conf_dataframe,cmap="Blues", annot=True,annot_kws={"size": 16}, fmt='g')
- ax.set(xlabel='Predicted Labels', ylabel='True Labels')
+ ax = sn.heatmap(conf_dataframe, cmap="Blues", annot=True, annot_kws={"size": 16}, fmt="g")
+ ax.set(xlabel="Predicted Labels", ylabel="True Labels")
