Ridge
#include <Skigen/LinearModel>
template <typename Scalar = double>
class Skigen::Ridge
Linear least squares with L2 regularization.
Minimizes the objective function:
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, default1). Controls regularization strength.alpha = 0is equivalent to ordinary least squares (useLinearRegressioninstead for numerical stability). -
fit_intercept Whether the intercept should be estimated (
bool, defaulttrue). Iffalse, the data is assumed to be already centered.
fit(X, y)
predict(X)
Predict using the linear model.
Computes where and 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 coefficient of determination on test data.
. 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 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 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";
}