What Moves the Price?

Data with PythonMedium+10 XP+10

What Moves the Price?

A heatmap shows every pair of columns. Often only one row of it matters: how each column relates to the one thing you want to predict, like a flat's price.

Task: write rank_against(rows, target). rows is a list of dictionaries, one per record, all with the same keys. Some columns hold text and some hold numbers. target is the name of a number column.

  • For every other number column, work out its correlation with target, rounded to 2 decimal places.
  • Skip the text columns, and don't pair target with itself.
  • Return a list of [column, r] pairs, ordered by the size of the rounded r, ignoring its sign, largest first. So -0.9 comes before 0.8.
  • If two pairs have the same size, put them in alphabetical order of column name.

There are no missing values, there is at least one other number column, and no number column holds the same value in every row.

Examples

rows = [{"area": "east", "rooms": 1, "size": 40, "floor": 3, "price": 150}, {"area": "west", "rooms": 2, "size": 55, "floor": 1, "price": 190}, {"area": "east", "rooms": 3, "size": 80, "floor": 5, "price": 260}, {"area": "north", "rooms": 2, "size": 60, "floor": 2, "price": 200}, {"area": "west", "rooms": 4, "size": 95, "floor": 4, "price": 300}]
target = "price"
returns [['size', 1.0], ['rooms', 0.99], ['floor', 0.64]]
rows = [{"name": "a", "hours": 1, "score": 40}, {"name": "b", "hours": 3, "score": 55}, {"name": "c", "hours": 5, "score": 80}]
target = "score"
returns [['hours', 0.99]]

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