Skip to content

arkas.metric

arkas.metric

Contain functions to compute metrics.

arkas.metric.accuracy

accuracy(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the accuracy metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import accuracy
>>> accuracy(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'accuracy': 1.0, 'count_correct': 5, 'count_incorrect': 0, 'count': 5, 'error': 0.0}

arkas.metric.average_precision

average_precision(
    y_true: ndarray,
    y_score: ndarray,
    *,
    label_type: str = "auto",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the average precision metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
label_type str

The type of labels used to evaluate the metrics. The valid values are: 'binary', 'multiclass', and 'multilabel'. If 'binary' or 'multilabel', y_true values must be 0 and 1.

'auto'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import average_precision
>>> # auto
>>> metrics = average_precision(
...     y_true=np.array([1, 0, 0, 1, 1]), y_score=np.array([2, -1, 0, 3, 1])
... )
>>> metrics
{'average_precision': 1.0, 'count': 5}
>>> # binary
>>> metrics = average_precision(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_score=np.array([2, -1, 0, 3, 1]),
...     label_type="binary",
... )
>>> metrics
{'average_precision': 1.0, 'count': 5}
>>> # multiclass
>>> metrics = average_precision(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_score=np.array(
...         [
...             [0.7, 0.2, 0.1],
...             [0.4, 0.3, 0.3],
...             [0.1, 0.8, 0.1],
...             [0.2, 0.3, 0.5],
...             [0.4, 0.4, 0.2],
...             [0.1, 0.2, 0.7],
...         ]
...     ),
...     label_type="multiclass",
... )
>>> metrics
{'average_precision': array([0.833..., 0.75 , 0.75 ]),
 'count': 6,
 'macro_average_precision': 0.777...,
 'micro_average_precision': 0.75,
 'weighted_average_precision': 0.777...}
>>> # multilabel
>>> metrics = average_precision(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_score=np.array([[2, -1, -1], [-1, 1, 2], [0, 2, 3], [3, -2, -4], [1, -3, -5]]),
...     label_type="multilabel",
... )
>>> metrics
{'average_precision': array([1. , 1. , 0.477...]),
 'count': 5,
 'macro_average_precision': 0.825...,
 'micro_average_precision': 0.588...,
 'weighted_average_precision': 0.804...}

arkas.metric.balanced_accuracy

balanced_accuracy(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the accuracy metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import balanced_accuracy
>>> balanced_accuracy(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'balanced_accuracy': 1.0, 'count': 5}

arkas.metric.binary_average_precision

binary_average_precision(
    y_true: ndarray,
    y_score: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the average precision metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, *).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples, *).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import binary_average_precision
>>> metrics = binary_average_precision(
...     y_true=np.array([1, 0, 0, 1, 1]), y_score=np.array([2, -1, 0, 3, 1])
... )
>>> metrics
{'average_precision': 1.0, 'count': 5}

arkas.metric.binary_confusion_matrix

binary_confusion_matrix(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the confusion matrix metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import binary_confusion_matrix
>>> binary_confusion_matrix(
...     y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1])
... )
{'confusion_matrix': array([[2, 0], [0, 3]]),
 'count': 5,
 'false_negative_rate': 0.0,
 'false_negative': 0,
 'false_positive_rate': 0.0,
 'false_positive': 0,
 'true_negative_rate': 1.0,
 'true_negative': 2,
 'true_positive_rate': 1.0,
 'true_positive': 3}

arkas.metric.binary_fbeta_score

binary_fbeta_score(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    betas: Sequence[float] = (1,),
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the F-beta metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
betas Sequence[float]

The betas used to compute the F-beta scores.

(1,)
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import binary_fbeta_score
>>> binary_fbeta_score(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_pred=np.array([1, 0, 0, 1, 1]),
... )
{'count': 5, 'f1': 1.0}

arkas.metric.binary_jaccard

binary_jaccard(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the Jaccard metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import binary_jaccard
>>> binary_jaccard(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'count': 5, 'jaccard': 1.0}

arkas.metric.binary_precision

binary_precision(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the precision metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import binary_precision
>>> binary_precision(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'count': 5, 'precision': 1.0}

arkas.metric.binary_recall

binary_recall(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the recall metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import binary_recall
>>> binary_recall(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'count': 5, 'recall': 1.0}

arkas.metric.binary_roc_auc

binary_roc_auc(
    y_true: ndarray,
    y_score: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the Area Under the Receiver Operating Characteristic Curve (ROC AUC) metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples,).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

arkas.metric.binary_top_k_accuracy

binary_top_k_accuracy(
    y_true: ndarray,
    y_score: ndarray,
    *,
    k: Sequence[int] = (2,),
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the Area Under the Top-k Accuracy classification metrics for binary labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples,).

required
k Sequence[int]

The numbers of most likely outcomes considered to find the correct label.

(2,)
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

arkas.metric.confusion_matrix

confusion_matrix(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    label_type: str = "auto",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the confusion matrix metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
label_type str

The type of labels used to evaluate the metrics. The valid values are: 'binary', 'multiclass', and 'multilabel'. If 'binary' or 'multilabel', y_true values must be 0 and 1.

'auto'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import confusion_matrix
>>> # binary
>>> confusion_matrix(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_pred=np.array([1, 0, 0, 1, 1]),
...     label_type="binary",
... )
{'confusion_matrix': array([[2, 0], [0, 3]]),
 'count': 5,
 'false_negative_rate': 0.0,
 'false_negative': 0,
 'false_positive_rate': 0.0,
 'false_positive': 0,
 'true_negative_rate': 1.0,
 'true_negative': 2,
 'true_positive_rate': 1.0,
 'true_positive': 3}
>>> # multiclass
>>> confusion_matrix(
...     y_true=np.array([0, 1, 1, 2, 2, 2]),
...     y_pred=np.array([0, 1, 1, 2, 2, 2]),
...     label_type="multiclass",
... )
{'confusion_matrix': array([[1, 0, 0], [0, 2, 0], [0, 0, 3]]), 'count': 6}
>>> # multilabel
>>> confusion_matrix(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     label_type="multilabel",
... )
{'confusion_matrix': array([[[2, 0], [0, 3]],
                            [[3, 0], [0, 2]],
                            [[2, 0], [0, 3]]]),
 'count': 5}

arkas.metric.energy_distance

energy_distance(
    u_values: ndarray,
    v_values: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the energy distance between two 1D distributions.

Parameters:

Name Type Description Default
u_values ndarray

The values observed in the (empirical) distribution.

required
v_values ndarray

The values observed in the (empirical) distribution.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import energy_distance
>>> energy_distance(u_values=np.array([1, 2, 3, 4, 5]), v_values=np.array([1, 2, 3, 4, 5]))
{'count': 5, 'energy_distance': 0.0}

arkas.metric.fbeta_score

fbeta_score(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    betas: Sequence[float] = (1,),
    label_type: str = "auto",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the F-beta metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
betas Sequence[float]

The betas used to compute the F-beta scores.

(1,)
label_type str

The type of labels used to evaluate the metrics. The valid values are: 'binary', 'multiclass', and 'multilabel'. If 'binary' or 'multilabel', y_true values must be 0 and 1.

'auto'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import fbeta_score
>>> # auto
>>> fbeta_score(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'count': 5, 'f1': 1.0}
>>> # binary
>>> fbeta_score(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_pred=np.array([1, 0, 0, 1, 1]),
...     label_type="binary",
... )
{'count': 5, 'f1': 1.0}
>>> # multiclass
>>> fbeta_score(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_pred=np.array([0, 0, 1, 1, 2, 2]),
...     label_type="multiclass",
... )
{'count': 6,
 'f1': array([1., 1., 1.]),
 'macro_f1': 1.0,
 'micro_f1': 1.0,
 'weighted_f1': 1.0}
>>> # multilabel
>>> fbeta_score(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     label_type="multilabel",
... )
{'count': 5,
 'f1': array([1., 1., 1.]),
 'macro_f1': 1.0,
 'micro_f1': 1.0,
 'weighted_f1': 1.0}

arkas.metric.jaccard

jaccard(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    label_type: str = "auto",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Jaccard metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
label_type str

The type of labels used to evaluate the metrics. The valid values are: 'binary', 'multiclass', and 'multilabel'. If 'binary' or 'multilabel', y_true values must be 0 and 1.

'auto'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import jaccard
>>> # auto
>>> jaccard(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'count': 5, 'jaccard': 1.0}
>>> # binary
>>> jaccard(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_pred=np.array([1, 0, 0, 1, 1]),
...     label_type="binary",
... )
{'count': 5, 'jaccard': 1.0}
>>> # multiclass
>>> jaccard(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_pred=np.array([0, 0, 1, 1, 2, 2]),
...     label_type="multiclass",
... )
{'count': 6,
 'jaccard': array([1., 1., 1.]),
 'macro_jaccard': 1.0,
 'micro_jaccard': 1.0,
 'weighted_jaccard': 1.0}
>>> # multilabel
>>> jaccard(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     label_type="multilabel",
... )
{'count': 5,
 'jaccard': array([1., 1., 1.]),
 'macro_jaccard': 1.0,
 'micro_jaccard': 1.0,
 'weighted_jaccard': 1.0}

arkas.metric.jensen_shannon_divergence

jensen_shannon_divergence(
    p: ndarray,
    q: ndarray,
    *,
    prefix: str = "",
    suffix: str = ""
) -> dict[str, float]

Return the Jensen-Shannon (JS) divergence between two distributions.

Parameters:

Name Type Description Default
p ndarray

The true probability distribution.

required
q ndarray

The model probability distribution.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import jensen_shannon_divergence
>>> jensen_shannon_divergence(
...     p=np.array([0.1, 0.6, 0.1, 0.2]), q=np.array([0.2, 0.5, 0.2, 0.1])
... )
{'size': 4, 'jensen_shannon_divergence': 0.027...}

arkas.metric.kl_div

kl_div(
    p: ndarray,
    q: ndarray,
    *,
    prefix: str = "",
    suffix: str = ""
) -> dict[str, float]

Return the Kullback-Leibler (KL) divergence between two distributions.

Parameters:

Name Type Description Default
p ndarray

The true probability distribution.

required
q ndarray

The model probability distribution.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import kl_div
>>> kl_div(p=np.array([0.1, 0.6, 0.1, 0.2]), q=np.array([0.2, 0.5, 0.2, 0.1]))
{'size': 4, 'kl_pq': 0.109..., 'kl_qp': 0.116...}

arkas.metric.mean_absolute_error

mean_absolute_error(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the mean absolute error (MAE).

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import mean_absolute_error
>>> mean_absolute_error(y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5]))
{'count': 5, 'mean_absolute_error': 0.0}

arkas.metric.mean_absolute_percentage_error

mean_absolute_percentage_error(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the mean absolute percentage error (MAPE).

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import mean_absolute_percentage_error
>>> mean_absolute_percentage_error(
...     y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5])
... )
{'count': 5, 'mean_absolute_percentage_error': 0.0}

arkas.metric.mean_squared_error

mean_squared_error(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the mean squared error (MSE).

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import mean_squared_error
>>> mean_squared_error(y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5]))
{'count': 5, 'mean_squared_error': 0.0}

arkas.metric.mean_squared_log_error

mean_squared_log_error(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the mean squared logarithmic error (MSLE).

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import mean_squared_log_error
>>> mean_squared_log_error(
...     y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5])
... )
{'count': 5, 'mean_squared_log_error': 0.0}

arkas.metric.mean_tweedie_deviance

mean_tweedie_deviance(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    powers: Sequence[float] = (0,),
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the mean Tweedie deviance regression loss.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
powers Sequence[float]

The Tweedie power parameter. The higher power the less weight is given to extreme deviations between true and predicted targets.

(0,)
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import mean_tweedie_deviance
>>> mean_tweedie_deviance(
...     y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5])
... )
{'count': 5, 'mean_tweedie_deviance_power_0': 0.0}

arkas.metric.median_absolute_error

median_absolute_error(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the median absolute error.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import median_absolute_error
>>> median_absolute_error(
...     y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5])
... )
{'count': 5, 'median_absolute_error': 0.0}

arkas.metric.multiclass_average_precision

multiclass_average_precision(
    y_true: ndarray,
    y_score: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the average precision metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples, n_classes).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multiclass_average_precision
>>> metrics = multiclass_average_precision(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_score=np.array(
...         [
...             [0.7, 0.2, 0.1],
...             [0.4, 0.3, 0.3],
...             [0.1, 0.8, 0.1],
...             [0.2, 0.3, 0.5],
...             [0.4, 0.4, 0.2],
...             [0.1, 0.2, 0.7],
...         ]
...     ),
... )
>>> metrics
{'average_precision': array([0.833..., 0.75 , 0.75 ]),
 'count': 6,
 'macro_average_precision': 0.777...,
 'micro_average_precision': 0.75,
 'weighted_average_precision': 0.777...}

arkas.metric.multiclass_confusion_matrix

multiclass_confusion_matrix(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the confusion matrix metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multiclass_confusion_matrix
>>> multiclass_confusion_matrix(
...     y_true=np.array([0, 1, 1, 2, 2, 2]), y_pred=np.array([0, 1, 1, 2, 2, 2])
... )
{'confusion_matrix': array([[1, 0, 0], [0, 2, 0], [0, 0, 3]]), 'count': 6}

arkas.metric.multiclass_fbeta_score

multiclass_fbeta_score(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    betas: Sequence[float] = (1,),
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the F-beta metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
betas Sequence[float]

The betas used to compute the F-beta scores.

(1,)
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multiclass_fbeta_score
>>> multiclass_fbeta_score(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_pred=np.array([0, 0, 1, 1, 2, 2]),
... )
{'count': 6,
 'f1': array([1., 1., 1.]),
 'macro_f1': 1.0,
 'micro_f1': 1.0,
 'weighted_f1': 1.0}

arkas.metric.multiclass_jaccard

multiclass_jaccard(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Jaccard metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multiclass_jaccard
>>> multiclass_jaccard(
...     y_true=np.array([0, 0, 1, 1, 2, 2]), y_pred=np.array([0, 0, 1, 1, 2, 2])
... )
{'count': 6,
 'jaccard': array([1., 1., 1.]),
 'macro_jaccard': 1.0,
 'micro_jaccard': 1.0,
 'weighted_jaccard': 1.0}

arkas.metric.multiclass_precision

multiclass_precision(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the precision metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multiclass_precision
>>> multiclass_precision(
...     y_true=np.array([0, 0, 1, 1, 2, 2]), y_pred=np.array([0, 0, 1, 1, 2, 2])
... )
{'count': 6,
 'macro_precision': 1.0,
 'micro_precision': 1.0,
 'precision': array([1., 1., 1.]),
 'weighted_precision': 1.0}

arkas.metric.multiclass_recall

multiclass_recall(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the recall metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multiclass_recall
>>> multiclass_recall(
...     y_true=np.array([0, 0, 1, 1, 2, 2]), y_pred=np.array([0, 0, 1, 1, 2, 2])
... )
{'count': 6,
 'macro_recall': 1.0,
 'micro_recall': 1.0,
 'recall': array([1., 1., 1.]),
 'weighted_recall': 1.0}

arkas.metric.multiclass_roc_auc

multiclass_roc_auc(
    y_true: ndarray,
    y_score: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Area Under the Receiver Operating Characteristic Curve (ROC AUC) metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples, n_classes).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

arkas.metric.multiclass_top_k_accuracy

multiclass_top_k_accuracy(
    y_true: ndarray,
    y_score: ndarray,
    *,
    k: Sequence[int] = (2,),
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Area Under the Top-k Accuracy classification metrics for multiclass labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples, n_classes).

required
k Sequence[int]

The numbers of most likely outcomes considered to find the correct label.

(2,)
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

arkas.metric.multilabel_average_precision

multilabel_average_precision(
    y_true: ndarray,
    y_score: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the average precision metrics for multilabel labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, n_classes).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples, n_classes).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multilabel_average_precision
>>> metrics = multilabel_average_precision(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_score=np.array([[2, -1, -1], [-1, 1, 2], [0, 2, 3], [3, -2, -4], [1, -3, -5]]),
... )
>>> metrics
{'average_precision': array([1. , 1. , 0.477...]),
 'count': 5,
 'macro_average_precision': 0.825...,
 'micro_average_precision': 0.588...,
 'weighted_average_precision': 0.804...}

arkas.metric.multilabel_confusion_matrix

multilabel_confusion_matrix(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the confusion matrix metrics for multilabel labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multilabel_confusion_matrix
>>> multilabel_confusion_matrix(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
... )
{'confusion_matrix': array([[[2, 0], [0, 3]],
                            [[3, 0], [0, 2]],
                            [[2, 0], [0, 3]]]),
 'count': 5}

arkas.metric.multilabel_fbeta_score

multilabel_fbeta_score(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    betas: Sequence[float] = (1,),
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the F-beta metrics for multilabel labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,).

required
betas Sequence[float]

The betas used to compute the F-beta scores.

(1,)
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multilabel_fbeta_score
>>> multilabel_fbeta_score(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
... )
{'count': 5,
 'f1': array([1., 1., 1.]),
 'macro_f1': 1.0,
 'micro_f1': 1.0,
 'weighted_f1': 1.0}

arkas.metric.multilabel_jaccard

multilabel_jaccard(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Jaccard metrics for multilabel labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, n_classes).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples, n_classes).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multilabel_jaccard
>>> multilabel_jaccard(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
... )
{'count': 5,
 'jaccard': array([1., 1., 1.]),
 'macro_jaccard': 1.0,
 'micro_jaccard': 1.0,
 'weighted_jaccard': 1.0}

arkas.metric.multilabel_precision

multilabel_precision(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the precision metrics for multilabel labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, n_classes).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples, n_classes).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multilabel_precision
>>> multilabel_precision(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
... )
{'count': 5,
 'macro_precision': 1.0,
 'micro_precision': 1.0,
 'precision': array([1., 1., 1.]),
 'weighted_precision': 1.0}

arkas.metric.multilabel_recall

multilabel_recall(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the recall metrics for multilabel labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, n_classes).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples, n_classes).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import multilabel_recall
>>> multilabel_recall(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
... )
{'count': 5,
 'macro_recall': 1.0,
 'micro_recall': 1.0,
 'recall': array([1., 1., 1.]),
 'weighted_recall': 1.0}

arkas.metric.multilabel_roc_auc

multilabel_roc_auc(
    y_true: ndarray,
    y_score: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Area Under the Receiver Operating Characteristic Curve (ROC AUC) metrics for multilabel labels.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, n_classes).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples, n_classes).

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

arkas.metric.ndcg

ndcg(
    y_true: ndarray,
    y_score: ndarray,
    *,
    k: int | None = None,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the Normalized Discounted Cumulative Gain (NDCG) metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target targets of multilabel classification, or true scores of entities to be ranked. Negative values in y_true may result in an output that is not between 0 and 1. This input must be an array of shape (n_samples, n_labels).

required
y_score ndarray

The predicted scores, can either be probability estimates, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples, n_labels).

required
k int | None

Only consider the highest k scores in the ranking. If None, use all outputs.

None
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import ndcg
>>> ndcg(
...     y_true=np.array([[1, 0, 0], [1, 2, 0], [1, 1, 2], [0, 0, 1]]),
...     y_score=np.array(
...         [[2.0, 1.0, 0.0], [0.0, 1.0, -1.0], [0.0, 0.0, 1.0], [1.0, 2.0, 3.0]]
...     ),
... )
{'count': 4, 'ndcg': 1.0}

arkas.metric.pearsonr

pearsonr(
    x: ndarray,
    y: ndarray,
    *,
    alternative: str = "two-sided",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the Pearson correlation coefficient and p-value for testing non-correlation.

Parameters:

Name Type Description Default
x ndarray

The first input array.

required
y ndarray

The second input array.

required
alternative str

The alternative hypothesis. Default is 'two-sided'. The following options are available: - 'two-sided': the correlation is nonzero - 'less': the correlation is negative (less than zero) - 'greater': the correlation is positive (greater than zero)

'two-sided'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import pearsonr
>>> pearsonr(x=np.array([1, 2, 3, 4, 5]), y=np.array([1, 2, 3, 4, 5]))
{'count': 5, 'pearson_coeff': 1.0, 'pearson_pvalue': 0.0}

arkas.metric.precision

precision(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    label_type: str = "auto",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the precision metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
label_type str

The type of labels used to evaluate the metrics. The valid values are: 'binary', 'multiclass', and 'multilabel'. If 'binary' or 'multilabel', y_true values must be 0 and 1.

'auto'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import precision
>>> # auto
>>> precision(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'count': 5, 'precision': 1.0}
>>> # binary
>>> precision(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_pred=np.array([1, 0, 0, 1, 1]),
...     label_type="binary",
... )
{'count': 5, 'precision': 1.0}
>>> # multiclass
>>> precision(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_pred=np.array([0, 0, 1, 1, 2, 2]),
...     label_type="multiclass",
... )
{'count': 6,
 'macro_precision': 1.0,
 'micro_precision': 1.0,
 'precision': array([1., 1., 1.]),
 'weighted_precision': 1.0}
>>> # multilabel
>>> precision(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     label_type="multilabel",
... )
{'count': 5,
 'macro_precision': 1.0,
 'micro_precision': 1.0,
 'precision': array([1., 1., 1.]),
 'weighted_precision': 1.0}

arkas.metric.r2_score

r2_score(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the R^2 (coefficient of determination) regression score metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import r2_score
>>> r2_score(y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5]))
{'count': 5, 'r2_score': 1.0}

arkas.metric.recall

recall(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    label_type: str = "auto",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the recall metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
y_pred ndarray

The predicted labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
label_type str

The type of labels used to evaluate the metrics. The valid values are: 'binary', 'multiclass', and 'multilabel'. If 'binary' or 'multilabel', y_true values must be 0 and 1.

'auto'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import recall
>>> # auto
>>> recall(y_true=np.array([1, 0, 0, 1, 1]), y_pred=np.array([1, 0, 0, 1, 1]))
{'count': 5, 'recall': 1.0}
>>> # binary
>>> recall(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_pred=np.array([1, 0, 0, 1, 1]),
...     label_type="binary",
... )
{'count': 5, 'recall': 1.0}
>>> # multiclass
>>> recall(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_pred=np.array([0, 0, 1, 1, 2, 2]),
...     label_type="multiclass",
... )
{'count': 6,
 'macro_recall': 1.0,
 'micro_recall': 1.0,
 'recall': array([1., 1., 1.]),
 'weighted_recall': 1.0}
>>> # multilabel
>>> recall(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_pred=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     label_type="multilabel",
... )
{'count': 5,
 'macro_recall': 1.0,
 'micro_recall': 1.0,
 'recall': array([1., 1., 1.]),
 'weighted_recall': 1.0}

arkas.metric.regression_errors

regression_errors(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the regression error metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import regression_errors
>>> regression_errors(y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5]))
{'count': 5,
 'mean_absolute_error': 0.0,
 'median_absolute_error': 0.0,
 'mean_squared_error': 0.0}

arkas.metric.roc_auc

roc_auc(
    y_true: ndarray,
    y_score: ndarray,
    *,
    label_type: str = "auto",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Area Under the Receiver Operating Characteristic Curve (ROC AUC) metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. This input must be an array of shape (n_samples,) or (n_samples, n_classes).

required
label_type str

The type of labels used to evaluate the metrics. The valid values are: 'binary', 'multiclass', and 'multilabel'. If 'binary' or 'multilabel', y_true values must be 0 and 1.

'auto'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import roc_auc
>>> # auto
>>> metrics = roc_auc(y_true=np.array([1, 0, 0, 1, 1]), y_score=np.array([2, -1, 0, 3, 1]))
>>> metrics
{'count': 5, 'roc_auc': 1.0}
>>> # binary
>>> metrics = roc_auc(
...     y_true=np.array([1, 0, 0, 1, 1]),
...     y_score=np.array([2, -1, 0, 3, 1]),
...     label_type="binary",
... )
>>> metrics
{'count': 5, 'roc_auc': 1.0}
>>> # multiclass
>>> metrics = roc_auc(
...     y_true=np.array([0, 0, 1, 1, 2, 2]),
...     y_score=np.array(
...         [
...             [0.7, 0.2, 0.1],
...             [0.4, 0.3, 0.3],
...             [0.1, 0.8, 0.1],
...             [0.2, 0.3, 0.5],
...             [0.4, 0.4, 0.2],
...             [0.1, 0.2, 0.7],
...         ]
...     ),
...     label_type="multiclass",
... )
>>> metrics
{'count': 6,
 'macro_roc_auc': 0.833...,
 'micro_roc_auc': 0.826...,
 'roc_auc': array([0.9375, 0.8125, 0.75  ]),
 'weighted_roc_auc': 0.833...}
>>> # multilabel
>>> metrics = roc_auc(
...     y_true=np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]]),
...     y_score=np.array([[2, -1, -1], [-1, 1, 2], [0, 2, 3], [3, -2, -4], [1, -3, -5]]),
...     label_type="multilabel",
... )
>>> metrics
{'count': 5,
 'macro_roc_auc': 0.666...,
 'micro_roc_auc': 0.544...,
 'roc_auc': array([1., 1., 0.]),
 'weighted_roc_auc': 0.625}

arkas.metric.root_mean_squared_error

root_mean_squared_error(
    y_true: ndarray,
    y_pred: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the root mean squared error (RMSE).

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target values.

required
y_pred ndarray

The predicted values.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import root_mean_squared_error
>>> root_mean_squared_error(
...     y_true=np.array([1, 2, 3, 4, 5]), y_pred=np.array([1, 2, 3, 4, 5])
... )
{'count': 5, 'root_mean_squared_error': 0.0}
Note

Require sklearn>=1.4.0

arkas.metric.spearmanr

spearmanr(
    x: ndarray,
    y: ndarray,
    *,
    alternative: str = "two-sided",
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the Spearman correlation coefficient and p-value for testing non-correlation.

Parameters:

Name Type Description Default
x ndarray

The first input array.

required
y ndarray

The second input array.

required
alternative str

The alternative hypothesis. Default is 'two-sided'. The following options are available: - 'two-sided': the correlation is nonzero - 'less': the correlation is negative (less than zero) - 'greater': the correlation is positive (greater than zero)

'two-sided'
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import spearmanr
>>> spearmanr(
...     x=np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]),
...     y=np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]),
... )
{'count': 9, 'spearman_coeff': 1.0, 'spearman_pvalue': 0.0}

arkas.metric.top_k_accuracy

top_k_accuracy(
    y_true: ndarray,
    y_score: ndarray,
    *,
    k: Sequence[int] = (2,),
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float | ndarray]

Return the Area Under the Top-k Accuracy classification metrics.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,).

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions. The binary case expects scores with shape (n_samples,) while the multiclass case expects scores with shape (n_samples, n_classes).

