Classification metrics

This module includes following functions:

These functions represent metrics and numeric summaries relevant for classification problems.

scripts.metrics._classification.accuracy_score(y_true, y_pred, normalised=True)

Number of correctly classified records.

Parameters
  • y_true (iterable object) – Ground values.

  • y_pred (iterale object) – Predicted values.

  • normalised (bool, optional) – Normalize the metric by the total number of records.

Returns

Number of incorrectly classified records.

Return type

float or int

Raises

ValueError – If the inputted vectors are not of the same length.

scripts.metrics._classification.classification_error(y_true, y_pred, normalised=True)

Number of incorrectly classified records.

Parameters
  • y_true (iterable object) – Ground values.

  • y_pred (iterale object) – Predicted values.

  • normalised (bool, optional) – Normalize the metric by the total number of records.

Returns

Number of incorrectly classified records.

Return type

float or int

Raises

ValueError – If the inputted vectors are not of the same length.

scripts.metrics._classification.classification_report(y_true, y_pred)

Returns classification report which includes following info:

Parameters
  • y_true (iterable object) – Ground values.

  • y_pred (iterale object) – Predicted values.

Returns

report – Report as described above.

Return type

pandas.DataFrame

scripts.metrics._classification.confusion_matrix(y_true, y_pred, normalised=True, as_frame=False)

N x N matrix where N is number of distinct classes. Rows in this matrix represent actual values and columns the predicted ones. Therefore, entry \(a_{i, j}\) represents a number of records whose actual class is i and their predicted class is j. Thus, perfect model would have all entries within the matrix on its diagonal.

Parameters
  • y_true (iterable object) – Ground values.

  • y_pred (iterale object) – Predicted values.

  • normalised (bool, optional) – Normalize the metric by the total number of records.

  • as_frame (bool, optional) – If you want to return it as a pandas dataframe.

Returns

confusion_matrix – Confusion matrix as described above.

Return type

numpy.ndarray or pandas.DataFrame

Raises

ValueError – If the inputted vectors are not of the same length.

scripts.metrics._classification.f1_score(y_true, y_pred, average=None)

Harmonic mean of a precision and recall.

Parameters
  • y_true (iterable object) – Ground values.

  • y_pred (iterale object) – Predicted values.

  • average (str, optional) – Aggregation metric for the f1 scores of the respective classes.

Returns

f1_scores or single score – Numpy array where ith entry represents f1 score for i-th class or single score if average is specified.

Return type

1d array or float

scripts.metrics._classification.precision_score(y_true, y_pred, average=None)

For each class j, return the ratio of the number of correct classificatin of j over number of times j was predicted in total.

Parameters
  • y_true (iterable object) – Ground values.

  • y_pred (iterale object) – Predicted values.

  • average (str, optional) – Aggregation metric for the precision scores of the respective classes.

Returns

precision_scores or single score – Numpy array where ith entry represents precision score for i-th class or single score if average is specified.

Return type

numpy.ndarray or float

scripts.metrics._classification.recall_score(y_true, y_pred, average=None)

For each class j, return the ratio of the number of correct classificatin of j over number of actual instances of j.

Parameters
  • y_true (iterable object) – Ground values.

  • y_pred (iterale object) – Predicted values.

  • average (str, optional) – Aggregation metric for the recall scores of the respective classes.

Returns

recall_scores or single score – Numpy array where ith entry represents recall score for i-th class or single score if average is specified.

Return type

numpy.ndarray or float