Skip to content

Commit

Permalink
🎨 visualizations and display fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry committed Jul 27, 2023
1 parent b84ece1 commit 4b73dc5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/njab/plotting/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/njab/sklearn/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 11 additions & 4 deletions src/njab/sklearn/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit 4b73dc5

Please sign in to comment.