Score on a Fair Scale

Data with PythonMedium+10 XP+10

Score on a Fair Scale

A charity shortlists volunteers with a simple scoring model. Each row of X is one volunteer, and each column is something measured about them, such as hours free per week or years of experience.

The columns use very different units, so the features are put on the same scale first.

  1. Standardise: for each column, subtract that column's average and divide by its spread (NumPy's .std()). Work these out from X itself.
  2. Score: each volunteer's score is their standardised features multiplied by weights, all added up, plus bias. weights holds one number per column.
  3. Shortlist: a volunteer is shortlisted when their score is greater than or equal to threshold. Compare the unrounded score.

Task: write fair_scores(X, weights, bias, threshold) and return a dict:

  • "scores" — every volunteer's score, in row order, rounded to 3 decimal places,
  • "shortlisted" — how many volunteers were shortlisted.

There are at least two volunteers, and no column has a spread of 0.

Examples

X = [[10, 2], [4, 8], [6, 5], [12, 1]]
weights = [0.7, 0.3]
bias = 0.0
threshold = 0.0
returns {'scores': [0.224, -0.447, -0.333, 0.557], 'shortlisted': 2}
X = [[25, 40000], [60, 41000], [27, 52000], [48, 60000]]
weights = [-0.5, 1.0]
bias = 1.0
threshold = 1.5
returns {'scores': [0.513, -0.561, 1.898, 2.15], 'shortlisted': 2}

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