Do the Clusters Find the Species?

Machine Learning in CodeMedium+10 XP+10

Do the Clusters Find the Species?

The iris dataset has 150 flowers, four measurements each, and a hidden answer: three species. Pretend you never saw the species, cluster the flowers, and only afterwards check how well the clusters match.

All four iris columns are in centimetres. So does scaling help here, as it did for the wine?

Task: write iris_match(k, scale).

  • Load the data with load_iris(return_X_y=True).
  • If scale is True, scale the features with StandardScaler().fit_transform(X). If it is False, use them as they are.
  • Fit KMeans(n_clusters=k, random_state=0, n_init=10) on those features.
  • Return adjusted_rand_score(y, labels) as a plain float, rounded to 3 decimal places.

Examples

k = 3
scale = True
returns 0.62
k = 3
scale = False
returns 0.73

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