How to print intercept and slope of a simple linear regression in Python with scikit-learn?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In scikit-learn, the intercept and slope of a simple linear regression model are available after fitting LinearRegression. The main details to remember are that X must be two-dimensional and that the slope is stored in coef_, which is still an array even when there is only one feature.
Fit a Simple Linear Regression Model
For simple linear regression, you have one input feature and one target value. Scikit-learn still expects X to have shape n_samples x n_features, so a single feature must be written as a column.
In this example, the fitted line is close to y = 2x + 1, so the intercept prints near 1.0 and the slope prints near 2.0.
Understand Where the Values Live
After fitting, scikit-learn stores:
- '
intercept_for the constant term' - '
coef_for the feature coefficients'
With one feature, coef_ contains a single value, so you usually print model.coef_[0]. With multiple features, coef_ contains one coefficient per feature.
That is why a lot of examples look slightly inconsistent at first glance. The intercept is usually scalar, while the coefficients are array-like.
Write the Fitted Equation
Once you have the numbers, it is easy to print the regression equation itself.
That is often more useful than printing the raw attributes because it makes the result easier to interpret and report.
Plot the Data and Fitted Line
If you want to verify the result visually, plot the original points and the predicted line.
This is especially helpful when you want to confirm that the model is actually linear enough to justify printing a slope and intercept in the first place.
Why Shape Matters
One of the most common beginner errors is passing a one-dimensional array for X, like this:
Scikit-learn will reject that because it expects a matrix, not a flat vector. The fix is to reshape:
That one line solves a large percentage of "why does LinearRegression.fit fail" questions.
Multiple Features Change the Meaning
The title says simple linear regression, which means one feature. If you later add more predictors, the model still has one intercept_, but coef_ becomes a list of slopes, one per feature.
At that point, you are no longer talking about a single slope in the usual classroom sense. You are talking about one coefficient per feature.
Common Pitfalls
The biggest mistake is passing X as a one-dimensional array. Reshape it to (-1, 1) for a single feature.
Another common issue is printing model.coef_ directly and being surprised that it is an array. For simple linear regression, the slope is usually model.coef_[0].
People also sometimes print coefficients before calling fit, which fails because the attributes do not exist until the model has been trained.
Finally, remember that a slope and intercept describe the fitted line, not proof that the relationship is truly linear or causally meaningful.
Summary
- Fit
LinearRegressionfirst, then readintercept_andcoef_. - In simple linear regression, the slope is usually
model.coef_[0]. - Keep
Xtwo-dimensional, even when you have only one feature. - Printing the fitted equation often makes the result easier to understand.
- Use a plot when you want to sanity-check the regression visually.

