33. Linear Regression in Python with sklearn
33.4 Reading coef_ and intercept_
Why. After fit, managers ask “what did the model learn?” You answer with coef_ and intercept_ in plain words.
What. model.coef_ is an array — one number per feature column. model.intercept_ is a single starting value. Signs matter: positive coef means “feature up → prediction up” on average (holding other features fixed in a linear model).
How.
Read the learned numbers
- Print
model.intercept_andmodel.coef_. - Match each coef to its column name.
- Say one business sentence in ₹ terms.
# Continue after model.fit(...) from 33.3
print("intercept_:", round(model.intercept_, 2))
print("coef_ area_sqft:", round(model.coef_[0], 2))
# Two-feature toy: area + is_metro (1 = Pune/Mumbai tier in fiction)
df2 = pd.DataFrame({
"area_sqft": [500, 700, 900, 500, 700, 900],
"is_metro": [1, 1, 1, 0, 0, 0],
"rent_inr": [16000, 20000, 24000, 11000, 14000, 17000],
})
X2 = df2[["area_sqft", "is_metro"]]
y2 = df2["rent_inr"]
m2 = LinearRegression().fit(X2, y2)
for name, c in zip(X2.columns, m2.coef_):
print(name, "->", round(c, 2))
print("intercept_:", round(m2.intercept_, 2))
What you should see. A positive coef on area (more sqft → higher rent) and a positive coef on is_metro (metro flag adds ₹). Exact numbers depend on the tiny sample — focus on the story, not memorising digits.
Ravindra Bagale's Tip
coef_ la “importance ranking” mhunu blindly sangu naka — scale matter karto. Area in sqft vs a 0/1 flag are different units.