Weights in the Wrong Order

Data with PythonMedium+10 XP+10

Weights in the Wrong Order

A bank scores loan applicants with a simple model. Each row of X is one applicant, and columns names each column of X in order.

The model's weights arrive as a dict, weights, mapping a feature's name to its weight. It was saved by a different program, so:

  • its names may be in a different order from columns,
  • a feature in columns that is missing from weights has a weight of 0,
  • names in weights that aren't in columns are ignored.

An applicant's score is each feature times its own weight, all added up, plus bias. An applicant is approved when their score is greater than or equal to threshold. Compare the unrounded score.

Task: write approve(X, columns, weights, bias, threshold) returning a dict:

  • "scores" — every applicant's score, in row order, rounded to 2 decimal places,
  • "approved" — the row numbers of the approved applicants, smallest first.

Examples

X = [[30, 2, 5], [45, 0, 1], [25, 4, 8]]
columns = ['income', 'debts', 'years']
weights = {'years': 1.0, 'income': 0.5, 'debts': -2.0}
bias = 0.0
threshold = 20
returns {'scores': [16.0, 23.5, 12.5], 'approved': [1]}
X = [[3, 7], [6, 2]]
columns = ['test', 'interview']
weights = {'interview': 2.0, 'test': 1.0, 'references': 5.0}
bias = 1.0
threshold = 10
returns {'scores': [18.0, 11.0], 'approved': [0, 1]}

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