Skip to main content

Ridge

#include <Skigen/LinearModel>

template <typename Scalar = double>
class Skigen::Ridge

Linear least squares with L2 regularization.

Minimizes the objective function:

yXw22+αw22\|y - Xw\|_2^2 + \alpha \|w\|_2^2

This model solves a regression model where the loss function is the linear least squares function and regularization is given by the L2-norm. Also known as Ridge Regression or Tikhonov regularization.

Mirrors sklearn.linear_model.Ridge.

Read more in the User Guide.


Attributes:

  • coef_matrix : MatrixType Coefficient matrix of shape (n_targets, n_features).

  • intercept_vector : VectorType Intercept vector of length n_targets.

  • n_targets : int


Methods

SKIGEN_PARAMS()

Construct a Ridge estimator.

Parameters:

  • alpha Constant that multiplies the L2 term (Scalar, default 1). Controls regularization strength. alpha = 0 is equivalent to ordinary least squares (use LinearRegression instead for numerical stability).

  • fit_intercept Whether the intercept should be estimated (bool, default true). If false, the data is assumed to be already centered.


fit(X, y)


predict(X)

Predict using the linear model.

Computes y^=Xw+b\hat{y} = X w + b where ww and bb are the fitted coefficients and intercept.

Parameters:

  • X : MatrixType Sample matrix of shape (n_samples, n_features).

Returns:

  • result : VectorType Predicted values of shape (n_samples,).

Throws:

  • std::runtime_error — if the model has not been fitted.

score(X, y)

Return the R2R^2 coefficient of determination on test data.

R2=1(yiy^i)2(yiyˉ)2R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}. Best possible score is 1.0; it can be negative if the model is arbitrarily worse than predicting the mean.

Parameters:

  • X : MatrixType Test samples of shape (n_samples, n_features).

  • y : VectorType True values of shape (n_samples,).

Returns:

  • result : ScalarType R2R^2 score.

Throws:

  • std::runtime_error — if the model has not been fitted.

fit_multi(X, Y)

Fit Ridge with a multi-target response matrix.

Solves (XcXc+αI)W=XcYc(X_c^\top X_c + \alpha I) W = X_c^\top Y_c via a single Cholesky factorisation shared across targets — LLT::solve accepts a matrix RHS, so per-target cost is just the back-substitution. Coefficient shape is (n_targets, n_features); intercept shape is (n_targets,).

Additive: the single-target API (coef() / intercept()) keeps working and reflects the first target column. New accessors: coef_matrix(), intercept_vector(), n_targets(), predict_multi(X).


predict_multi(X)

Multi-target predict, shape (n_samples, n_targets).


Example

// Compare different alpha values
std::cout << "=== Ridge: alpha sweep ===\n";
for (double alpha : {0.01, 0.1, 1.0, 10.0, 100.0}) {
Skigen::Ridge<double> model(alpha);
model.fit(X_tr, split.y_train);
std::cout << " alpha=" << std::setw(6) << alpha
<< " R²=" << model.score(X_te, split.y_test)
<< " coef=" << model.coef() << "\n";
}