diff --git a/src/njab/plotting/metrics.py b/src/njab/plotting/metrics.py index 09ec187..6c510b1 100644 --- a/src/njab/plotting/metrics.py +++ b/src/njab/plotting/metrics.py @@ -4,6 +4,7 @@ from njab.sklearn.types import ResultsSplit, Results +LIMITS = (-0.05, 1.05) def plot_split_auc(result: ResultsSplit, name: str, ax: matplotlib.axes.Axes) -> matplotlib.axes.Axes: @@ -15,6 +16,8 @@ def plot_split_auc(result: ResultsSplit, name: str, xlabel='false positive rate', ylabel='true positive rate', style='.-', + ylim=LIMITS, + xlim=LIMITS, ax=ax) return ax @@ -42,6 +45,8 @@ def plot_split_prc(result: ResultsSplit, name: str, xlabel='true positive rate', ylabel='precision', style='.-', + ylim=LIMITS, + xlim=LIMITS, ax=ax) return ax diff --git a/src/njab/sklearn/pca.py b/src/njab/sklearn/pca.py index c107840..4276625 100644 --- a/src/njab/sklearn/pca.py +++ b/src/njab/sklearn/pca.py @@ -39,6 +39,7 @@ def run_pca( def plot_explained_variance(pca: sklearn.decomposition.PCA, ax=None): exp_var = pd.Series( pca.explained_variance_ratio_).to_frame('explained variance') + exp_var.index += 1 # start at 1 exp_var["explained variance (cummulated)"] = exp_var[ 'explained variance'].cumsum() exp_var.index.name = 'PC' diff --git a/src/njab/sklearn/scoring.py b/src/njab/sklearn/scoring.py index cdf735b..44951cb 100644 --- a/src/njab/sklearn/scoring.py +++ b/src/njab/sklearn/scoring.py @@ -8,17 +8,24 @@ class ConfusionMatrix(): def __init__(self, y_true, y_pred): self.cm_ = sklm.confusion_matrix(y_true, y_pred) - @property - def as_dataframe(self): + def as_dataframe(self, names=('true', 'pred')) -> pd.DataFrame: """Create pandas.DataFrame and return. Names rows and columns.""" if not hasattr(self, 'df'): + true_name, pred_name = names self.df = pd.DataFrame(self.cm_) - self.df.index.name = 'true' - self.df.columns = pd.MultiIndex.from_product([['pred'], + self.df.index.name = true_name + self.df.columns = pd.MultiIndex.from_product([[pred_name], self.df.columns]) return self.df + def classification_label(self) -> dict: + tn, fp, fn, tp = self.cm_.ravel() + return {'TN': tn, 'FP': fp, 'FN': fn, 'TP': tp} + + def as_classification_series(self) -> pd.Series: + return pd.Series(self.classification_label()) + @property def as_array(self): """Return sklearn.metrics.confusion_matrix array"""