Mixed Groups, Wrong Sign

Data with PythonMedium+10 XP+10

Mixed Groups, Wrong Sign

A biologist measured animals from several species. Across all animals together, two measurements seem to fall together. She suspects mixing the species is hiding the real pattern, just like the penguin bills.

Task: write group_correlations(groups, xs, ys). Row i is one animal: groups[i] is its species, xs[i] and ys[i] are its two measurements.

Return a dictionary with:

  • the key "all": the correlation between xs and ys over every animal
  • one key per species: the correlation between xs and ys for that species only

Round every correlation to 2 decimal places.

Every species has at least 3 animals, and inside each species neither measurement is the same for every animal. No species is called "all".

Examples

groups = ["A", "A", "A", "B", "B", "B"]
xs = [1, 2, 3, 6, 7, 8]
ys = [10, 11, 13, 1, 2, 2]
returns {'all': -0.88, 'A': 0.98, 'B': 0.87}
groups = ["cat", "cat", "cat", "cat"]
xs = [2, 4, 6, 8]
ys = [9, 7, 6, 2]
returns {'all': -0.96, 'cat': -0.96}

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