sklearn
LinearRegression
model coefficients
machine learning
Python

sklearn LinearRegression, why only one coefficient returned by the model?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Overview

sklearn is a widely-used Python library for machine learning, and one of its fundamental components is the LinearRegression model. This model implements ordinary least squares linear regression. It is a straightforward and efficient method for modeling the relationship between a dependent variable and one or more independent variables.

Understanding Linear Regression

Linear regression involves fitting a linear equation:

y=β0+β1x1+β2x2+…+βnxny = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \ldots + \beta_n x_n

where:

  • yy is the dependent variable,
  • x1,x2,…,xnx_1, x_2, \ldots, x_n are the independent variables,
  • β0\beta_0 is the intercept, and
  • β1,β2,…,βn\beta_1, \beta_2, \ldots, \beta_n are the coefficients of the model.

Why Only One Coefficient?

In some cases, you may observe that sklearn's LinearRegression model returns only one coefficient. This generally happens when you fit a model using a single feature. In such cases, you're working with a simple linear regression, which can be stated as:

y=β0+β1x1y = \beta_0 + \beta_1 x_1

Here, there is only one independent variable, thus only one coefficient β1\beta_1. This simplicity is beneficial when the relationship between x1x_1 and yy is linear, making it easy to understand and interpret.

Technical Explanation

When using sklearn's LinearRegression, the model is fit using the method of least squares. This involves finding the line that minimizes the sum of the squares of the residuals (the differences between observed and predicted values).

Fit and Interpret

Here's how you can fit and interpret a simple linear regression model using sklearn:

  • X has been reshaped to indicate there's only one feature ([[1], [2], [3], [4]]),
  • The model calculates the coefficient as an array; this differs from multivariate regression where there will be multiple coefficients,
  • The resulting equation from the example would be straightforwardly y=2x+1y = 2x + 1.
  • Predicting sales based on the amount spent on advertising,
  • Estimating weight based on height,
  • Forecasting temperature using altitude.
  • Non-Linear Relationships: Linear regression assumes a linear relationship, which may not be suitable if the data exhibits non-linear patterns.
  • Sensitivity to Outliers: Outliers can significantly impact the slope and intercept of the best-fit line.
  • Assumes Homoscedasticity: It assumes constant variance of errors, which may not always be the case.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design