dscience.ml.confusion_matrix

Module Contents

class dscience.ml.confusion_matrix.ConfusionMatrix(data=None, index=None, columns=None, dtype=None, copy=False)

Bases: SimpleFrame

A wrapper around a confusion matrix as a Pandas DataFrame. The rows are the correct labels, and the columns are the predicted labels.

rows
cols
length :int

Get a safe length, verifying that the len(rows) == len(cols).

warn_if_asymmetric(self)
is_symmetric(self)
_repr_html_(self)
sub(self, names: Set[str])
shuffle(self)

Returns a copy with every value mapped to a new location. Destroys the correct links between labels and values. Useful for permutation tests. :return: A copy

diagonals(self)

Returns diagonal elements.

off_diagonals_quantiles(self, q: float = 0.5)
off_diagonals_means(self)
flatten(self)
sum_diagonal(self)
sum_off_diagonal(self)
score_df(self)

Get the diagonal elements as a Pandas DataFrame with columns ‘name’ and ‘score’. :return: A Pandas DataFrame

symmetrize(self)

Averages with its transpose, forcing it to be symmetric. Returns a copy.

triagonalize(self)

NaNs out the upper triangle, returning a copy. You may consider calling symmetrize first. WARNING: Do NOT call a sorting method after this.

log(self)

Takes the log10 of every value in this ConfusionMatrix, returning a new one.

negative(self)
sort(self, **kwargs)

Sorts this confusion matrix to show clustering. The same ordering is applied to the rows and columns. Call this first. Do not call symmetrize(), log(), or triagonalize() before calling this. Returns a copy. :param: kwargs Passed to simulated_annealing: steps, cooling_factor, temp, deterministic :return: A dictionary mapping class names to their new positions (starting at 0) :return: A new ConfusionMatrix, sorted

permutation(self, **kwargs)

Sorts this confusion matrix to show clustering. The same ordering is applied to the rows and columns. Returns the sorting. Does not alter this ConfusionMatrix. :param: kwargs Passed to simulated_annealing: steps, cooling_factor, temp, deterministic :return: A dictionary mapping class names to their new positions (starting at 0)

sort_alphabetical(self)

Sort by the labels alphabetically.

sort_with(self, permutation: Union[Sequence[str], Mapping[str, int]])

Sorts this ConfusionMatrix’s rows and columns by a predefined ordering. Returns a copy. :param permutation: Maps names (strings) to their 0-indexed positions (integers)

If a mapping, takes as-is; these are returned by permutation() If a DataFrame, must have 2 columns ‘key’ (name) and ‘value’ (position), and 1 row per name If a str, tries to read a CSV file at that path into a DataFrame; uses Tools.csv_to_dict
Returns:A new ConfusionMatrix with sorted rows and columns
sort_first(self, first: Sequence[str])

Put these elements first.

__repr__(self)
__str__(self)
classmethod read_csv(cls, path: PathLike, *args, **kwargs)
class dscience.ml.confusion_matrix.ConfusionMatrices
classmethod average(cls, matrices: Sequence[ConfusionMatrix])

Averages a list of confusion matrices. :param matrices: An iterable of ConfusionMatrices (does not need to be a list) :return: A new ConfusionMatrix

classmethod agg_matrices(cls, matrices: Sequence[ConfusionMatrix], aggregation: Callable[[Sequence[pd.DataFrame]], None])

Averages a list of confusion matrices. :param matrices: An iterable of ConfusionMatrices (does not need to be a list) :param aggregation to perform, such as np.mean :return: A new ConfusionMatrix

classmethod zeros(cls, classes: Sequence[str])
classmethod perfect(cls, classes: Sequence[str])
classmethod uniform(cls, classes: Sequence[str])