Read the Coefficients
Intermediate · Predict the output · from Build Your First ML Model: Linear Regression with scikit-learn
From the lesson: Build Your First ML Model: Linear Regression with scikit-learn
Imagine you're a real estate agent. A client asks, "How much is my house worth?" You look at the square footage, the number of bedrooms, and the neighborhood - then you give an estimate. You just did regression in your head.
Read the lessonWhat will this code print? Think about what coef_ and intercept_ represent.
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10]) # y = 2*x
model = LinearRegression()
model.fit(X, y)
print(round(model.coef_[0], 4), round(model.intercept_, 4) + 0.0)
More Linear Regression exercises
- Train Your First ModelIntermediate
- Fix the Scaling BugIntermediate
- Multi-Feature RegressionIntermediate
- Polynomial vs LinearIntermediate
- Complete Regression PipelineIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz