Metrics Reference
All 20 Rust-backed regression, classification, and clustering metrics in ochreml.metrics.
ochreml.metrics implements 20 scikit-learn-compatible evaluation metrics backed by Rust (with silhouette_score accelerated by the Hardware Backend pairwise_l2_sq kernel). (^▽^)
Regression Metrics (6)
All 6 regression metrics accept 1D or 2D ((n_samples, n_outputs)) y_true and y_pred, optional sample_weight, and multioutput ("uniform_average", "raw_values", or "variance_weighted" where supported). Returning "raw_values" yields a 1D float64 NumPy array; otherwise returns a Python float.
| Function | Signature | Description |
|---|---|---|
mean_squared_error | (y_true, y_pred, sample_weight=None, multioutput="uniform_average") | Mean squared error: mean((y_true - y_pred)^2) |
root_mean_squared_error | (y_true, y_pred, sample_weight=None, multioutput="uniform_average") | Square root of MSE |
mean_absolute_error | (y_true, y_pred, sample_weight=None, multioutput="uniform_average") | Mean absolute error: `mean( |
mean_absolute_percentage_error | (y_true, y_pred, sample_weight=None, multioutput="uniform_average") | Mean absolute percentage error (epsilon-guarded for zero targets) |
r2_score | (y_true, y_pred, sample_weight=None, multioutput="uniform_average") | Coefficient of determination R^2 = 1 - SS_res / SS_tot |
explained_variance_score | (y_true, y_pred, sample_weight=None, multioutput="uniform_average") | Explained variance 1 - Var(y_true - y_pred) / Var(y_true) |
from ochreml.metrics import (
mean_squared_error,
root_mean_squared_error,
mean_absolute_error,
mean_absolute_percentage_error,
r2_score,
explained_variance_score,
)
mse = mean_squared_error(y_true, y_pred)
rmse = root_mean_squared_error(y_true, y_pred)
mae = mean_absolute_error(y_true, y_pred)
mape = mean_absolute_percentage_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred)
ev = explained_variance_score(y_true, y_pred)Classification Metrics (9)
Classification metrics accept 1D integer, boolean, or string labels and run in Rust via zero-copy integer encoding.
accuracy_score
accuracy_score(y_true, y_pred, normalize=True, sample_weight=None) -> float | intReturns fraction of correct predictions when normalize=True, or exact count (int when sample_weight=None) when normalize=False.
confusion_matrix
confusion_matrix(y_true, y_pred, labels=None, sample_weight=None, normalize=None) -> np.ndarrayReturns (n_classes, n_classes) matrix (int64 when unweighted and normalize=None, float64 otherwise). normalize supports None, "true", "pred", or "all".
precision_recall_fscore_support
precision_recall_fscore_support(
y_true,
y_pred,
beta=1.0,
labels=None,
pos_label=1,
average=None,
sample_weight=None,
zero_division=0.0,
) -> tupleComputes (precision, recall, fbeta_score, support). When average=None, returns per-class NumPy arrays; when average is "binary", "micro", "macro", or "weighted", returns (float, float, float, None).
precision_score, recall_score, f1_score
precision_score(y_true, y_pred, labels=None, pos_label=1, average="binary", sample_weight=None, zero_division=0.0)
recall_score(y_true, y_pred, labels=None, pos_label=1, average="binary", sample_weight=None, zero_division=0.0)
f1_score(y_true, y_pred, labels=None, pos_label=1, average="binary", sample_weight=None, zero_division=0.0)Convenience wrappers over precision_recall_fscore_support with beta=1.0 and default average="binary".
roc_auc_score
roc_auc_score(
y_true,
y_score,
average="macro",
sample_weight=None,
multi_class="raise",
labels=None,
) -> float | np.ndarray- Binary: Accepts 1D scores/probabilities or 2-column probability matrix (uses column 1).
- Multiclass: Pass 2D
(n_samples, n_classes)probabilities withmulti_class="ovr"or"ovo"andaveragein{"macro", "weighted", None}.
log_loss
log_loss(
y_true,
y_pred,
normalize=True,
sample_weight=None,
labels=None,
) -> floatComputes binary or multiclass cross-entropy loss. Accepts 1D positive-class probabilities or 2D (n_samples, n_classes) probability matrices. Returns mean loss when normalize=True or total loss when normalize=False.
classification_report
classification_report(
y_true,
y_pred,
labels=None,
target_names=None,
sample_weight=None,
digits=2,
output_dict=False,
zero_division=0.0,
) -> str | dict[str, Any]Formats per-class precision, recall, F1-score, support, overall accuracy, macro average, and weighted average as a tabular string (or nested dictionary when output_dict=True).
Clustering Metrics (5)
Clustering metrics include three internal (unsupervised) indices and two external (ground-truth comparison) indices.
silhouette_score
silhouette_score(
X,
labels,
metric="euclidean",
sample_size=None,
random_state=None,
device="auto",
) -> floatComputes the mean Silhouette Coefficient in [-1, 1] (higher is better). For metric="euclidean", pairwise distances are computed on the target device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0") via Hardware Backend pairwise_l2_sq. Optional sample_size and random_state subsample rows before scoring.
davies_bouldin_score
davies_bouldin_score(X, labels) -> floatComputes the Davies-Bouldin index (lower is better; 0.0 is ideal).
calinski_harabasz_score
calinski_harabasz_score(X, labels) -> floatComputes the Calinski-Harabasz Variance Ratio Criterion (higher is better).
adjusted_rand_score
adjusted_rand_score(labels_true, labels_pred) -> floatComputes the Rand Index adjusted for chance in [-1, 1] (1.0 for identical clusterings up to permutation, ~0.0 for random labeling).
normalized_mutual_info_score
normalized_mutual_info_score(
labels_true,
labels_pred,
average_method="arithmetic",
) -> floatComputes Normalized Mutual Information in [0, 1] between two clusterings (average_method supports "arithmetic", "geometric", "min", "max").
from ochreml.metrics import (
silhouette_score,
davies_bouldin_score,
calinski_harabasz_score,
adjusted_rand_score,
normalized_mutual_info_score,
)
sil = silhouette_score(X, pred_labels, device="auto")
db = davies_bouldin_score(X, pred_labels)
ch = calinski_harabasz_score(X, pred_labels)
ari = adjusted_rand_score(true_labels, pred_labels)
nmi = normalized_mutual_info_score(true_labels, pred_labels)Pipelines, Composition & Meta-Estimators
Pipeline, ColumnTransformer, TransformedTargetRegressor, multiclass/multioutput wrappers, and bagging/voting/stacking meta-ensembles.
Architecture & Lazy Imports
How OchreML's Backend<'a> enum dispatch, E1-E5 and N1-N6 primitives, zero-copy PyBuffer, and PEP 562 lazy imports work.