required
k Sequence[int]

The numbers of most likely outcomes considered to find the correct label.

(2,)
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float | ndarray]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import top_k_accuracy
>>> # binary
>>> metrics = top_k_accuracy(
...     y_true=np.array([1, 0, 0, 1, 1]), y_score=np.array([2, -1, 0, 3, 1]), k=[1, 2]
... )
>>> metrics
{'count': 5, 'top_1_accuracy': 1.0, 'top_2_accuracy': 1.0}
>>> # multiclass
>>> metrics = top_k_accuracy(
...     y_true=np.array([0, 1, 2, 2]),
...     y_score=np.array(
...         [[0.5, 0.2, 0.2], [0.3, 0.4, 0.2], [0.2, 0.4, 0.3], [0.7, 0.2, 0.1]]
...     ),
...     k=[1, 2, 3],
... )
>>> metrics
{'count': 4, 'top_1_accuracy': 0.5, 'top_2_accuracy': 0.75, 'top_3_accuracy': 1.0}

arkas.metric.wasserstein_distance

wasserstein_distance(
    u_values: ndarray,
    v_values: ndarray,
    *,
    prefix: str = "",
    suffix: str = "",
    nan_policy: str = "propagate"
) -> dict[str, float]

Return the Wasserstein distance between two 1D discrete distributions.

