nirs4all.core.metrics module
Evaluator module - Generic functions for calculating metrics
This module provides: - eval(y_true, y_pred, metric): Calculate a specific metric - eval_multi(y_true, y_pred, task_type): Calculate all metrics for a task type
Supports regression, binary classification, and multiclass classification metrics using sklearn, scipy, and other standard libraries.
- nirs4all.core.metrics.abbreviate_metric(metric: str) str[source]
Convert metric name to abbreviated form.
- Parameters:
metric – Full metric name (e.g., ‘balanced_accuracy’).
- Returns:
Abbreviated metric name (e.g., ‘BalAcc’).
- nirs4all.core.metrics.eval(y_true: ndarray, y_pred: ndarray, metric: str | List[str]) float | Dict[str, float][source]
Calculate a specific metric for given predictions.
- Parameters:
y_true – True target values
y_pred – Predicted values
metric – Metric name (e.g., ‘mse’, ‘accuracy’, ‘f1’, ‘r2’)
- Returns:
Calculated metric value
- Return type:
- Raises:
ValueError – If metric is not supported or calculation fails
- nirs4all.core.metrics.eval_list(y_true: ndarray, y_pred: ndarray, metrics: list) list[source]
Calculate multiple metrics and return their scores as a list.
- Parameters:
y_true – True target values
y_pred – Predicted values
metrics – List of metric names to calculate
- Returns:
List of calculated metric values in the same order as input metrics
- Return type:
Example
scores = eval_list(y_true, y_pred, [‘mse’, ‘r2’, ‘mae’]) # Returns: [0.022, 0.989, 0.14]
- nirs4all.core.metrics.eval_multi(y_true: ndarray, y_pred: ndarray, task_type: str) Dict[str, float][source]
Calculate all relevant metrics for a given task type.
- Parameters:
y_true – True target values
y_pred – Predicted values
task_type – Type of task (‘regression’, ‘binary_classification’, ‘multiclass_classification’)
- Returns:
Dictionary of metric names and their values
- Return type:
- Raises:
ValueError – If task_type is not supported
- nirs4all.core.metrics.get_available_metrics(task_type: str) list[source]
Get list of available metrics for a given task type.
- Parameters:
task_type – Type of task (‘regression’, ‘binary_classification’, ‘multiclass_classification’)
- Returns:
List of available metric names
- nirs4all.core.metrics.get_default_metrics(task_type: str) list[source]
Get list of default/essential metrics for a given task type. This is a subset of available metrics, focusing on the most commonly used ones.
- Parameters:
task_type – Type of task (‘regression’, ‘binary_classification’, ‘multiclass_classification’)
- Returns:
List of default metric names
- nirs4all.core.metrics.get_stats(y: ndarray) Dict[str, float][source]
Calculate descriptive statistics for target values.
- Parameters:
y – Target values
- Returns:
Dictionary of statistical measures
- Return type:
Example
stats = get_stats(y_true) # Returns: {‘nsample’: 100, ‘mean’: 2.5, ‘median’: 2.4, ‘min’: 0.1, ‘max’: 5.0, ‘sd’: 1.2, ‘cv’: 0.48}