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:
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, default1). Controls regularization strength.alpha = 0is equivalent to ordinary least squares (useLinearRegressioninstead for numerical stability). -
fit_intercept : bool, default=true Whether the intercept should be estimated (
bool, defaulttrue). Iffalse, 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 (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
Scalarif necessary.
Returns:
- result : Ridge
Reference to the fitted estimator (
*this).
sklearn parity gap: sample_weight parameter is not yet supported.
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.
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";
}