Parameters:

Name Type Description Default
u_values ndarray

An array that contains a sample from a probability distribution or the support (set of all possible values) of a probability distribution. Each element is an observation or possible value.

required
v_values ndarray

An array that contains a sample from or the support of a second distribution.

required
prefix str

The key prefix in the returned dictionary.

''
suffix str

The key suffix in the returned dictionary.

''
nan_policy str

The policy on how to handle NaN values in the input arrays. The following options are available: 'omit', 'propagate', and 'raise'.

'propagate'

Returns:

Type Description
dict[str, float]

The computed metrics.

Example usage:

>>> import numpy as np
>>> from arkas.metric import wasserstein_distance
>>> wasserstein_distance(
...     u_values=np.array([1, 2, 3, 4, 5]), v_values=np.array([1, 2, 3, 4, 5])
... )
{'count': 5, 'wasserstein_distance': 0.0}

arkas.metric.utils

Contain utility functions to compute metrics.

arkas.metric.utils.check_array_ndim

check_array_ndim(arr: ndarray, ndim: int) -> None

Check if the number of array dimensions is matching the target number of dimensions.

Parameters:

Name Type Description Default
arr ndarray

The array to check.

required
ndim int

The targeted number of array dimensions.

required

Raises:

Type Description
ValueError

if the number of array dimensions does not match.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import check_array_ndim
>>> check_array_ndim(np.ones((2, 3)), ndim=2)

