Who Pushes Hardest?

Machine Learning in CodeMedium+10 XP+10

Who Pushes Hardest?

In the diabetes dataset, each coefficient says how strongly one measurement pushes the prediction up (positive) or pulls it down (negative). The columns were scaled before the dataset was published, so the coefficients can be compared with each other.

Write strongest_columns(n).

  • Load the data with load_diabetes(return_X_y=True, as_frame=True).
  • Split it with train_test_split(X, y, test_size=0.25, random_state=0).
  • Fit a LinearRegression model on the training rows only.
  • Return a dict with two lists of column names:
    • "up": the n columns with the largest positive coefficients, biggest first.
    • "down": the n columns with the most negative coefficients, most negative first.

If there are fewer than n positive (or negative) coefficients, list all of them.

Examples

n = 1
returns {'up': ['s5'], 'down': ['s1']}
n = 2
returns {'up': ['s5', 'bmi'], 'down': ['s1', 'sex']}

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