Metrics¶
tinax.metrics computes validated binary-classification and regression metrics from explicit one-dimensional NumPy arrays, rejecting non-array inputs, shape mismatches, non-finite values, and invalid label or probability values at the public boundary instead of returning silently wrong numbers.
import numpy as np
from tinax.metrics import best_f1_threshold, binary_metrics
labels = np.array([0, 0, 1, 1])
probabilities = np.array([0.1, 0.4, 0.6, 0.9])
threshold = best_f1_threshold(labels, probabilities)
report = binary_metrics(labels, probabilities, threshold=threshold)
print(report.f1)
BinaryMetrics
dataclass
¶
Binary-classification metrics at one decision threshold.
Attributes:
| Name | Type | Description |
|---|---|---|
accuracy |
float
|
Fraction of predictions that match the label. |
f1 |
float
|
Harmonic mean of precision and recall, |
precision |
float
|
Fraction of predicted positives that are true positives, |
recall |
float
|
Fraction of actual positives that are predicted positives, |
threshold |
float
|
Decision threshold used to produce the predictions. |
RegressionMetrics
dataclass
¶
Regression error metrics over finite target and prediction arrays.
Attributes:
| Name | Type | Description |
|---|---|---|
rmse |
float
|
Root mean squared error. |
mae |
float
|
Mean absolute error. |
r2 |
float
|
Coefficient of determination, |
best_f1_threshold
¶
Find the smallest probability threshold that maximizes binary F1.
Candidate thresholds are the unique values in probabilities, and each
candidate treats every entry with probabilities >= candidate as positive.
The sweep runs in O(n log n) time and breaks F1 ties toward the smallest
candidate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
ndarray
|
One-dimensional array of 0/1 labels. Booleans are accepted. |
required |
probabilities
|
ndarray
|
One-dimensional array of finite probabilities in |
required |
Returns:
| Type | Description |
|---|---|
float
|
The candidate threshold with the highest F1 score as a plain |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an argument is not a |
ValueError
|
If the arrays have different shapes, are not one-dimensional, are empty, or violate the label or probability value contracts. |
binary_metrics
¶
binary_metrics(
labels: ndarray,
probabilities: ndarray,
*,
threshold: float = 0.5,
) -> BinaryMetrics
Compute binary-classification metrics at an explicit decision threshold.
Entries with probabilities >= threshold are predicted positive. Precision,
recall, and F1 fall back to 0.0 when their denominator is zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
ndarray
|
One-dimensional array of 0/1 labels. Booleans are accepted. |
required |
probabilities
|
ndarray
|
One-dimensional array of finite probabilities in |
required |
threshold
|
float
|
Decision threshold in |
0.5
|
Returns:
| Type | Description |
|---|---|
BinaryMetrics
|
A |
BinaryMetrics
|
validated threshold. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the arrays have different shapes, are not one-dimensional, are
empty, or violate the label or probability value contracts, or if
|
regression_metrics
¶
regression_metrics(
targets: ndarray, predictions: ndarray
) -> RegressionMetrics
Compute RMSE, MAE, and R2 for finite regression targets and predictions.
r2 is 1 - SSE / SST and 0.0 when the targets have zero variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
ndarray
|
One-dimensional array of finite target values. |
required |
predictions
|
ndarray
|
One-dimensional array of finite predictions with the same shape
as |
required |
Returns:
| Type | Description |
|---|---|
RegressionMetrics
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the arrays have different shapes, are not one-dimensional, are empty, or contain non-finite values. |