arkas.metric.utils.check_label_type

check_label_type(label_type: str) -> None

Check if the label type value is valid or not.

Parameters:

Name Type Description Default
label_type str

The type of labels. The valid values are 'binary', 'multiclass', 'multilabel', and 'auto'.

required

Raises:

Type Description
ValueError

if an invalid value is passed to label_type.

Example usage:

>>> from arkas.metric.utils import check_label_type
>>> check_label_type(label_type="binary")

arkas.metric.utils.check_nan_policy

check_nan_policy(nan_policy: str) -> None

Check the NaN policy.

Parameters:

Name Type Description Default
nan_policy str

The NaN policy.

required

Raises:

Type Description
ValueError

if nan_policy is not 'omit', 'propagate', or 'raise'.

Example usage:

>>> from arkas.metric.utils import check_nan_policy
>>> check_nan_policy(nan_policy="omit")

arkas.metric.utils.check_nan_pred

check_nan_pred(y_true: ndarray, y_pred: ndarray) -> None

Check if any array elements in y_true or y_pred arrays is a NaN value.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required

Raises:

Type Description
RuntimeError

'y_true' or 'y_pred' has a NaN value.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import check_nan_pred
>>> y_true = np.array([1, 0, 0, 1])
>>> y_pred = np.array([0, 1, 0, 1])
>>> check_nan_pred(y_true, y_pred)

