metrics #
VSL Metrics Module
The vsl.metrics module provides evaluation metrics for machine learning models, including classification and regression metrics.
Features
Classification Metrics
- accuracy_score: Classification accuracy
- precision_score: Precision (positive predictive value)
- recall_score: Recall (sensitivity, true positive rate)
- f1_score: F1 score (harmonic mean of precision and recall)
- confusion_matrix: Confusion matrix for binary classification
- roc_curve: Receiver Operating Characteristic curve
- roc_auc_score: Area Under the ROC Curve
- precision_recall_curve: Precision-Recall curve
- average_precision_score: Average precision score
- log_loss: Logistic loss (cross-entropy)
- gini_coefficient: Gini coefficient
- ks_statistic: Kolmogorov-Smirnov statistic
Regression Metrics
- mean_squared_error: Mean Squared Error (MSE)
- root_mean_squared_error: Root Mean Squared Error (RMSE)
- mean_absolute_error: Mean Absolute Error (MAE)
- r2_score: Coefficient of determination (R²)
- mean_absolute_percentage_error: MAPE
- explained_variance_score: Explained variance
- max_error: Maximum absolute error
- median_absolute_error: Median absolute error
- mean_squared_log_error: Mean squared logarithmic error
Quick Start
Classification Metrics
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]
y_pred := [1.0, 0.0, 0.0, 0.0, 1.0, 1.0]
// Basic metrics
acc := metrics.accuracy_score(y_true, y_pred)!
prec := metrics.precision_score(y_true, y_pred)!
rec := metrics.recall_score(y_true, y_pred)!
f1 := metrics.f1_score(y_true, y_pred)!
println('Accuracy: ${acc}')
println('Precision: ${prec}')
println('Recall: ${rec}')
println('F1 Score: ${f1}')
// Confusion matrix
cm := metrics.confusion_matrix(y_true, y_pred)!
println('Confusion Matrix: ${cm}')
// [[TN, FP], [FN, TP]]
ROC Curve and AUC
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0]
y_score := [0.9, 0.8, 0.3, 0.1] // prediction probabilities
// Compute ROC curve
roc := metrics.roc_curve(y_true, y_score)!
println('FPR: ${roc.fpr}')
println('TPR: ${roc.tpr}')
// Compute AUC
auc := metrics.roc_auc_score(y_true, y_score)!
println('AUC: ${auc}')
Precision-Recall Curve
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0]
y_score := [0.9, 0.8, 0.3, 0.1]
pr := metrics.precision_recall_curve(y_true, y_score)!
println('Precision: ${pr.precision}')
println('Recall: ${pr.recall}')
ap := metrics.average_precision_score(y_true, y_score)!
println('Average Precision: ${ap}')
Gini and KS Statistics
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0]
y_score := [0.9, 0.8, 0.2, 0.1]
gini := metrics.gini_coefficient(y_true, y_score)!
println('Gini: ${gini}')
ks := metrics.ks_statistic(y_true, y_score)!
println('KS Statistic: ${ks}')
Regression Metrics
import vsl.metrics
y_true := [3.0, -0.5, 2.0, 7.0]
y_pred := [2.5, 0.0, 2.0, 8.0]
mse := metrics.mean_squared_error(y_true, y_pred)!
rmse := metrics.root_mean_squared_error(y_true, y_pred)!
mae := metrics.mean_absolute_error(y_true, y_pred)!
r2 := metrics.r2_score(y_true, y_pred)!
println('MSE: ${mse}')
println('RMSE: ${rmse}')
println('MAE: ${mae}')
println('R²: ${r2}')
API Reference
Classification Functions
| Function | Description |
|---|---|
confusion_matrix(y_true, y_pred) |
Returns [[TN, FP], [FN, TP]] |
accuracy_score(y_true, y_pred) |
(TP + TN) / total |
precision_score(y_true, y_pred) |
TP / (TP + FP) |
recall_score(y_true, y_pred) |
TP / (TP + FN) |
f1_score(y_true, y_pred) |
2 * P * R / (P + R) |
roc_curve(y_true, y_score) |
Returns FPR, TPR, thresholds |
roc_auc_score(y_true, y_score) |
Area under ROC curve |
precision_recall_curve(y_true, y_score) |
Returns precision, recall, thresholds |
average_precision_score(y_true, y_score) |
AP from predictions |
log_loss(y_true, y_pred) |
Cross-entropy loss |
gini_coefficient(y_true, y_score) |
2 * AUC - 1 |
ks_statistic(y_true, y_score) |
Max difference between CDFs |
auc(x, y) |
Area under curve (trapezoidal) |
Regression Functions
| Function | Description |
|---|---|
mean_squared_error(y_true, y_pred) |
Σ(y - ŷ)² / n |
root_mean_squared_error(y_true, y_pred) |
√MSE |
mean_absolute_error(y_true, y_pred) |
Σ|y - ŷ| / n |
r2_score(y_true, y_pred) |
1 - SS_res / SS_tot |
mean_absolute_percentage_error(y_true, y_pred) |
MAPE in % |
explained_variance_score(y_true, y_pred) |
1 - Var(residuals) / Var(y) |
max_error(y_true, y_pred) |
max(|y - ŷ|) |
median_absolute_error(y_true, y_pred) |
median(|y - ŷ|) |
mean_squared_log_error(y_true, y_pred) |
MSLE (for non-negative) |
Integration with ML Models
import vsl.metrics
import vsl.ml
// Train a model
mut model := ml.LogReg.new(mut data, 'my_model')
model.train(ml.LogRegTrainConfig{})
// Make predictions
mut predictions := []f64{}
for i in 0 .. test_data.nb_samples {
x := test_data.x.get_row(i)
predictions << model.predict(x)
}
// Evaluate
accuracy := metrics.accuracy_score(test_data.y, predictions)!
auc := metrics.roc_auc_score(test_data.y, predictions)!
println('Test Accuracy: ${accuracy}')
println('Test AUC: ${auc}')
Visualization with Metrics
import vsl.metrics
import vsl.plot
// Compute ROC curve
roc := metrics.roc_curve(y_true, y_score)!
auc_val := metrics.roc_auc_score(y_true, y_score)!
// Plot ROC curve
mut plt := plot.Plot.new()
plt.scatter(
x: roc.fpr
y: roc.tpr
mode: 'lines'
name:'ROC (AUC = ${auc_val:.3f})'
)
plt.scatter(
x: [0.0, 1.0]
y: [0.0, 1.0]
mode: 'lines'
name: 'Random'
)
plt.layout(
title: 'ROC Curve'
xaxis: plot.Axis{
title: plot.AxisTitle{
text: 'False Positive Rate'
}
}
yaxis: plot.Axis{
title: plot.AxisTitle{
text: 'True Positive Rate'
}
}
)
plt.show()!
Notes
- For binary classification, labels should be 0.0 or 1.0 (or values that threshold at 0.5)
y_scoreparameters expect probability scores, not class labels- R² can be negative for predictions worse than the mean
- MAPE and MSLE have specific requirements for input values
See Also
fn accuracy_score #
fn accuracy_score(y_true []f64, y_pred []f64) !f64
accuracy_score computes the accuracy classification score
fn auc #
fn auc(x []f64, y []f64) !f64
auc computes Area Under the Curve using the trapezoidal rule
fn average_precision_score #
fn average_precision_score(y_true []f64, y_score []f64) !f64
average_precision_score computes average precision from prediction scores
fn confusion_matrix #
fn confusion_matrix(y_true []f64, y_pred []f64) ![][]int
confusion_matrix computes the confusion matrix for binary classification. Returns a 2x2 matrix: [[TN, FP], [FN, TP]]
fn explained_variance_score #
fn explained_variance_score(y_true []f64, y_pred []f64) !f64
explained_variance_score computes the explained variance regression score
fn f1_score #
fn f1_score(y_true []f64, y_pred []f64) !f64
f1_score computes the F1 score: 2 * (precision * recall) / (precision + recall)
fn gini_coefficient #
fn gini_coefficient(y_true []f64, y_score []f64) !f64
gini_coefficient computes the Gini coefficient from predictions
fn ks_statistic #
fn ks_statistic(y_true []f64, y_score []f64) !f64
ks_statistic computes the Kolmogorov-Smirnov statistic
fn log_loss #
fn log_loss(y_true []f64, y_pred []f64) !f64
log_loss computes the logistic loss (cross-entropy loss)
fn max_error #
fn max_error(y_true []f64, y_pred []f64) !f64
max_error computes the maximum absolute error
fn mean_absolute_error #
fn mean_absolute_error(y_true []f64, y_pred []f64) !f64
mean_absolute_error computes the mean absolute error (MAE)
fn mean_absolute_percentage_error #
fn mean_absolute_percentage_error(y_true []f64, y_pred []f64) !f64
mean_absolute_percentage_error computes MAPE MAPE = (1/n) * Σ|y_true - y_pred| / |y_true| * 100
fn mean_squared_error #
fn mean_squared_error(y_true []f64, y_pred []f64) !f64
mean_squared_error computes the mean squared error (MSE)
fn mean_squared_log_error #
fn mean_squared_log_error(y_true []f64, y_pred []f64) !f64
mean_squared_log_error computes the mean squared logarithmic error Only valid for non-negative values
fn median_absolute_error #
fn median_absolute_error(y_true []f64, y_pred []f64) !f64
median_absolute_error computes the median absolute error
fn precision_recall_curve #
fn precision_recall_curve(y_true []f64, y_score []f64) !PrecisionRecallResult
precision_recall_curve computes precision-recall pairs for different thresholds
fn precision_score #
fn precision_score(y_true []f64, y_pred []f64) !f64
precision_score computes the precision: tp / (tp + fp)
fn r2_score #
fn r2_score(y_true []f64, y_pred []f64) !f64
r2_score computes the coefficient of determination (R² score) R² = 1 - SS_res / SS_tot where SS_res = Σ(y_true - y_pred)² and SS_tot = Σ(y_true - mean(y_true))²
fn recall_score #
fn recall_score(y_true []f64, y_pred []f64) !f64
recall_score computes the recall: tp / (tp + fn)
fn roc_auc_score #
fn roc_auc_score(y_true []f64, y_score []f64) !f64
roc_auc_score computes Area Under the ROC Curve using the trapezoidal rule
fn roc_curve #
fn roc_curve(y_true []f64, y_score []f64) !RocCurveResult
roc_curve computes Receiver Operating Characteristic (ROC) curve
fn root_mean_squared_error #
fn root_mean_squared_error(y_true []f64, y_pred []f64) !f64
root_mean_squared_error computes the root mean squared error (RMSE)
struct PrecisionRecallResult #
struct PrecisionRecallResult {
pub:
precision []f64 // precision values
recall []f64 // recall values
thresholds []f64 // thresholds used
}
PrecisionRecallResult holds the result of precision-recall curve computation
struct RocCurveResult #
struct RocCurveResult {
pub:
fpr []f64 // false positive rates
tpr []f64 // true positive rates
thresholds []f64 // thresholds used
}
RocCurveResult holds the result of ROC curve computation
- README
- fn accuracy_score
- fn auc
- fn average_precision_score
- fn confusion_matrix
- fn explained_variance_score
- fn f1_score
- fn gini_coefficient
- fn ks_statistic
- fn log_loss
- fn max_error
- fn mean_absolute_error
- fn mean_absolute_percentage_error
- fn mean_squared_error
- fn mean_squared_log_error
- fn median_absolute_error
- fn precision_recall_curve
- fn precision_score
- fn r2_score
- fn recall_score
- fn roc_auc_score
- fn roc_curve
- fn root_mean_squared_error
- struct PrecisionRecallResult
- struct RocCurveResult