Describe the Wine Groups

Machine Learning in CodeMedium+10 XP+10

Describe the Wine Groups

A cluster number means nothing until someone can say what the group is. A wine merchant wants each group described in real units.

Task: write describe_groups(k, columns).

  • Load the wine data with load_wine(return_X_y=True, as_frame=True).
  • Scale all 13 columns with StandardScaler().fit_transform(X).
  • Fit KMeans(n_clusters=k, random_state=0, n_init=10) on the scaled data.
  • For each cluster, find the mean of each name in columns, using the original, unscaled values.
  • Return a list with one dict per cluster. Each dict has "size" (the number of wines in it, a plain int) and one key per column name, holding that mean as a plain float rounded to 2 decimal places.
  • Order the dicts from the largest cluster to the smallest. No two clusters have the same size.

Examples

k = 3
columns = ["alcohol", "flavanoids"]
returns [{'size': 65, 'alcohol': 12.25, 'flavanoids': 2.05}, {'size': 62, 'alcohol': 13.68, 'flavanoids': 3.0}, {'size': 51, 'alcohol': 13.13, 'flavanoids': 0.82}]
k = 2
columns = ["proline"]
returns [{'size': 113, 'proline': 828.75}, {'size': 65, 'proline': 604.58}]

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