arkas.metric.utils.check_same_shape

check_same_shape(arrays: Iterable[ndarray]) -> None

Check if arrays have the same shape.

Parameters:

Name Type Description Default
arrays Iterable[ndarray]

The arrays to check.

required

Raises:

Type Description
RuntimeError

if the arrays have different shapes.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import check_same_shape
>>> check_same_shape([np.array([1, 0, 0, 1]), np.array([0, 1, 0, 1])])

arkas.metric.utils.check_same_shape_pred

check_same_shape_pred(
    y_true: ndarray, y_pred: ndarray
) -> None

Check if y_true and y_pred arrays have the same shape.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required

Raises:

Type Description
RuntimeError

'y_true' and 'y_pred' have different shapes.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import check_same_shape_pred
>>> y_true = np.array([1, 0, 0, 1])
>>> y_pred = np.array([0, 1, 0, 1])
>>> check_same_shape_pred(y_true, y_pred)

arkas.metric.utils.check_same_shape_score

check_same_shape_score(
    y_true: ndarray, y_score: ndarray
) -> None

Check if y_true and y_score arrays have the same shape.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_score ndarray

The target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions.

required

Raises:

Type Description
RuntimeError

'y_true' and 'y_score' have different shapes.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import check_same_shape_score
>>> y_true = np.array([1, 0, 0, 1])
>>> y_score = np.array([0, 1, 0, 1])
>>> check_same_shape_score(y_true, y_score)

