When the Average Hides Two Stories

Machine Learning in CodeMedium+10 XP+10

When the Average Hides Two Stories

A shop predicts basket value from [discount_percent, is_member, random_score]. Discounts might help members and put off everyone else. An average curve could hide that, so look at one curve per row (ICE) as well.

Task: write ice_summary(rows, baskets, column).

  • Fit DecisionTreeRegressor(max_depth=4, random_state=0) on rows and baskets.
  • Run partial_dependence(model, rows, features=[column], kind="both", grid_resolution=5).
  • The result's ["average"][0] is the average curve. Its ["individual"][0] holds one curve per row.
  • A curve rises if its last value is greater than its first, and falls if its last value is smaller. Otherwise it is flat.
  • Return a dict:
    • "average_change": last value minus first value of the average curve, a plain float rounded to 2 decimal places
    • "rising": how many row curves rise
    • "falling": how many row curves fall

Examples

rows = [[27.1, 0, 0.67], [20.3, 0, 0.04], [5.3, 1, 0.3], [29.5, 1, 0.71], [12.7, 1, 0.28], [0.6, 0, 0.84], [15.5, 0, 0.6], [29.8, 1, 0.58], [20.8, 1, 0.85], [9.7, 1, 0.04], [18.8, 1, 0.67], [18.1, 1, 0.23], [12.5, 0, 0.95], [29.1, 0, 0.79], [10.9, 0, 0.62], [12.7, 0, 0.75], [18.8, 1, 0.63], [20.3, 0, 0.9], [15.1, 1, 0.7], [9.5, 0, 0.62], [29.4, 0, 0.42], [14.2, 0, 0.26], [21.8, 1, 0.32], [15.6, 1, 0.87], [1.7, 1, 0.94], [26.1, 1, 0.9], [10.6, 1, 0.49], [23.1, 0, 0.06], [2.1, 0, 0.48], [0.4, 1, 0.13]]
baskets = [56.4, 71.9, 115.9, 161.0, 123.6, 97.8, 75.9, 158.6, 139.5, 122.2, 142.1, 135.6, 77.7, 58.9, 84.2, 77.6, 135.6, 68.5, 127.8, 84.9, 51.8, 84.0, 141.4, 130.6, 102.1, 147.3, 122.1, 67.3, 98.9, 100.3]
column = 0
returns {'average_change': 10.91, 'rising': 16, 'falling': 14}
rows = [[4.8, 1, 0.38], [20.6, 1, 0.97], [20.1, 1, 0.46], [16.3, 1, 0.6], [12.3, 0, 0.41], [1.2, 1, 0.23], [27.1, 1, 0.46], [28.9, 0, 0.16], [4.5, 0, 0.37], [1.4, 0, 0.92], [16.1, 0, 0.43], [22.5, 1, 0.81], [26.0, 0, 0.93], [18.7, 0, 0.26], [28.2, 1, 0.99], [14.8, 0, 0.38], [8.6, 0, 0.28], [22.7, 1, 0.38], [5.1, 1, 0.11], [24.5, 0, 0.8], [0.1, 1, 0.02], [1.2, 1, 0.14], [26.6, 0, 0.54], [10.5, 0, 0.5]]
baskets = [111.6, 146.2, 138.3, 133.5, 82.1, 106.2, 152.6, 60.3, 95.8, 97.2, 77.0, 141.9, 60.0, 70.9, 154.4, 81.9, 89.4, 145.4, 106.6, 64.7, 101.2, 101.4, 61.3, 83.1]
column = 0
returns {'average_change': 4.86, 'rising': 12, 'falling': 12}

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