Continuous regression
Predicting a continuous response from numeric predictors.
Scikit-Learn · Class
LinearRegression fits an ordinary least-squares model by minimizing residual squared error. Use it to predict continuous targets from one or more features, with optional intercept fitting and non-negative coefficient constraints.
LinearRegression estimates a coefficient for each feature and an intercept so the resulting linear model minimizes the residual sum of squares on the training data. Call fit with feature data and targets, then use predict for new rows; the fitted object exposes learned coefficients and intercept.
LinearRegression(*, fit_intercept=True, copy_X=True, tol=1e-06, n_jobs=None, positive=False)| Name | Type | Description |
|---|---|---|
fit_intercept | bool, default=True | Whether to calculate the intercept for this model. If set to False, no intercept will be used in calculations. |
copy_X | bool, default=True | If True, X will be copied; otherwise, it may be overwritten. |
tol | float, default=1e-6 | The precision of the solution. Only available for sparse inputs. |
n_jobs | int, default=None | The number of jobs to use for the computation. This will only provide speedup in case of sufficiently large problems, that is if firstly n_targets > 1 and secondly X is sparse or if positive is set to True. |
positive | bool, default=False | When set to True, forces the coefficients to be positive. Only supported for dense arrays. |
fit returns self; predict returns the predicted values for X; score returns the coefficient of determination R².
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
y = np.array([6, 8, 9, 11])
model = LinearRegression().fit(X, y)
print(model.predict([[3, 4]]))
print(model.coef_)
print(model.intercept_)Creates an ordinary least-squares model, fits it to two features, then predicts a target and inspects the learned coefficients.
Predicting a continuous response from numeric predictors.
Estimating several target values from the same feature matrix.
Models where feature effects must not be negative.
LinearRegression fits an ordinary least-squares linear model. It chooses coefficients and an intercept that minimize the residual sum of squares between observed and predicted targets.
Follow-up: How can coefficient signs be constrained? Set positive=True, which requires non-negative coefficients and uses a non-negative least-squares approach.
fit(X, y, sample_weight=None) learns the model from feature matrix X and target y. After fitting, predict(X) returns predictions for new samples.
Follow-up: What happens with multiple targets? The estimator supports multi-output regression by fitting a coefficient set for each target.
The estimator can fit dense or sparse input. With positive=True, coefficients are constrained to be positive, and sparse input is not supported for that mode.
Follow-up: Which diagnostic can identify redundant feature directions? Inspect rank_ and singular_ when the fitted input is sufficiently compatible with those attributes.
RidgeLassoElasticNetSGDRegressor