arkas.metric.utils.contains_nan

contains_nan(
    arr: ndarray,
    nan_policy: str = "propagate",
    name: str = "input array",
) -> bool

Indicate if the given array contains at least one NaN value.

Parameters:

Name Type Description Default
arr ndarray

The array to check.

required
nan_policy str

The NaN policy. The valid values are 'omit', 'propagate', or 'raise'.

'propagate'
name str

An optional name to be more precise about the array when the exception is raised.

'input array'

Returns:

Type Description
bool

True if the array contains at least one NaN value.

Raises:

Type Description
ValueError

if the array contains at least one NaN value and nan_policy is 'raise'.

arkas.metric.utils.multi_isnan

multi_isnan(arrays: Sequence[ndarray]) -> ndarray

Test element-wise for NaN for all input arrays and return result as a boolean array.

Parameters:

Name Type Description Default
arrays Sequence[ndarray]

The input arrays to test. All the arrays must have the same shape.

required

Returns:

Type Description
ndarray

A boolean array. True where any array is NaN, False otherwise.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import multi_isnan
>>> mask = multi_isnan(
...     [np.array([1, 0, 0, 1, float("nan")]), np.array([1, float("nan"), 0, 1, 1])]
... )
>>> mask
array([False,  True, False, False,  True])

arkas.metric.utils.preprocess_pred

