Skip to main content

Ridge

#include <Skigen/LinearModel>

template <typename Scalar = double>
class Skigen::Ridge(alpha=1, fit_intercept=true)

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.


Parameters:

  • alpha : Scalar, default=1 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 : bool, default=true Whether the intercept should be estimated (bool, default true). If false, the data is assumed to be already centered.


Attributes:

  • alpha : Scalar Regularization strength.

  • fit_intercept : bool Whether an intercept is fitted.

  • coef : RowVectorType Parameter vector ww (1 × n_features).

  • intercept : Scalar Independent term in the decision function.


Methods

fit(X, y)

Fit the Ridge model via Cholesky decomposition.

Centers the data when fit_intercept is true, then solves the regularized normal equation via Cholesky factorization.

Parameters:

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

  • y : VectorType Target vector of shape (n_samples,). Will be cast to Scalar if necessary.

Returns:

  • result : Ridge Reference to the fitted estimator (*this).
note

sklearn parity gap: sample_weight parameter is not yet supported.


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.

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";
}