Each Column's Push

Machine Learning in CodeEasy+10 XP+10

Each Column's Push

An estate agent's model predicts house prices (in thousands) from [size_m2, rooms, km_to_centre]. A seller asks: why did my house get that price?

For a scaled linear model, each column's push on one row is its scaled value times its coefficient.

Task: write pushes(rows, prices, names, index).

  • Fit make_pipeline(StandardScaler(), LinearRegression()) on rows and prices.
  • Take row index of rows. Scale it with the pipeline's fitted scaler, model.named_steps["standardscaler"].
  • Multiply the scaled values by model.named_steps["linearregression"].coef_.
  • Return a dict mapping each name in names to its push, as a plain float rounded to 2 decimal places.

Examples

rows = [[135.0, 5, 2.3], [125.0, 4, 5.6], [100.0, 1, 9.5], [126.0, 2, 5.4], [99.0, 4, 14.0], [117.0, 5, 2.8], [98.0, 4, 3.0], [75.0, 4, 12.6], [63.0, 3, 13.2], [119.0, 1, 14.9], [97.0, 1, 10.8], [72.0, 2, 5.3]]
prices = [388.7, 336.9, 229.4, 316.9, 230.0, 348.0, 300.8, 193.3, 159.0, 234.1, 219.5, 212.2]
names = ['size_m2', 'rooms', 'km_to_centre']
index = 0
returns {'size_m2': 63.96, 'rooms': 21.1, 'km_to_centre': 37.99}
rows = [[135.0, 5, 2.3], [125.0, 4, 5.6], [100.0, 1, 9.5], [126.0, 2, 5.4], [99.0, 4, 14.0], [117.0, 5, 2.8], [98.0, 4, 3.0], [75.0, 4, 12.6], [63.0, 3, 13.2], [119.0, 1, 14.9], [97.0, 1, 10.8], [72.0, 2, 5.3]]
prices = [388.7, 336.9, 229.4, 316.9, 230.0, 348.0, 300.8, 193.3, 159.0, 234.1, 219.5, 212.2]
names = ['size_m2', 'rooms', 'km_to_centre']
index = 5
returns {'size_m2': 28.9, 'rooms': 21.1, 'km_to_centre': 34.82}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run