preprocess_pred(
    y_true: ndarray, y_pred: ndarray, drop_nan: bool = False
) -> tuple[ndarray, ndarray]

Preprocess y_true and y_pred arrays.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
drop_nan bool

If True, the NaN values are removed, otherwise they are kept.

False

Returns:

Type Description
tuple[ndarray, ndarray]

A tuple with the preprocessed y_true and y_pred arrays.

Raises:

Type Description
RuntimeError

if an invalid value is passed to nan.

RuntimeError

'y_true' and 'y_pred' have different shapes.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import preprocess_pred
>>> y_true = np.array([1, 0, 0, 1, 1, float("nan")])
>>> y_pred = np.array([0, 1, 0, 1, float("nan"), 1])
>>> preprocess_pred(y_true, y_pred)
(array([ 1.,  0.,  0.,  1.,  1., nan]), array([ 0.,  1.,  0.,  1., nan,  1.]))
>>> preprocess_pred(y_true, y_pred, drop_nan=True)
(array([1., 0., 0., 1.]), array([0., 1., 0., 1.]))

arkas.metric.utils.preprocess_pred_multilabel

preprocess_pred_multilabel(
    y_true: ndarray, y_pred: ndarray, drop_nan: bool = False
) -> tuple[ndarray, ndarray]

Preprocess y_true and y_pred arrays.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels.

required
y_pred ndarray

The predicted labels.

required
drop_nan bool

If True, the NaN values are removed, otherwise they are kept.

False

Returns:

Type Description
tuple[ndarray, ndarray]

A tuple with the preprocessed y_true and y_pred arrays.

Raises:

Type Description
RuntimeError

if an invalid value is passed to nan.

RuntimeError

'y_true' and 'y_pred' have different shapes.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import preprocess_pred_multilabel
>>> y_true = np.array([[1, float("nan"), 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]])
>>> y_pred = np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, float("nan")]])
>>> preprocess_pred_multilabel(y_true, y_pred)
(array([[ 1., nan,  1.],
        [ 0.,  1.,  0.],
        [ 0.,  1.,  0.],
        [ 1.,  0.,  1.],
        [ 1.,  0.,  1.]]),
 array([[ 1.,  0.,  1.],
        [ 0.,  1.,  0.],
        [ 0.,  1.,  0.],
        [ 1.,  0.,  1.],
        [ 1.,  0., nan]]))
>>> preprocess_pred_multilabel(y_true, y_pred, drop_nan=True)
(array([[0., 1., 0.],
        [0., 1., 0.],
        [1., 0., 1.]]),
 array([[0., 1., 0.],
        [0., 1., 0.],
        [1., 0., 1.]]))

arkas.metric.utils.preprocess_same_shape_arrays

preprocess_same_shape_arrays(
    arrays: Sequence[ndarray], drop_nan: bool = False
) -> tuple[ndarray, ...]

Preprocess a sequence of same shape arrays.

Parameters:

Name Type Description Default
arrays Sequence[ndarray]

The arrays to preprocess.

required
drop_nan bool

If True, the NaN values are removed, otherwise they are kept.

False

Returns:

Type Description
tuple[ndarray, ...]

A tuple with the preprocessed y_true and y_pred arrays.

Raises:

Type Description
RuntimeError

