sklearn.linear_model.LinearRegression (2024)

class sklearn.linear_model.LinearRegression(*, fit_intercept=True, copy_X=True, n_jobs=None, positive=False)[source]

Ordinary least squares Linear Regression.

LinearRegression fits a linear model with coefficients w = (w1, …, wp)to minimize the residual sum of squares between the observed targets inthe dataset, and the targets predicted by the linear approximation.

Parameters:
fit_interceptbool, default=True

Whether to calculate the intercept for this model. If setto False, no intercept will be used in calculations(i.e. data is expected to be centered).

copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.

n_jobsint, default=None

The number of jobs to use for the computation. This will only providespeedup in case of sufficiently large problems, that is if firstlyn_targets > 1 and secondly X is sparse or if positive is setto True. None means 1 unless in ajoblib.parallel_backend context. -1 means using allprocessors. See Glossary for more details.

positivebool, default=False

When set to True, forces the coefficients to be positive. Thisoption is only supported for dense arrays.

Added in version 0.24.

Attributes:
coef_array of shape (n_features, ) or (n_targets, n_features)

Estimated coefficients for the linear regression problem.If multiple targets are passed during the fit (y 2D), thisis a 2D array of shape (n_targets, n_features), while if onlyone target is passed, this is a 1D array of length n_features.

rank_int

Rank of matrix X. Only available when X is dense.

singular_array of shape (min(X, y),)

Singular values of X. Only available when X is dense.

intercept_float or array of shape (n_targets,)

Independent term in the linear model. Set to 0.0 iffit_intercept = False.

n_features_in_int

Number of features seen during fit.

Added in version 0.24.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when Xhas feature names that are all strings.

Added in version 1.0.

See also

Ridge

Ridge regression addresses some of the problems of Ordinary Least Squares by imposing a penalty on the size of the coefficients with l2 regularization.

Lasso

The Lasso is a linear model that estimates sparse coefficients with l1 regularization.

ElasticNet

Elastic-Net is a linear regression model trained with both l1 and l2 -norm regularization of the coefficients.

Notes

From the implementation point of view, this is just plain OrdinaryLeast Squares (scipy.linalg.lstsq) or Non Negative Least Squares(scipy.optimize.nnls) wrapped as a predictor object.

Examples

>>> import numpy as np>>> from sklearn.linear_model import LinearRegression>>> X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])>>> # y = 1 * x_0 + 2 * x_1 + 3>>> y = np.dot(X, np.array([1, 2])) + 3>>> reg = LinearRegression().fit(X, y)>>> reg.score(X, y)1.0>>> reg.coef_array([1., 2.])>>> reg.intercept_3.0...>>> reg.predict(np.array([[3, 5]]))array([16.])

Methods

fit(X,y[,sample_weight])

Fit linear model.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

predict(X)

Predict using the linear model.

score(X,y[,sample_weight])

Return the coefficient of determination of the prediction.

set_fit_request(*[,sample_weight])

Request metadata passed to the fit method.

set_params(**params)

Set the parameters of this estimator.

set_score_request(*[,sample_weight])

Request metadata passed to the score method.

fit(X, y, sample_weight=None)[source]

Fit linear model.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data.

yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values. Will be cast to X’s dtype if necessary.

sample_weightarray-like of shape (n_samples,), default=None

Individual weights for each sample.

Added in version 0.17: parameter sample_weight support to LinearRegression.

Returns:
selfobject

Fitted Estimator.

get_metadata_routing()[source]

Get metadata routing of this object.

Please check User Guide on how the routingmechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulatingrouting information.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator andcontained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

predict(X)[source]

Predict using the linear model.

Parameters:
Xarray-like or sparse matrix, shape (n_samples, n_features)

Samples.

Returns:
Carray, shape (n_samples,)

Returns predicted values.

score(X, y, sample_weight=None)[source]

Return the coefficient of determination of the prediction.

