Which Way Does It Push?

Machine Learning in CodeMedium+10 XP+10

Which Way Does It Push?

A gym wants to know what makes members cancel. Each row describes one member, and each label is 1 if they cancelled and 0 if they stayed.

Write push_directions(rows, cancelled, names).

  • names holds the column names, in the same order as each row.
  • Fit make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).
  • Read the coefficients from the logistic regression step of the pipeline (named_steps["logisticregression"].coef_). There is one row of coefficients, with one number per column.
  • A positive coefficient pushes towards class 1 (cancelling). A negative one pushes towards class 0 (staying).

Return a dict that maps each column name to "cancel" for a positive coefficient or "stay" for a negative one. No coefficient is zero in the tests.

Examples

rows = [[0, 12, 80], [1, 10, 60], [5, 1, 30], [4, 2, 45], [0, 8, 95], [6, 1, 20], [3, 3, 50], [2, 6, 70], [7, 2, 25], [1, 9, 85], [4, 4, 40], [0, 5, 65], [5, 3, 35], [2, 1, 55]]
cancelled = [0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1]
names = ['complaints', 'years_member', 'visits_per_month']
returns {'complaints': 'cancel', 'years_member': 'stay', 'visits_per_month': 'stay'}
rows = [[1, 20], [2, 18], [3, 15], [8, 6], [9, 4], [10, 2], [4, 12], [7, 9]]
cancelled = [0, 0, 0, 1, 1, 1, 0, 1]
names = ['missed_payments', 'classes_booked']
returns {'missed_payments': 'cancel', 'classes_booked': 'stay'}

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