Most Alike, Fairly

Data with PythonHard+10 XP+10

Most Alike, Fairly

A running club's buddy-matching app describes each member by a few measured features, one row per member in X, with their names in names. A newcomer, query, has the same features in the same order.

The app wants to know who is most like the newcomer: the member with the smallest straight-line distance to them. It also wants to see whether scaling changes the answer.

  • Raw: measure the distances on the numbers exactly as given.
  • Scaled: first standardise. Work out each column's average and spread (the same .std() as in the lesson) from the members in X only, not the newcomer. Use those same values to scale every member's row and the newcomer's row. Then measure the distances.

Task: write most_alike(names, X, query) returning a dict {"raw": ..., "scaled": ...}, holding the name of the nearest member each way. If two members are exactly as near, pick the one that comes first in names.

No column of X has a spread of 0.

Examples

names = ['Ben', 'Cara', 'Dev']
X = [[60, 41000], [27, 52000], [48, 60000]]
query = [25, 40000]
returns {'raw': 'Ben', 'scaled': 'Cara'}
names = ['Ivy', 'Jon', 'Kim', 'Lou']
X = [[170, 3], [150, 9], [182, 1], [160, 5]]
query = [168, 8]
returns {'raw': 'Ivy', 'scaled': 'Lou'}

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