LinearRegression
Ordinary Least Squares (OLS) linear regression. Fits a linear model by minimizing the sum of squared residuals.
Objective Function
The gradient condition yields the normal equation:
Solver
Skigen solves the least squares problem via Eigen::ColPivHouseholderQR, which computes a column-pivoted QR decomposition of . This is numerically more stable than forming and inverting directly, and it correctly handles rank-deficient design matrices.
The factorization yields the solution (with appropriate permutation), and also provides the numerical rank of .
Computational Complexity
The QR factorization costs where is the number of samples and is the number of features. For , this is dominated by forming the factor.
When to Use
- OLS is the baseline linear model — use it when features are uncorrelated and the system is well-determined ().
- For ill-conditioned or underdetermined problems, prefer Ridge.
- For feature selection, prefer Lasso or ElasticNet.
Mirrors sklearn.linear_model.LinearRegression.
Constructor
Skigen::LinearRegression<Scalar> model(bool fit_intercept = true);
| Parameter | Default | Description |
|---|---|---|
fit_intercept | true | Whether to center the data and compute an intercept |
Methods
| Method | Description |
|---|---|
fit(X, y) | Fit the model via QR decomposition |
predict(X) | Predict |
score(X, y) | Return the coefficient of determination |
Fitted Attributes
| Accessor | Type | Description |
|---|---|---|
coef() | RowVectorType | Estimated coefficient vector |
intercept() | Scalar | Intercept term |
rank() | IndexType | Numerical rank of the design matrix |
Example
#include <Skigen/LinearModel>
#include <Eigen/Dense>
#include <iostream>
int main() {
Eigen::MatrixXd X(4, 2);
X << 1, 1, 1, 2, 2, 2, 2, 3;
Eigen::VectorXd y(4);
y << 6, 8, 9, 11;
Skigen::LinearRegression model;
model.fit(X, y);
std::cout << "Coef: " << model.coef() << "\n";
std::cout << "Intercept: " << model.intercept() << "\n";
std::cout << "Rank: " << model.rank() << "\n";
std::cout << "R²: " << model.score(X, y) << "\n";
}
For full parameter details and method signatures, see the auto-generated LinearRegression API Reference.