def _mostrar_resultados(y_test, pred_y): conf_matrix = _confusion_matrix(y_test, pred_y) _plt.figure(figsize=(10, 10)) _sns.heatmap(conf_matrix, xticklabels=LABELS, yticklabels=LABELS, annot=True, fmt="d") _plt.title("Confusion matrix") _plt.ylabel('True class') _plt.xlabel('Predicted class') print(_classification_report(y_test, pred_y))
def _getConfusionMatrix(self, ): """ Calculates confusion matrix from predictions and truth labels. Parameters ========== None. Returns ======= numpy 2-d array. """ csv_buffer = StringIO() labels = set(self.truths) | set(self.preds) cm = _confusion_matrix(self.truths, self.preds, labels=list(labels)) pd.DataFrame(cm, columns=labels, index=labels).to_csv(csv_buffer) return csv_buffer.getvalue()
def confusion_matrix( orig: Union[pd.Series, np.ndarray, Sequence], new: Union[pd.Series, np.ndarray, Sequence], data: Optional[pd.DataFrame] = None, *, normalize: bool = True, ) -> pd.DataFrame: """\ Given an original and new set of labels, create a labelled confusion matrix. Parameters `orig` and `new` can either be entries in data or categorical arrays of the same size. Params ------ orig Original labels. new New labels. data Optional dataframe to fill entries from. normalize Should the confusion matrix be normalized? Examples -------- .. plot:: import scanpy as sc; import seaborn as sns pbmc = sc.datasets.pbmc68k_reduced() cmtx = sc.metrics.confusion_matrix("bulk_labels", "louvain", pbmc.obs) sns.heatmap(cmtx) """ from sklearn.metrics import confusion_matrix as _confusion_matrix if data is not None: if isinstance(orig, str): orig = data[orig] if isinstance(new, str): new = data[new] # Coercing so I don't have to deal with it later orig, new = pd.Series(orig), pd.Series(new) assert len(orig) == len(new) unique_labels = pd.unique(np.concatenate((orig.values, new.values))) # Compute mtx = _confusion_matrix(orig, new, labels=unique_labels) if normalize: sums = mtx.sum(axis=1)[:, np.newaxis] mtx = np.divide(mtx, sums, where=sums != 0) # Label orig_name = "Original labels" if orig.name is None else orig.name new_name = "New Labels" if new.name is None else new.name df = pd.DataFrame( mtx, index=pd.Index(unique_labels, name=orig_name), columns=pd.Index(unique_labels, name=new_name), ) # Filter if is_categorical(orig): orig_idx = pd.Series(orig).cat.categories else: orig_idx = natsorted(pd.unique(orig)) if is_categorical(new): new_idx = pd.Series(new).cat.categories else: new_idx = natsorted(pd.unique(new)) df = df.loc[np.array(orig_idx), np.array(new_idx)] return df
def confusion_matrix(y, y_pred): cm = _confusion_matrix(y, y_pred) cm = (cm.T / cm.sum(axis=1)).T return cm
def confusion_matrix(Y_true, Y_pred, labels=None): if labels is None: labels = np.unique(np.append(Y_true, Y_pred)) return pd.DataFrame(_confusion_matrix(Y_true, Y_pred, labels=labels), index=[f'actual_{label}' for label in labels], columns=[f'predicted_{label}' for label in labels])