Steepest and Flattest

Machine Learning in CodeMedium+10 XP+10

Steepest and Flattest

A forest predicts diabetes progression. For a handful of columns, which one moves its predictions the most, and which barely moves them at all?

Measure each curve's size as its range: the highest average prediction minus the lowest.

Task: write steepest_and_flattest(columns).

  • Load load_diabetes(return_X_y=True, as_frame=True).
  • Split with train_test_split(X, y, test_size=0.25, random_state=0).
  • Fit RandomForestRegressor(n_estimators=50, random_state=0) on the training rows.
  • For each name in columns, run partial_dependence(model, X_train, features=[name], grid_resolution=10) and take its ["average"][0].
  • Return [steepest, flattest]: the name with the biggest range and the name with the smallest.

Examples

columns = ["bmi", "age", "bp"]
returns ['bmi', 'age']
columns = ["s5", "s1", "bp"]
returns ['s5', 's1']

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