ridge_r2

ridge_r2

Functions

Name Description
ridge_get_r2 Calculate the coefficient of determination (R² score) for a regression model.

ridge_get_r2

ridge_r2.ridge_get_r2(y_true, y_pred)

Calculate the coefficient of determination (R² score) for a regression model.

The R² score measures how well the regression line fits the data, representing the proportion of variance in the dependent variable that is predictable from the independent variable(s). Values range from -∞ to 1, where 1 indicates perfect prediction.

This function calcuates the ratio of the Residual Sum of Squares (RSS) to the Total Sum of Squares (TSS), as a measure of how well the data performs compared to the mean.

Parameters

Name Type Description Default
y_true array-like of shape (n_samples,) True target values (observed data points). required
y_pred array-like of shape (n_samples,) Predicted values from the regression model. required

Returns

Name Type Description
r2 float The R² score. A value of 1.0 indicates perfect prediction, 0.0 indicates the model performs no better than predicting the mean, and negative values indicate the model performs worse than a horizontal line at the mean.

Notes

R² is calculated as: R² = 1 - (SS_res / SS_tot) where: SS_res = Σ(y_true - y_pred)² (residual sum of squares) SS_tot = Σ(y_true - ȳ)² (total sum of squares)

Examples

>>> y_true = np.array([3, -0.5, 2, 7])
>>> y_pred = np.array([2.5, 0.0, 2, 8])
>>> ridge_get_r2(y_true, y_pred)
0.9486081370449679