Pretend Everyone Had It

Machine Learning in CodeEasy+10 XP+10

Pretend Everyone Had It

Partial dependence is a thought experiment you can run yourself: give every row the same value in one column, ask the model for its predictions, and average them. Repeat for each value you want to try.

A letting agent predicts monthly rent from [floor_area_m2, building_age_years].

Task: write average_prediction_at(rows, rents, column, values).

  • Fit LinearRegression() on rows and rents.
  • For each number in values, in order: copy the rows, set column column of every copied row to that number, predict all the copied rows, and take the mean prediction.
  • Return the means as plain floats, each rounded to 2 decimal places.

Examples

rows = [[102.0, 37.0], [47.0, 38.0], [74.0, 8.0], [94.0, 13.0], [119.0, 13.0], [96.0, 14.0], [116.0, 21.0], [92.0, 27.0], [61.0, 29.0], [89.0, 26.0]]
rents = [308.5, 152.6, 263.4, 328.0, 412.1, 338.9, 387.5, 304.4, 211.0, 300.9]
column = 0
values = [50, 80, 110]
returns [180.76, 273.04, 365.33]
rows = [[102.0, 37.0], [47.0, 38.0], [74.0, 8.0], [94.0, 13.0], [119.0, 13.0], [96.0, 14.0], [116.0, 21.0], [92.0, 27.0], [61.0, 29.0], [89.0, 26.0]]
rents = [308.5, 152.6, 263.4, 328.0, 412.1, 338.9, 387.5, 304.4, 211.0, 300.9]
column = 1
values = [5, 50]
returns [325.21, 262.62]

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