Understand the idea
Highly correlated inputs can exchange coefficient weight with little change in predictions. This makes individual effects difficult to interpret. The Change round keeps X fixed and perturbs y slightly, measuring both coefficient sensitivity and prediction sensitivity. It is a controlled fitted-state experiment, not a held-out performance comparison.
What the syntax does
rng.normal(0, .001, len(df))- Creates a small reproducible perturbation for each observation.
pd.Series(model.coef_, index=X.columns)- Labels the jointly fitted coefficients, including the near-duplicate input.
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
distance | weight | service | weekend | duration |
|---|---|---|---|---|
| 15.7052 | 6.75035 | standard | 0 | 61.7704 |
| 9.33869 | 4.81674 | express | 1 | 35.1694 |
| 17.3134 | 5.73931 | economy | 0 | 59.0766 |
| 14.25 | 7.69699 | standard | 1 | 56.5785 |
| 2.78937 | 6.42024 | express | 0 | 19.5577 |
| 19.5368 | 5.62508 | economy | 1 | 77.0289 |
| 15.4617 | 5.68023 | standard | 0 | 56.8109 |
| 15.9352 | 3.17871 | express | 1 | 52.08 |
- Fit a line using
distanceand a near-copy ofdistance. - Store the two coefficients in
answer, labelled by their feature names.
answerHints
Think: Nearly duplicate predictors can share the same predictive contribution in unstable proportions.
Tools: Seeded noise, dataframe construction and LinearRegression.coef_.
Approach: Create the near-copy column, fit both columns together and label the resulting coefficients.
Explained solution
from sklearn.linear_model import LinearRegression
rng=np.random.default_rng(42)
X=pd.DataFrame({'distance':df.distance,'near_copy':df.distance+rng.normal(0,.001,len(df))})
model=LinearRegression().fit(X,df.duration)
answer=pd.Series(model.coef_,index=X.columns)
The tiny perturbation preserves strong correlation; inspecting both coefficients exposes instability that prediction quality alone can conceal.