Sub topic 7
Reading coef_ and intercept_
This chapter assumes you can fit a LinearRegression and use a pandas DataFrame. After fit, the model stores what it learned in two attributes. coef_ holds one weight per feature, and intercept_ holds the constant term b in y = Xw + b. The trailing underscore is scikit-learn's convention for values set during fitting.
What each number means
A coefficient is the expected change in the target when that feature rises by one unit, with all other features held fixed. The intercept is the prediction when every feature equals zero. That point is often outside your data, so the intercept may have no practical meaning on its own.
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
rng = np.random.default_rng(0)
n = 200
area = rng.uniform(40, 160, n)
age = rng.uniform(0, 50, n)
price = 2.5 * area - 1.2 * age + 40 + rng.normal(0, 10, n)
X = pd.DataFrame({"area": area, "age": age})
model = LinearRegression().fit(X, price)
for name, c in zip(X.columns, model.coef_):
print(f"{name}: {c:.3f}")
print("intercept:", model.intercept_)The printed values should land near 2.5 for area, near -1.2 for age, and near 40 for the intercept. Read the first as "one more square metre adds about 2.5 to the price, at a fixed age". The order of coef_ matches the column order of X, so pair them by position.
Units change the numbers
Coefficients depend on the units of each feature. If you measured area in square feet instead, its coefficient would shrink. You therefore cannot compare raw coefficients across features to decide which matters more.
To compare, standardize the features first. Each coefficient then describes the change in the target per one standard deviation of that feature.
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
pipe = make_pipeline(StandardScaler(), LinearRegression()).fit(X, price)
lr = pipe[-1]
print("scaled coef:", dict(zip(X.columns, lr.coef_.round(2))))
print("intercept == mean(y):", np.isclose(lr.intercept_, price.mean()))
# Convert back to original units
scaler = pipe[0]
orig = lr.coef_ / scaler.scale_
print("original-unit coef:", orig)With standardized features, the intercept equals the mean of the target. Dividing by scaler.scale_ recovers the coefficients in the original units.
Cautions
- For a multi-target
y,coef_has shape(n_targets, n_features). coef_is always a NumPy array, even if you passed a DataFrame. Usemodel.feature_names_in_to get the column names.- With
fit_intercept=False,intercept_is0.0and the line is forced through the origin.
Exercise
You standardize two features and get scaled coefficients of 4.0 for income and -6.0 for debt. A colleague says income matters more because its raw coefficient is larger. What do you say?
Show answer
Compare the scaled values, not raw ones, because raw coefficients depend on units. Here debt has the larger magnitude (6.0 versus 4.0), so a one-standard-deviation change in debt shifts the target more, in the negative direction. This still describes association, and correlated features can make the comparison unreliable.