The coefficient of determination \(R^2\) is defined as\((1 - \frac{u}{v})\), where \(u\) is the residualsum of squares ((y_true - y_pred)** 2).sum() and \(v\)is the total sum of squares ((y_true - y_true.mean()) ** 2).sum().The best possible score is 1.0 and it can be negative (because themodel can be arbitrarily worse). A constant model that always predictsthe expected value of y, disregarding the input features, would geta \(R^2\) score of 0.0.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputedkernel matrix or a list of generic objects instead with shape(n_samples, n_samples_fitted), where n_samples_fittedis the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

\(R^2\) of self.predict(X) w.r.t. y.

Notes

The \(R^2\) score used when calling score on a regressor usesmultioutput='uniform_average' from version 0.23 to keep consistentwith default value of r2_score.This influences the score method of all the multioutputregressors (except forMultiOutputRegressor).

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LinearRegression[source]

Request metadata passed to the fit method.

Note that this method is only relevant ifenable_metadata_routing=True (see sklearn.set_config).Please see User Guide on how the routingmechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains theexisting request. This allows you to change the request for someparameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as asub-estimator of a meta-estimator, e.g. used inside aPipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects(such as Pipeline). The latter haveparameters of the form <component>__<parameter> so that it’spossible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LinearRegression[source]

Request metadata passed to the score method.

Note that this method is only relevant ifenable_metadata_routing=True (see sklearn.set_config).Please see User Guide on how the routingmechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains theexisting request. This allows you to change the request for someparameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as asub-estimator of a meta-estimator, e.g. used inside aPipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

sklearn.linear_model.LinearRegression (1)

Principal Component Regression vs Partial Least Squares Regression

Principal Component Regression vs Partial Least Squares Regression

sklearn.linear_model.LinearRegression (2)

Plot individual and voting regression predictions

Plot individual and voting regression predictions

sklearn.linear_model.LinearRegression (3)

Comparing Linear Bayesian Regressors

Comparing Linear Bayesian Regressors

sklearn.linear_model.LinearRegression (4)

Linear Regression Example

Linear Regression Example

sklearn.linear_model.LinearRegression (5)

Logistic function

Logistic function

sklearn.linear_model.LinearRegression (6)

Non-negative least squares

Non-negative least squares

sklearn.linear_model.LinearRegression (7)

Ordinary Least Squares and Ridge Regression Variance

Ordinary Least Squares and Ridge Regression Variance

sklearn.linear_model.LinearRegression (8)

Quantile regression

Quantile regression

sklearn.linear_model.LinearRegression (9)

Robust linear estimator fitting

Robust linear estimator fitting

sklearn.linear_model.LinearRegression (10)

Robust linear model estimation using RANSAC

Robust linear model estimation using RANSAC

sklearn.linear_model.LinearRegression (11)

Sparsity Example: Fitting only features 1 and 2

Sparsity Example: Fitting only features 1 and 2

sklearn.linear_model.LinearRegression (12)

Theil-Sen Regression

Theil-Sen Regression

sklearn.linear_model.LinearRegression (13)

Failure of Machine Learning to infer causal effects

Failure of Machine Learning to infer causal effects

sklearn.linear_model.LinearRegression (14)

Face completion with a multi-output estimators

Face completion with a multi-output estimators

sklearn.linear_model.LinearRegression (15)

Isotonic Regression

Isotonic Regression

sklearn.linear_model.LinearRegression (16)

Metadata Routing

Metadata Routing

sklearn.linear_model.LinearRegression (17)

Plotting Cross-Validated Predictions

Plotting Cross-Validated Predictions

sklearn.linear_model.LinearRegression (18)

Underfitting vs. Overfitting

Underfitting vs. Overfitting

sklearn.linear_model.LinearRegression (19)

Using KBinsDiscretizer to discretize continuous features

Using KBinsDiscretizer to discretize continuous features

sklearn.linear_model.LinearRegression (2024)
Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5822

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.