mojolearn.metrics#
Scoring functions: classification, regression, ranking and clustering metrics.
mojolearn.metrics is shaped like sklearn.metrics: plain functions, no
estimator class. Function and argument names follow scikit-learn, and the
work runs on the GPU. Where cuML has the same function, its defaults and
semantics are followed, and each function states where scikit-learn and cuML
differ. Three differences matter most:
entropyandmutual_info_scoreare in nats.r2_scorereturns scikit-learn’s finite values for constant targets (force_finite=True) by default.kl_divergencedoes not normalizePandQ, as in cuML.
A parameter a function cannot honor raises an error naming it; nothing is silently accepted and ignored. Inputs are checked here: non-finite values are refused where the docstring says so.
Functions that take numeric_mode accept 'identical' (the same bits
on NVIDIA and AMD), 'deterministic', or 'fast' (Apple only); None
uses the library default. Other functions run in the library default mode.
Examples
>>> import numpy as np
>>> from mojolearn import metrics
>>> rng = np.random.default_rng(0)
>>> y_true = rng.integers(0, 2, 1000).astype(np.int32)
>>> y_pred = rng.integers(0, 2, 1000).astype(np.int32)
>>> acc = metrics.accuracy_score(y_true, y_pred)
Functions
|
Accuracy: the fraction (or number) of positions where two label arrays agree. |
|
Confusion matrix of single-label predictions. |
|
Precision: |
|
Recall: |
|
F1 score: the harmonic mean of precision and recall. |
|
Log loss (cross-entropy) of predicted probabilities. |
|
Area under the ROC curve. |
|
Precision-recall pairs for each score threshold (binary). |
|
Rand index adjusted for chance. |
|
Completeness: whether all members of a class are in the same cluster. |
|
Entropy of a labeling, in nats unless |
|
Fowlkes-Mallows index between two clusterings. |
|
Homogeneity, completeness and V-measure in one call. |
|
Homogeneity: whether each cluster holds members of a single class. |
|
Kullback-Leibler divergence |
|
Mutual information between two clusterings, in nats. |
|
Mean squared error, computed in float32. |
|
Mean absolute error, computed in float32. |
|
Root mean squared error, computed in float32. |
|
Coefficient of determination (R^2), computed in float32. |
|
Rand index: the fraction of sample pairs on which two clusterings agree. |
|
Silhouette coefficient of every sample. |
|
Mean silhouette coefficient over all samples. |
|
Trustworthiness: how well an embedding preserves local neighborhoods. |
|
V-measure: the weighted harmonic mean of homogeneity and completeness. |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
Per-label one-vs-rest 2x2 confusion matrices, |
|
Precision, recall, F-score and support for each label. |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
Area under a curve by the trapezoid rule. |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
Detection error tradeoff curve for binary targets. |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
Normalized discounted cumulative gain. |
|
Receiver operating characteristic curve for binary targets. |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
scikit-learn 1.9 |
|
Normalized mutual information between two clusterings. |
|
scikit-learn 1.9 |
- mojolearn.metrics.accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None, numeric_mode=None)[source]#
Accuracy: the fraction (or number) of positions where two label arrays agree.
Follows scikit-learn’s
accuracy_score; the unweighted fraction uses cuML’s kernel. The count of matches is exact on every device.- Parameters:
y_true, y_pred (array-like of shape (n_samples,)) – Integer labels of the same length that fit in int32. Larger labels raise
ValueError. Withnormalize=False, string labels are accepted too.normalize (bool, default True) – True returns the fraction of matches. False returns the number of matches (an int), or with
sample_weighttheir summed weight.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights, as in scikit-learn’s
np.average(y_true == y_pred, weights=sample_weight), computed in float32. Weights must be finite, non-negative and have a positive total; negative weights, which scikit-learn accepts, raiseValueError.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
score (float or int)
- Raises:
ValueError – For empty input or arrays of different lengths.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> metrics.accuracy_score(np.array([0, 1, 1, 0], dtype=np.int32), ... np.array([0, 1, 0, 0], dtype=np.int32)) 0.75
- mojolearn.metrics.confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None, normalize=None, numeric_mode=None)[source]#
Confusion matrix of single-label predictions.
Follows scikit-learn’s
confusion_matrix: rowiis the true label and columnjthe predicted label. By default the labels are the sorted union of those seen iny_trueandy_pred. Withlabels, their order is kept and rows whose true or predicted label is not inlabelsare left out.- Parameters:
y_true, y_pred (array-like of shape (n_samples,)) – String or integer labels of the same type and length, nonempty. Float labels raise
TypeError.labels (array-like, default None) – Unique labels to index the matrix. At least one must occur in
y_true. Withoutsample_weight, at most 4096 labels.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights; the cells are then float64 sums of weights.
normalize ({‘true’, ‘pred’, ‘all’} or None, default None) – Normalize over rows, columns or the whole matrix. Rows or columns with no mass give zeros.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
C (Array of shape (n_labels, n_labels)) – int64 counts; float32 when normalized; float64 with
sample_weight.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> C = metrics.confusion_matrix(np.array([0, 1, 2, 2], dtype=np.int32), ... np.array([0, 2, 2, 1], dtype=np.int32))
- mojolearn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn', numeric_mode=None)[source]#
Precision:
tp / (tp + fp).Follows scikit-learn’s
precision_scorefor single-label targets.- Parameters:
y_true, y_pred (array-like of shape (n_samples,)) – String or integer labels of the same type and length, nonempty. Float and multilabel input are refused.
labels (array-like, default None) – Labels to report, in order. Labels left out still count as errors for the reported ones. Ignored for
average='binary'.pos_label (int or str, default 1) – The positive class for
average='binary'.average ({‘binary’, ‘micro’, ‘macro’, ‘weighted’} or None, default ‘binary’) – How to combine per-label scores; None returns one score per label.
'binary'needs at most two observed classes.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights.
zero_division ({‘warn’, 0, 1}, default ‘warn’) – The value for an undefined ratio;
'warn'gives 0 and a warning.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
score (float or Array of shape (n_labels,)) – A float, or a float32 Array per label when
average=None.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> p = metrics.precision_score(np.array([0, 1, 1, 0], dtype=np.int32), ... np.array([0, 1, 0, 1], dtype=np.int32))
- mojolearn.metrics.recall_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn', numeric_mode=None)[source]#
Recall:
tp / (tp + fn).Follows scikit-learn’s
recall_scorefor single-label targets. Labels, averaging andzero_divisionwork as inprecision_score.- Parameters:
y_true, y_pred (array-like of shape (n_samples,)) – String or integer labels of the same type and length, nonempty. Float and multilabel input are refused.
labels (array-like, default None) – Labels to report, in order. Labels left out still count as errors for the reported ones. Ignored for
average='binary'.pos_label (int or str, default 1) – The positive class for
average='binary'.average ({‘binary’, ‘micro’, ‘macro’, ‘weighted’} or None, default ‘binary’) – How to combine per-label scores; None returns one score per label.
'binary'needs at most two observed classes.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights.
zero_division ({‘warn’, 0, 1}, default ‘warn’) – The value for an undefined ratio;
'warn'gives 0 and a warning.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
score (float or Array of shape (n_labels,)) – A float, or a float32 Array per label when
average=None.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> r = metrics.recall_score(np.array([0, 1, 1, 0], dtype=np.int32), ... np.array([0, 1, 0, 1], dtype=np.int32))
- mojolearn.metrics.f1_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn', numeric_mode=None)[source]#
F1 score: the harmonic mean of precision and recall.
Follows scikit-learn’s
f1_scorefor single-label targets. Labels, averaging andzero_divisionwork as inprecision_score.- Parameters:
y_true, y_pred (array-like of shape (n_samples,)) – String or integer labels of the same type and length, nonempty. Float and multilabel input are refused.
labels (array-like, default None) – Labels to report, in order. Labels left out still count as errors for the reported ones. Ignored for
average='binary'.pos_label (int or str, default 1) – The positive class for
average='binary'.average ({‘binary’, ‘micro’, ‘macro’, ‘weighted’} or None, default ‘binary’) – How to combine per-label scores; None returns one score per label.
'binary'needs at most two observed classes.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights.
zero_division ({‘warn’, 0, 1}, default ‘warn’) – The value for an undefined ratio;
'warn'gives 0 and a warning.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
score (float or Array of shape (n_labels,)) – A float, or a float32 Array per label when
average=None.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> f1 = metrics.f1_score(np.array([0, 1, 1, 0], dtype=np.int32), ... np.array([0, 1, 0, 1], dtype=np.int32), average='macro')
- mojolearn.metrics.log_loss(y_true, y_pred, *, normalize=True, sample_weight=None, labels=None, numeric_mode=None)[source]#
Log loss (cross-entropy) of predicted probabilities.
Follows scikit-learn’s
log_lossfor single-label targets. A 1-D (or one-column)y_predholds the probability of the positive class and is expanded to[1 - p, p]. Otherwise each column is a class: in the order oflabelswhen given, else the sorted labels seen iny_true. Probabilities are checked, not renormalized: each must lie in[0, 1]and each row must sum to one withinsqrt(float32 eps). The selected probabilities are clipped to[eps, 1 - eps](float32 epsilon) before the logarithm.- Parameters:
y_true (array-like of shape (n_samples,)) – String or integer class labels, nonempty.
y_pred (array-like of shape (n_samples,), (n_samples, 1) or (n_samples, n_classes)) – float32 probabilities. Other dtypes raise
TypeError; Python lists are converted to float32.normalize (bool, default True) – True returns the mean loss per sample, False the sum.
sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights, as in scikit-learn.
labels (array-like, default None) – The class labels, unique, in column order. Needed when
y_trueholds a single class. At least two labels are required.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
loss (float)
- Raises:
ValueError – For fewer than two labels, a label in
y_truemissing fromlabels, a column count that does not match the labels, or probabilities that are not finite, outside[0, 1]or do not sum to one.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> y = np.array([0, 1, 1, 0], dtype=np.int32) >>> p = np.array([0.1, 0.9, 0.8, 0.35], dtype=np.float32) >>> loss = metrics.log_loss(y, p)
- mojolearn.metrics.roc_auc_score(y_true, y_score, *, average='macro', sample_weight=None, max_fpr=None, multi_class='raise', labels=None, numeric_mode=None)[source]#
Area under the ROC curve.
Follows scikit-learn’s
roc_auc_score. In the binary case,y_truehas two classes, the larger sorted label is the positive class, andy_scoreholds one finite score per sample (probabilities or any decision scores); tied scores are grouped. Multiclass input (a 2-Dy_scoreof class probabilities) needsmulti_class='ovr'or'ovo', as in scikit-learn.- Parameters:
y_true (array-like of shape (n_samples,)) – String or integer labels. Both classes must be present in the binary case.
y_score (array-like of shape (n_samples,) or (n_samples, n_classes)) – Finite float32 scores. Other dtypes raise
TypeError.average ({‘macro’, ‘weighted’, ‘micro’, ‘samples’} or None, default ‘macro’) – Averaging for multiclass input, as in scikit-learn.
sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights.
max_fpr (float or None, default None) – In (0, 1]: return the standardized partial AUC up to this false positive rate (binary only), as in scikit-learn.
multi_class ({‘raise’, ‘ovr’, ‘ovo’}, default ‘raise’) – Multiclass strategy.
labels (array-like, default None) – Class labels for multiclass input, as in scikit-learn.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
auc (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> y = np.array([0, 0, 1, 1], dtype=np.int32) >>> s = np.array([0.1, 0.4, 0.35, 0.8], dtype=np.float32) >>> auc = metrics.roc_auc_score(y, s)
- mojolearn.metrics.precision_recall_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=False, numeric_mode=None)[source]#
Precision-recall pairs for each score threshold (binary).
Follows scikit-learn’s
precision_recall_curve. Thresholds are the distinct scores in ascending order;precisionandrecallhave one more element thanthresholdsand end at(1, 0).Without
pos_label,y_truemust be integers whose observed values are a subset of{0, 1}or{-1, 1}, and 1 is the positive class; other labels need an explicitpos_labelof the same type. If the positive class is absent, recall is set to one for every threshold and aUserWarningis issued.- Parameters:
y_true (array-like of shape (n_samples,)) – String or integer labels with at most two classes.
y_score (array-like of shape (n_samples,)) – Finite float32 scores (not necessarily probabilities). Other dtypes raise
TypeError.pos_label (int, str or None, default None) – The positive class.
sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights.
drop_intermediate (bool, default False) – Drop thresholds that do not change the curve’s shape, as in scikit-learn.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
precision (Array of shape (n_thresholds + 1,))
recall (Array of shape (n_thresholds + 1,))
thresholds (Array of shape (n_thresholds,)) – float32 by default; float64 when
sample_weightis given ordrop_intermediate=True.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> y = np.array([0, 0, 1, 1], dtype=np.int32) >>> s = np.array([0.1, 0.4, 0.35, 0.8], dtype=np.float32) >>> precision, recall, thresholds = metrics.precision_recall_curve(y, s)
- mojolearn.metrics.adjusted_rand_score(labels_true, labels_pred)[source]#
Rand index adjusted for chance.
Follows scikit-learn’s and cuML’s
adjusted_rand_score: 1.0 for identical clusterings (up to relabeling), about 0.0 for random ones. Labels are used as given.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer cluster labels of the same length that fit in int32.
- Returns:
score (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> rng = np.random.default_rng(0) >>> a = rng.integers(0, 4, 1000).astype(np.int32) >>> score = metrics.adjusted_rand_score(a, a)
- mojolearn.metrics.completeness_score(labels_true, labels_pred)[source]#
Completeness: whether all members of a class are in the same cluster.
MI(pred, true) / H(pred), in[0, 1]. Follows scikit-learn’s and cuML’scompleteness_score. Labels are mapped onto0..k-1over their union first, as cuML does.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer labels of the same length that fit in int32.
- Returns:
score (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> c = metrics.completeness_score(np.array([0, 0, 1, 1], dtype=np.int32), ... np.array([0, 0, 1, 2], dtype=np.int32))
- mojolearn.metrics.entropy(clustering, *, base=None)[source]#
Entropy of a labeling, in nats unless
baseis given.Follows cuML’s
cuml.metrics.cluster.entropy; scikit-learn has no public equivalent.The label histogram spans
max(labels) - min(labels) + 1bins, not the number of distinct labels, as in cuML. Sparse, large-valued labels therefore allocate that many bins; encode them as0..k-1first if that matters.In the
'identical'numeric mode the final logarithms run in float32, so the result can differ from the other modes in about the seventh significant digit.- Parameters:
clustering (array-like of shape (n_samples,)) – Integer labels that fit in int32.
base (float or None, default None) – Logarithm base. None means nats.
- Returns:
entropy (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> h = metrics.entropy(np.array([0, 0, 1, 1], dtype=np.int32), base=2)
- mojolearn.metrics.fowlkes_mallows_score(labels_true, labels_pred, *, sparse='deprecated')[source]#
Fowlkes-Mallows index between two clusterings.
Follows scikit-learn’s
fowlkes_mallows_score(cuML has no such metric): the geometric mean of pairwise precision and recall,sqrt(tk / pk) * sqrt(tk / qk). Returns 0.0 whentk == 0(every cluster a singleton, a single sample, or no samples), as scikit-learn does. Labels are mapped onto0..k-1over their union first.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer cluster labels of the same length that fit in int32.
sparse (default ‘deprecated’) – Deprecated and ignored in scikit-learn; any explicit value raises
NotImplementedError.
- Returns:
score (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> score = metrics.fowlkes_mallows_score(np.array([0, 0, 1, 1], dtype=np.int32), ... np.array([0, 0, 1, 2], dtype=np.int32))
- mojolearn.metrics.homogeneity_completeness_v_measure(labels_true, labels_pred, *, beta=1.0)[source]#
Homogeneity, completeness and V-measure in one call.
Returns
(homogeneity_score, completeness_score, v_measure_score), as scikit-learn’s function of the same name. Each value is computed by its own call, so this costs about three separate metric calls; call the one you need if speed matters.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer labels of the same length that fit in int32.
beta (float, default 1.0) – Passed to
v_measure_score.
- Returns:
homogeneity, completeness, v_measure (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> h, c, v = metrics.homogeneity_completeness_v_measure( ... np.array([0, 0, 1, 1], dtype=np.int32), np.array([0, 0, 1, 2], dtype=np.int32))
- mojolearn.metrics.homogeneity_score(labels_true, labels_pred)[source]#
Homogeneity: whether each cluster holds members of a single class.
MI(true, pred) / H(true), in[0, 1]. Follows scikit-learn’s and cuML’shomogeneity_score. Labels are mapped onto0..k-1over their union first, as cuML does.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer labels of the same length that fit in int32.
- Returns:
score (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> h = metrics.homogeneity_score(np.array([0, 0, 1, 1], dtype=np.int32), ... np.array([0, 0, 1, 2], dtype=np.int32))
- mojolearn.metrics.kl_divergence(P, Q)[source]#
Kullback-Leibler divergence
sum p_i * (log p_i - log q_i), in float32.Follows cuML’s
cuml.metrics.kl_divergence.PandQare not normalized, as in cuML; divide each by its sum first if you want the divergence between distributions. A term withp_i == 0contributes zero, andq_i == 0withp_i > 0givesinf.- Parameters:
P, Q (array-like of shape (n,)) – Finite, non-negative float32 arrays of the same length. float64 input raises
TypeError(cast to float32 first); a negative, NaN or infinite entry raisesValueError.- Returns:
divergence (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> P = np.array([0.5, 0.3, 0.2], dtype=np.float32) >>> Q = np.array([0.4, 0.4, 0.2], dtype=np.float32) >>> kl = metrics.kl_divergence(P, Q)
- mojolearn.metrics.mutual_info_score(labels_true, labels_pred, *, contingency=None)[source]#
Mutual information between two clusterings, in nats.
Follows scikit-learn’s and cuML’s
mutual_info_score, in nats as cuML computes it. Both label arrays are first mapped onto0..k-1over the union of their distinct labels, as cuML does. The contingency counts are exact on every device.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer cluster labels of the same length that fit in int32.
contingency (None, default None) – A precomputed contingency matrix is not supported; passing one raises
NotImplementedError.
- Returns:
mi (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> mi = metrics.mutual_info_score(np.array([0, 0, 1, 1], dtype=np.int32), ... np.array([0, 0, 1, 2], dtype=np.int32))
- mojolearn.metrics.mean_squared_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', numeric_mode=None)[source]#
Mean squared error, computed in float32.
Follows scikit-learn’s
mean_squared_error. The residuals, their squares and the sum are float32, with no wider accumulator, so very large residuals can overflow toinf.- Parameters:
y_true, y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Finite, nonempty float32 arrays of the same shape. A float64 or other non-float32 buffer raises
TypeError: cast it first. Python lists are converted to float32. NaN or infinity raisesValueError.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights, as in scikit-learn.
multioutput ({‘uniform_average’, ‘raw_values’} or array-like, default ‘uniform_average’) – How to combine several outputs, as in scikit-learn.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
loss (float or Array) – A float, or one float64 value per output for
'raw_values'.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> y = np.array([3.0, -0.5, 2.0, 7.0], dtype=np.float32) >>> pred = np.array([2.5, 0.0, 2.0, 8.0], dtype=np.float32) >>> metrics.mean_squared_error(y, pred) 0.375
- mojolearn.metrics.mean_absolute_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', numeric_mode=None)[source]#
Mean absolute error, computed in float32.
Follows scikit-learn’s
mean_absolute_error, with the same input and overflow behavior asmean_squared_error.- Parameters:
y_true, y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Finite, nonempty float32 arrays of the same shape. A float64 or other non-float32 buffer raises
TypeError: cast it first. Python lists are converted to float32. NaN or infinity raisesValueError.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights, as in scikit-learn.
multioutput ({‘uniform_average’, ‘raw_values’} or array-like, default ‘uniform_average’) – How to combine several outputs, as in scikit-learn.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
loss (float or Array) – A float, or one float64 value per output for
'raw_values'.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> y = np.array([3.0, -0.5, 2.0, 7.0], dtype=np.float32) >>> pred = np.array([2.5, 0.0, 2.0, 8.0], dtype=np.float32) >>> metrics.mean_absolute_error(y, pred) 0.5
- mojolearn.metrics.root_mean_squared_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', numeric_mode=None)[source]#
Root mean squared error, computed in float32.
Follows scikit-learn’s
root_mean_squared_error: the square root ofmean_squared_error, with the same input behavior. The squares can overflow toinfeven when the true RMSE would fit in float32.- Parameters:
y_true, y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Finite, nonempty float32 arrays of the same shape. A float64 or other non-float32 buffer raises
TypeError: cast it first. Python lists are converted to float32. NaN or infinity raisesValueError.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights, as in scikit-learn.
multioutput ({‘uniform_average’, ‘raw_values’} or array-like, default ‘uniform_average’) – How to combine several outputs, as in scikit-learn.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
loss (float or Array) – A float, or one float64 value per output for
'raw_values'.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> y = np.array([3.0, -0.5, 2.0, 7.0], dtype=np.float32) >>> pred = np.array([2.5, 0.0, 2.0, 8.0], dtype=np.float32) >>> rmse = metrics.root_mean_squared_error(y, pred)
- mojolearn.metrics.r2_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', force_finite=True, numeric_mode=None)[source]#
Coefficient of determination (R^2), computed in float32.
Follows scikit-learn’s
r2_score. When the targets are constant (total sum of squares 0), the defaultforce_finite=Truereturns 1.0 for a perfect prediction and 0.0 otherwise, as scikit-learn and cuML do.- Parameters:
y_true, y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Finite float32 targets and predictions of the same shape. float64 arrays raise
TypeError(for 1-D input, so do Python lists of floats): cast to float32 first. NaN or infinity raisesValueError.sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights, as in scikit-learn. They must be finite, non-negative and have a positive total.
multioutput ({‘uniform_average’, ‘raw_values’, ‘variance_weighted’} or array-like, default ‘uniform_average’) – How to combine the scores of several outputs, as in scikit-learn.
force_finite (bool, default True) – False returns scikit-learn’s raw values for constant targets (
nanor-inf).numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this call (
'fast'is Apple only). None uses the library default.
- Returns:
score (float or Array) – A float, or one float64 value per output for
'raw_values'.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> rng = np.random.default_rng(0) >>> y = rng.random(1000, dtype=np.float32) >>> pred = y + rng.normal(0, 0.1, 1000).astype(np.float32) >>> r2 = metrics.r2_score(y, pred)
- mojolearn.metrics.rand_score(labels_true, labels_pred)[source]#
Rand index: the fraction of sample pairs on which two clusterings agree.
Follows scikit-learn’s
rand_score. Labels are used as given; they do not need to be contiguous.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer cluster labels of the same length that fit in int32.
- Returns:
score (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> score = metrics.rand_score(np.array([0, 0, 1, 1], dtype=np.int32), ... np.array([1, 1, 0, 0], dtype=np.int32))
- mojolearn.metrics.silhouette_samples(X, labels, *, metric='euclidean', chunksize=None)[source]#
Silhouette coefficient of every sample.
The per-sample values that
silhouette_scoreaverages; parameters, input rules and errors are those ofsilhouette_score. A sample in a singleton cluster gets 0.0, as in scikit-learn.- Parameters:
X (array-like of shape (n_samples, n_features)) – Finite data, converted to float32 row-major.
labels (array-like of shape (n_samples,)) – Integer cluster labels that fit in int32.
metric ({‘euclidean’, ‘l2’}, default ‘euclidean’) – Both mean Euclidean distance.
chunksize (int or None, default None) – Accepted for compatibility with cuML; does not change the result.
- Returns:
scores (Array of shape (n_samples,), float32)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> labels = rng.integers(0, 4, 1000).astype(np.int32) >>> s = metrics.silhouette_samples(X, labels)
- mojolearn.metrics.silhouette_score(X, labels, *, metric='euclidean', sample_size=None, random_state=None, chunksize=None)[source]#
Mean silhouette coefficient over all samples.
Follows scikit-learn’s and cuML’s
silhouette_scorewith Euclidean distance. Values lie in[-1, 1]; higher means better-separated clusters.- Parameters:
X (array-like of shape (n_samples, n_features)) – Finite data, converted to float32 row-major. NaN or infinity raises
ValueError.labels (array-like of shape (n_samples,)) – Integer cluster labels that fit in int32; they are mapped onto
0..k-1. There must be at least 2 and at mostn_samples - 1distinct labels.metric ({‘euclidean’, ‘l2’}, default ‘euclidean’) – Both mean Euclidean distance. Any other metric raises
NotImplementedError.sample_size, random_state (None, default None) – scikit-learn’s subsampling is not supported; any other value raises
NotImplementedError. Subsample X yourself if you need it.chunksize (int or None, default None) – Accepted for compatibility with cuML (default 40000, must be at least 1). It does not change the result or the work done.
- Returns:
score (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> labels = rng.integers(0, 4, 1000).astype(np.int32) >>> s = metrics.silhouette_score(X, labels)
- mojolearn.metrics.trustworthiness(X, X_embedded, *, n_neighbors=5, metric='euclidean', batch_size=512)[source]#
Trustworthiness: how well an embedding preserves local neighborhoods.
Follows scikit-learn’s and cuML’s
trustworthinesswith Euclidean distance. Values lie in[0, 1]; 1 means every embedded neighbor is also a near neighbor in the original space.- Parameters:
X (array-like of shape (n_samples, n_features)) – Finite original data, converted to float32 row-major.
X_embedded (array-like of shape (n_samples, n_components)) – Finite embedding with the same number of rows, converted to float32.
n_neighbors (int, default 5) – Neighbors considered, as in cuML. Must be at least 1, less than
n_samples / 2, and at most 255; otherwise an error names it.metric ({‘euclidean’}, default ‘euclidean’) – Any other metric raises
NotImplementedError, as in cuML.batch_size (int, default 512) – Accepted for compatibility with cuML (must be at least 1). It does not change the result or the work done.
- Returns:
score (float)
- Raises:
ValueError – If X and X_embedded have different numbers of rows, contain NaN or infinity, or
batch_size < 1.
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> t = metrics.trustworthiness(X, X[:, :2], n_neighbors=5)
- mojolearn.metrics.v_measure_score(labels_true, labels_pred, *, beta=1.0)[source]#
V-measure: the weighted harmonic mean of homogeneity and completeness.
(1 + beta) * h * c / (beta * h + c). Follows scikit-learn’s and cuML’sv_measure_score. Labels are mapped onto0..k-1over their union first.- Parameters:
labels_true, labels_pred (array-like of shape (n_samples,)) – Integer labels of the same length that fit in int32.
beta (float, default 1.0) – Above 1 weights completeness more, below 1 weights homogeneity more.
- Returns:
score (float)
Examples
>>> import numpy as np >>> from mojolearn import metrics >>> v = metrics.v_measure_score(np.array([0, 0, 1, 1], dtype=np.int32), ... np.array([0, 0, 1, 2], dtype=np.int32))
- mojolearn.metrics.balanced_accuracy_score(y_true, y_pred, *, sample_weight=None, adjusted=False, numeric_mode=None)[source]#
scikit-learn 1.9
balanced_accuracy_score: the mean per-class recall over the classes present in y_true (a class with no true weight is dropped with sklearn’s warning), chance-adjusted whenadjusted.
- mojolearn.metrics.class_likelihood_ratios(y_true, y_pred, *, labels=None, sample_weight=None, replace_undefined_by=nan, numeric_mode=None)[source]#
scikit-learn 1.9
class_likelihood_ratios(binary targets): LR+ and LR- from the 2 x 2 (weighted) confusion matrix;replace_undefined_bynp.nan, 1.0 or {‘LR+’: v, ‘LR-’: v}.
- mojolearn.metrics.classification_report(y_true, y_pred, *, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn', numeric_mode=None)[source]#
scikit-learn 1.9
classification_reportfor binary / multiclass 1-D targets, from precision_recall_fscore_support.
- mojolearn.metrics.cohen_kappa_score(y1, y2, *, labels=None, weights=None, sample_weight=None, replace_undefined_by=nan, numeric_mode=None)[source]#
scikit-learn 1.9
cohen_kappa_score: the (weighted) confusion matrix overlabelson the device, the kappa in the binary64 epilogue; weights None, ‘linear’ or ‘quadratic’.
- mojolearn.metrics.fbeta_score(y_true, y_pred, *, beta, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn', numeric_mode=None)[source]#
scikit-learn 1.9
fbeta_score(binary / multiclass 1-D targets); see precision_recall_fscore_support.
- mojolearn.metrics.hamming_loss(y_true, y_pred, *, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
hamming_lossfor binary / multiclass 1-D targets: the (weighted) fraction of mismatched labels.
- mojolearn.metrics.jaccard_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn', numeric_mode=None)[source]#
scikit-learn 1.9
jaccard_score,tp / (tp + fp + fn)per label, for binary and multiclass 1-D targets; see precision_recall_fscore_support for the sums and the epilogue.
- mojolearn.metrics.matthews_corrcoef(y_true, y_pred, *, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
matthews_corrcoef(binary and multiclass): the covariance form over the per-class true / predicted sums and the trace.
- mojolearn.metrics.multilabel_confusion_matrix(y_true, y_pred, *, sample_weight=None, labels=None, samplewise=False, numeric_mode=None)[source]#
Per-label one-vs-rest 2x2 confusion matrices,
[[tn, fp], [fn, tp]].Follows scikit-learn’s
multilabel_confusion_matrixfor binary and multiclass 1-D targets (strings or integers). Unweighted counts are exact integers; withsample_weightthey are float64. Multilabel-indicator input andsamplewise=Trueraise an error.
- mojolearn.metrics.precision_recall_fscore_support(y_true, y_pred, *, beta=1.0, labels=None, pos_label=1, average=None, warn_for=('precision', 'recall', 'f-score'), sample_weight=None, zero_division='warn', numeric_mode=None)[source]#
Precision, recall, F-score and support for each label.
Follows scikit-learn’s
precision_recall_fscore_supportfor binary and multiclass 1-D targets.zero_divisionis'warn', 0.0, 1.0 ornp.nan.average='samples'(multilabel) raises an error.
- mojolearn.metrics.zero_one_loss(y_true, y_pred, *, normalize=True, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
zero_one_lossfor binary / multiclass 1-D targets.
- mojolearn.metrics.d2_absolute_error_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', numeric_mode=None)[source]#
scikit-learn 1.9
d2_absolute_error_score(d2_pinball_score at alpha 0.5).
- mojolearn.metrics.d2_pinball_score(y_true, y_pred, *, sample_weight=None, alpha=0.5, multioutput='uniform_average', numeric_mode=None)[source]#
scikit-learn 1.9
d2_pinball_score: the pinball loss against the (weighted, averaged) alpha-quantile of y_true per output, one program.
- mojolearn.metrics.d2_tweedie_score(y_true, y_pred, *, sample_weight=None, power=0, numeric_mode=None)[source]#
scikit-learn 1.9
d2_tweedie_score:1 - dev(y, p) / dev(y, avg y)in one program; a zero null deviance answers NaN (0/0) or -inf as numpy’s float division does.
- mojolearn.metrics.explained_variance_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', force_finite=True, numeric_mode=None)[source]#
scikit-learn 1.9
explained_variance_score:1 - Var(y - p) / Var(y)with (weighted) means, one program (the means, then the centered squares); force_finite=False returns scikit-learn’s NaN / -inf by value.
- mojolearn.metrics.max_error(y_true, y_pred, *, numeric_mode=None)[source]#
scikit-learn 1.9
max_error(single output).
- mojolearn.metrics.mean_absolute_percentage_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', numeric_mode=None)[source]#
scikit-learn 1.9
mean_absolute_percentage_error:|p - y| / max(|y|, eps)with numpy’s float64 epsilon, averaged (not multiplied by 100).
- mojolearn.metrics.mean_gamma_deviance(y_true, y_pred, *, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
mean_gamma_deviance(Tweedie power 2).
- mojolearn.metrics.mean_pinball_loss(y_true, y_pred, *, sample_weight=None, alpha=0.5, multioutput='uniform_average', numeric_mode=None)[source]#
scikit-learn 1.9
mean_pinball_loss(alpha is rounded to Float32).
- mojolearn.metrics.mean_poisson_deviance(y_true, y_pred, *, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
mean_poisson_deviance(Tweedie power 1).
- mojolearn.metrics.mean_squared_log_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', numeric_mode=None)[source]#
scikit-learn 1.9
mean_squared_log_error: the mean of(log1p(y) - log1p(p))^2(portable log1p, IDENTITY_PATHS row 51); values <= -1 are refused as scikit-learn refuses them.
- mojolearn.metrics.mean_tweedie_deviance(y_true, y_pred, *, sample_weight=None, power=0, numeric_mode=None)[source]#
scikit-learn 1.9
mean_tweedie_deviance(single output; the power is rounded to Float32; pow/log are the portable row-12 functions).
- mojolearn.metrics.median_absolute_error(y_true, y_pred, *, multioutput='uniform_average', sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
median_absolute_error: the per-output median of|y - p|(the weighted percentile at 50 with averaging when weighted, the same answer as numpy’s median when not), from a stable device sort.
- mojolearn.metrics.root_mean_squared_log_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', numeric_mode=None)[source]#
scikit-learn 1.9
root_mean_squared_log_error(per-output root, then averaged).
- mojolearn.metrics.auc(x, y)[source]#
Area under a curve by the trapezoid rule.
Follows scikit-learn’s
auc.xmust be monotonic. Lists and integer arrays are converted to float64 first. Computed on the GPU.
- mojolearn.metrics.average_precision_score(y_true, y_score, *, average='macro', pos_label=1, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
average_precision_score(binary and multiclass one-vs-rest; multilabel-indicator targets are refused by name).
- mojolearn.metrics.brier_score_loss(y_true, y_proba, *, sample_weight=None, pos_label=None, labels=None, scale_by_half='auto', numeric_mode=None)[source]#
scikit-learn 1.9
brier_score_loss(binary vector or multiclass matrix): the mean ofsum_c (onehot - p)^2, halved by default for the binary case.
- mojolearn.metrics.coverage_error(y_true, y_score, *, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
coverage_error(multilabel indicator y_true).
- mojolearn.metrics.d2_brier_score(y_true, y_proba, *, sample_weight=None, pos_label=None, labels=None, numeric_mode=None)[source]#
scikit-learn 1.9
d2_brier_score: one minus the Brier score over the Brier score of the (weighted) class frequencies.
- mojolearn.metrics.d2_log_loss_score(y_true, y_proba=None, *, sample_weight=None, labels=None, numeric_mode=None)[source]#
scikit-learn 1.9
d2_log_loss_score: one minus the log loss over the log loss of the (weighted) class frequencies.
- mojolearn.metrics.dcg_score(y_true, y_score, *, k=None, log_base=2, sample_weight=None, ignore_ties=False, numeric_mode=None)[source]#
scikit-learn 1.9
dcg_score: tie-averaged gains by default; ignore_ties=True ranks tied scores by DESCENDING column index (numpy’sargsort(...)[::-1]there, whose tie order its unstable sort leaves unspecified; this one is fixed).
- mojolearn.metrics.det_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=False, numeric_mode=None)[source]#
Detection error tradeoff curve for binary targets.
Follows scikit-learn’s
det_curve. Returnsfpr,fnrandthresholds, computed on the GPU. A zero class weight raisesZeroDivisionError.
- mojolearn.metrics.hinge_loss(y_true, pred_decision, *, labels=None, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
hinge_loss: binary (labels mapped to -1 / +1, the larger class positive) or Crammer-Singer multiclass margins.
- mojolearn.metrics.label_ranking_average_precision_score(y_true, y_score, *, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
label_ranking_average_precision_score.
- mojolearn.metrics.label_ranking_loss(y_true, y_score, *, sample_weight=None, numeric_mode=None)[source]#
scikit-learn 1.9
label_ranking_loss(tied scores count as misordered).
- mojolearn.metrics.ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False, numeric_mode=None)[source]#
Normalized discounted cumulative gain.
Follows scikit-learn’s
ndcg_score: each row’s DCG divided by its ideal DCG (0 when the row has no relevant item), averaged over rows, weighted whensample_weightis given. Computed on the GPU.
- mojolearn.metrics.roc_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=True, numeric_mode=None)[source]#
Receiver operating characteristic curve for binary targets.
Follows scikit-learn’s
roc_curve. Returnsfpr,tprandthresholdsas float64 arrays; the first threshold is+inf. Computed on the GPU.
- mojolearn.metrics.top_k_accuracy_score(y_true, y_score, *, k=2, normalize=True, sample_weight=None, labels=None, numeric_mode=None)[source]#
scikit-learn 1.9
top_k_accuracy_score: a hit when the true class is among the k largest scores, ties ordered as numpy’s stable argsort reversed (the larger class index first).
- mojolearn.metrics.adjusted_mutual_info_score(labels_true, labels_pred, *, average_method='arithmetic', numeric_mode=None)[source]#
scikit-learn 1.9
adjusted_mutual_info_score: (MI - E[MI]) / (mean(H) - E[MI]) with scikit-learn’s epsilon guards.
- mojolearn.metrics.calinski_harabasz_score(X, labels, *, numeric_mode=None)[source]#
scikit-learn 1.9
calinski_harabasz_score: the between- over the within-cluster dispersion, scaled by (n - k) / (k - 1), in one device program (_cluster_score).
- mojolearn.metrics.contingency_matrix(labels_true, labels_pred, *, eps=None, sparse=False, dtype='int64', numeric_mode=None)[source]#
scikit-learn 1.9
contingency_matrix(dense): exact counts from the device grouping, rows the sorted classes, columns the sorted clusters;epsadds a constant and makes the result Float64. sparse=True is NOT IMPLEMENTED (no sparse container in this package).
- mojolearn.metrics.davies_bouldin_score(X, labels, *, numeric_mode=None)[source]#
scikit-learn 1.9
davies_bouldin_score: the mean over clusters of the worst (s_i + s_j) / d(c_i, c_j), in one device program (_cluster_score).