Skip to main content

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.

MSE(y,y^)=1ni=1n(yiy^i)2\text{MSE}(y, \hat{y}) = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2

Root Mean Squared Error (RMSE)

The square root of MSE, expressed in the same units as the target variable.

RMSE(y,y^)=MSE(y,y^)\text{RMSE}(y, \hat{y}) = \sqrt{\text{MSE}(y, \hat{y})}

Mean Absolute Error (MAE)

The average of absolute differences. Less sensitive to outliers than MSE.

MAE(y,y^)=1ni=1nyiy^i\text{MAE}(y, \hat{y}) = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i|

Coefficient of Determination (R2R^2)

Measures the proportion of variance in the target explained by the model. A score of 1.01.0 indicates perfect prediction; 0.00.0 corresponds to always predicting the mean yˉ\bar{y}.

R2(y,y^)=1i=1n(yiy^i)2i=1n(yiyˉ)2R^2(y, \hat{y}) = 1 - \frac{\sum_{i=1}^{n} (y_i - \hat{y}_i)^2}{\sum_{i=1}^{n} (y_i - \bar{y})^2}

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);
FunctionDescription
mean_squared_errorMean of squared errors
root_mean_squared_errorSquare root of MSE
mean_absolute_errorMean of absolute errors
r2_scoreCoefficient 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

Accuracy=TP+TNn\text{Accuracy} = \frac{\text{TP} + \text{TN}}{n}

Precision

Fraction of predicted positives that are truly positive. High precision means few false alarms.

Precision=TPTP+FP\text{Precision} = \frac{\text{TP}}{\text{TP} + \text{FP}}

Recall

Fraction of actual positives that are correctly identified. High recall means few missed positives.

Recall=TPTP+FN\text{Recall} = \frac{\text{TP}}{\text{TP} + \text{FN}}

F1 Score

The harmonic mean of precision and recall, balancing both concerns equally.

F1=2PrecisionRecallPrecision+Recall=2TP2TP+FP+FNF_1 = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}} = \frac{2\,\text{TP}}{2\,\text{TP} + \text{FP} + \text{FN}}
#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);
FunctionDescription
accuracy_scoreFraction of correct predictions
precision_scorePrecision (positive predictive value)
recall_scoreRecall (sensitivity)
f1_scoreHarmonic mean of precision and recall
confusion_matrixN×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";