Metrics
Evaluation metrics for quantifying model performance on regression and classification tasks.
Regression Metrics
Mean Squared Error (MSE)
The average of squared differences between predictions and true values. Penalizes large errors quadratically.
Root Mean Squared Error (RMSE)
The square root of MSE, expressed in the same units as the target variable.
Mean Absolute Error (MAE)
The average of absolute differences. Less sensitive to outliers than MSE.
Coefficient of Determination ()
Measures the proportion of variance in the target explained by the model. A score of indicates perfect prediction; corresponds to always predicting the mean .
The score can be negative if the model is worse than predicting the mean.
#include <Skigen/Metrics>
Skigen::Metrics::mean_squared_error(y_true, y_pred);
Skigen::Metrics::root_mean_squared_error(y_true, y_pred);
Skigen::Metrics::mean_absolute_error(y_true, y_pred);
Skigen::Metrics::r2_score(y_true, y_pred);
| Function | Description |
|---|---|
mean_squared_error | Mean of squared errors |
root_mean_squared_error | Square root of MSE |
mean_absolute_error | Mean of absolute errors |
r2_score | Coefficient of determination |
Classification Metrics
All binary classification metrics below are defined in terms of the confusion matrix entries:
- TP (True Positives): correctly predicted positive
- TN (True Negatives): correctly predicted negative
- FP (False Positives): negative samples incorrectly predicted as positive
- FN (False Negatives): positive samples incorrectly predicted as negative
Accuracy
Precision
Fraction of predicted positives that are truly positive. High precision means few false alarms.
Recall
Fraction of actual positives that are correctly identified. High recall means few missed positives.
F1 Score
The harmonic mean of precision and recall, balancing both concerns equally.
#include <Skigen/Metrics>
Skigen::Metrics::accuracy_score(y_true, y_pred);
Skigen::Metrics::precision_score(y_true, y_pred);
Skigen::Metrics::recall_score(y_true, y_pred);
Skigen::Metrics::f1_score(y_true, y_pred);
auto cm = Skigen::Metrics::confusion_matrix(y_true, y_pred);
| Function | Description |
|---|---|
accuracy_score | Fraction of correct predictions |
precision_score | Precision (positive predictive value) |
recall_score | Recall (sensitivity) |
f1_score | Harmonic mean of precision and recall |
confusion_matrix | N×N confusion matrix |
Example
#include <Skigen/LinearModel>
#include <Skigen/Metrics>
#include <Skigen/ModelSelection>
auto [X_train, X_test, y_train, y_test] = Skigen::train_test_split(X, y);
Skigen::LinearRegression model;
model.fit(X_train, y_train);
auto y_pred = model.predict(X_test);
std::cout << "MSE: " << Skigen::Metrics::mean_squared_error(y_test, y_pred) << "\n";
std::cout << "R²: " << Skigen::Metrics::r2_score(y_test, y_pred) << "\n";