Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

LinearRegression

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.

How it works

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.

Syntax

LinearRegression(*, fit_intercept=True, copy_X=True, tol=1e-06, n_jobs=None, positive=False)

Parameters

NameTypeDescription
fit_interceptbool, default=TrueWhether to calculate the intercept for this model. If set to False, no intercept will be used in calculations.
copy_Xbool, default=TrueIf True, X will be copied; otherwise, it may be overwritten.
tolfloat, default=1e-6The precision of the solution. Only available for sparse inputs.
n_jobsint, default=NoneThe 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.
positivebool, default=FalseWhen set to True, forces the coefficients to be positive. Only supported for dense arrays.

Returns

fit returns self; predict returns the predicted values for X; score returns the coefficient of determination R².

Key points

  • This class implements ordinary least-squares linear regression.
  • Training minimizes the residual sum of squares between observed targets and model predictions.
  • The constructor defaults to fitting an intercept, copying X, using a tolerance of 1e-6, no job-count limit, and unconstrained coefficients.
  • score returns the coefficient of determination R² for the supplied data.
  • n_jobs controls parallelism when the problem configuration allows parallel computation.

Common mistakes

  • The default score is R², so interpret it as a coefficient-of-determination measure rather than an error value.

Example: Fit and predict with ordinary least squares

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.

When to use it

Continuous regression

Predicting a continuous response from numeric predictors.

Multi-output prediction

Estimating several target values from the same feature matrix.

Non-negative coefficients

Models where feature effects must not be negative.

Interview questions

QWhat objective does LinearRegression optimize?

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.

QHow is the estimator used after construction?

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.

QWhat input and positivity considerations matter?

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.

  • Ridge
  • Lasso
  • ElasticNet
  • SGDRegressor

Source: LinearRegression — scikit-learn 1.9.1 documentation. Checked 2026-10-15.