if the arrays have different shapes.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import preprocess_same_shape_arrays
>>> arrays = [
...     np.array([1, 0, 0, 1, 1, float("nan")]),
...     np.array([0, 1, 0, 1, float("nan"), 1]),
... ]
>>> preprocess_same_shape_arrays(arrays)
(array([ 1.,  0.,  0.,  1.,  1., nan]), array([ 0.,  1.,  0.,  1., nan,  1.]))
>>> preprocess_same_shape_arrays(arrays, drop_nan=True)
(array([1., 0., 0., 1.]), array([0., 1., 0., 1.]))

arkas.metric.utils.preprocess_score_binary

preprocess_score_binary(
    y_true: ndarray,
    y_score: ndarray,
    drop_nan: bool = False,
) -> tuple[ndarray, ndarray]

Preprocess y_true and y_score arrays for the binary classification case.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, *).

required
y_score ndarray

The predicted labels. This input must be an array of shape (n_samples, *).

required
drop_nan bool

If True, the NaN values are removed, otherwise they are kept.

False

Returns:

Type Description
tuple[ndarray, ndarray]

A tuple with the preprocessed y_true and y_score arrays.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import preprocess_score_binary
>>> y_true = np.array([1, 0, 0, 1, 1, float("nan")])
>>> y_score = np.array([0, 1, 0, 1, float("nan"), 1])
>>> preprocess_score_binary(y_true, y_score)
(array([ 1.,  0.,  0.,  1.,  1., nan]), array([ 0.,  1.,  0.,  1., nan,  1.]))
>>> preprocess_score_binary(y_true, y_score, drop_nan=True)
(array([1., 0., 0., 1.]), array([0., 1., 0., 1.]))

arkas.metric.utils.preprocess_score_multiclass

preprocess_score_multiclass(
    y_true: ndarray,
    y_score: ndarray,
    drop_nan: bool = False,
) -> tuple[ndarray, ndarray]

Preprocess y_true and y_score arrays for the multiclass classification case.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples,) or (n_samples, 1).

required
y_score ndarray

The predicted labels. This input must be an array of shape (n_samples, n_classes).

required
drop_nan bool

If True, the NaN values are removed, otherwise they are kept.

False

Returns:

Type Description
tuple[ndarray, ndarray]

A tuple with the preprocessed y_true and y_score arrays.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import preprocess_score_multiclass
>>> y_true = np.array([0, 0, 1, 1, 2, float("nan")])
>>> y_score = np.array(
...     [
...         [0.7, 0.2, 0.1],
...         [0.4, 0.3, 0.3],
...         [0.1, 0.8, float("nan")],
...         [0.2, 0.3, 0.5],
...         [0.4, 0.4, 0.2],
...         [0.1, 0.2, 0.7],
...     ]
... )
>>> preprocess_score_multiclass(y_true, y_score)
(array([ 0.,  0.,  1.,  1.,  2., nan]),
 array([[0.7, 0.2, 0.1],
        [0.4, 0.3, 0.3],
        [0.1, 0.8, nan],
        [0.2, 0.3, 0.5],
        [0.4, 0.4, 0.2],
        [0.1, 0.2, 0.7]]))
>>> preprocess_score_multiclass(y_true, y_score, drop_nan=True)
(array([0., 0., 1., 2.]),
 array([[0.7, 0.2, 0.1],
        [0.4, 0.3, 0.3],
        [0.2, 0.3, 0.5],
        [0.4, 0.4, 0.2]]))

arkas.metric.utils.preprocess_score_multilabel

preprocess_score_multilabel(
    y_true: ndarray,
    y_score: ndarray,
    drop_nan: bool = False,
) -> tuple[ndarray, ndarray]

Preprocess y_true and y_score arrays for the multilabel classification case.

Parameters:

Name Type Description Default
y_true ndarray

The ground truth target labels. This input must be an array of shape (n_samples, n_classes) or (n_samples,).

required
y_score ndarray

The predicted labels. This input must be an array of shape (n_samples, n_classes) or (n_samples,).

required
drop_nan bool

If True, the NaN values are removed, otherwise they are kept.

False

Returns:

Type Description
tuple[ndarray, ndarray]

A tuple with the preprocessed y_true and y_score arrays.

Example usage:

>>> import numpy as np
>>> from arkas.metric.utils import preprocess_score_multilabel
>>> y_true = np.array([[1, float("nan"), 1], [0, 1, 0], [0, 1, 0], [1, 0, 1], [1, 0, 1]])
>>> y_score = np.array(
...     [[2, -1, -1], [-1, 1, 2], [0, 2, 3], [3, -2, -4], [1, float("nan"), -5]]
... )
>>> preprocess_score_multilabel(y_true, y_score)
(array([[ 1., nan,  1.],
        [ 0.,  1.,  0.],
        [ 0.,  1.,  0.],
        [ 1.,  0.,  1.],
        [ 1.,  0.,  1.]]),
 array([[ 2., -1., -1.],
        [-1.,  1.,  2.],
        [ 0.,  2.,  3.],
        [ 3., -2., -4.],
        [ 1., nan, -5.]]))
>>> preprocess_score_multilabel(y_true, y_score, drop_nan=True)
(array([[0., 1., 0.],
        [0., 1., 0.],
        [1., 0., 1.]]),
 array([[-1.,  1.,  2.],
        [ 0.,  2.,  3.],
        [ 3., -2., -4.]]))