Skip to main content

LinearRegression

#include <Skigen/LinearModel>

template <typename Scalar = double>
class Skigen::LinearRegression(fit_intercept=true)

Ordinary least squares Linear Regression.

LinearRegression fits a linear model with coefficients w=(w1,,wp)w = (w_1, \ldots, w_p) to minimize the residual sum of squares between the observed targets in the dataset, and the targets predicted by the linear approximation:

w^=argminwXwy22\hat{w} = \arg\min_w \|Xw - y\|_2^2

Solves via ColPivHouseholderQR decomposition. When fit_intercept is true, data is centered before solving.

Mirrors sklearn.linear_model.LinearRegression.

Read more in the User Guide.


Parameters:

  • fit_intercept : bool, default=true Whether to calculate the intercept (bool, default true). If false, no intercept will be used (data is expected to be centered).

Attributes:

  • fit_intercept : bool Whether an intercept is fitted.

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

  • intercept : Scalar Independent term in the decision function.

  • rank : IndexType Numerical rank of the design matrix X.


Methods

fit(X, y)

Fit the model using Ordinary Least Squares.

Uses ColPivHouseholderQR decomposition. Centers data when fit_intercept is true.

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 : LinearRegression 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.
note

sklearn parity gap: sample_weight parameter is not yet supported.


Example

// Simple 2-feature dataset: y = 2*x1 + 3*x2 + 1
Eigen::MatrixXd X(6, 2);
X << 1, 1,
1, 2,
2, 2,
2, 3,
3, 3,
3, 4;
Eigen::VectorXd y(6);
y << 6, 9, 8, 11, 10, 13;

Skigen::LinearRegression<double> model;
model.fit(X, y);

std::cout << std::fixed << std::setprecision(4);
std::cout << "=== Linear Regression ===\n";
std::cout << "Coefficients: " << model.coef() << "\n";
std::cout << "Intercept: " << model.intercept() << "\n";
std::cout << "R² (train): " << model.score(X, y) << "\n\n";