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
Do not treat the coef_ values as an importance ranking without thinking. The size of a coefficient depends on the unit of each feature. For example, area in square feet and a 0/1 flag use very different units, so you cannot compare their coefficients directly.
Ravindra Bagale's Tip – मराठी
coef_ च्या values ना विचार न करता importance ranking समजू नका. Coefficient किती मोठा आहे हे प्रत्येक feature च्या unit वर अवलंबून असतं. उदाहरणार्थ, square feet मधला area आणि 0/1 flag यांचे units खूप वेगळे आहेत, त्यामुळे त्यांच्या coefficients ची थेट तुलना करता येत नाही.
Ravindra Bagale's Tip – हिंदी
coef_ की values को बिना सोचे importance ranking मत समझो. Coefficient कितना बड़ा है, यह हर feature की unit पर निर्भर करता है. उदाहरण के लिए, square feet में area और 0/1 flag की units बहुत अलग हैं, इसलिए उनके coefficients की सीधी तुलना नहीं की जा सकती.