Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← All challenges
ML-X17 · Clustering · 30–45 minutes

Discover and describe Penguin groups

kmeans · discovery · profiles

Complete workflow and evidence

The question

Discover groups from Penguin measurements with species hidden during fitting. Combine k evidence with sizes and original-unit profiles.

data/ml-learning/ML-X17.csv Complete workflow and evidence

Your inputs

Challenge input · ML-X17

333 observations. One measured penguin. Load the CSV file yourself.

Download challenge input CSV · Original source dataset · Source and original dictionary

Ready to load: comma-delimited UTF-8, header row, no saved index. No filtering or target construction is required. Fitting input contains measurements only; species/diagnosis reference labels are not supplied or required.

ML-X17 · first 8 prepared rows
bill_length_mmbill_depth_mmflipper_length_mmbody_mass_g
39.118.71813750
39.517.41863800
40.3181953250
36.719.31933450
39.320.61903650
38.917.81813625
39.219.61954675
41.117.61823200

Column meanings and units

Bill length/depth and flipper length: mm. Body mass: grams. Year and island: sampling context.

ML uses 333 complete cases. Removing incomplete records may change the represented population. Geographic context may not generalise to new islands.

Ready to load: comma-delimited UTF-8, header row, no saved index. No filtering or target construction is required. Fitting input contains measurements only; species/diagnosis reference labels are not supplied or required.

Input schema
ColumnStored type
bill_length_mmfloat64
bill_depth_mmfloat64
flipper_length_mmint64
body_mass_gint64

The file is available in the Python working folder. Load it yourself.

Your task
  1. Discover groups from Penguin measurements with species hidden during fitting.
  2. Combine k evidence with sizes and original-unit profiles.
  3. Plot bill length against bill depth in original units with labelled axes and distinct colours or markers for the aligned discovered groups.
evidencesizesprofileslabels

Outputs you create

evidence
  1. Dataframe with k, inertia and silhouette columns for k=2–8.
sizes
  1. Group sizes indexed by cluster label.
profiles
  1. Original-unit means grouped by aligned cluster labels.
labels
  1. One cluster label per fitted row; equivalent consistent label names are accepted.

Deliverables

These conditions describe a complete workflow. Use the declared Python variables beside the editor; Check inspects evidence from your Run.

Python name key

Teal outlines name table columns. Violet left rules name Python objects. Dotted rules mark syntax, functions and parameters. Use each name exactly as shown.

Words in this task
original-unit
shown in the measurement’s original units
aligned
matched by row or category identity, rather than guessed position
  1. Discovery population

    1. Load the four supplied Penguin measurements and fit scaling to this declared population; no species labels enter fitting.
  2. Compare groupings

    1. Compare k=2–8 with inertia, silhouette and geometry.
    2. Justify a grouping without treating a score as proof of natural classes.
  3. Assignments & profiles

    1. Return one assignment per fitted row, group sizes and aligned original-unit profiles.
  4. Interpretation

    1. Explain the grouping and its limits using scores, sizes, original-unit profiles and a labelled measurement plot showing the discovered groups.

The playground workflow

Use the ML → Workflow discovery route: define the observations and measurements, explore and prepare them, fit the grouping or representation, inspect its evidence and explain what the result can support. There is no prediction target or reserved final test in this workflow.

  1. Choose what to discoverWhat groups can I describe in Palmer Penguins · All features continuous rows using bill_length_mm, bill_depth_mm, and 2 more selected inputs?
  2. Explore the dataWhat ranges and overlaps among bill_length_mm, bill_depth_mm, and 2 more selected inputs might shape groups in Palmer Penguins · All features continuous?
  3. Prepare the dataHow should bill_length_mm, bill_depth_mm, and 2 more selected inputs be scaled so distance comparisons treat Palmer Penguins · All features continuous rows fairly?
  4. Compare possible group countsWhich candidate group count gives a useful balance for Palmer Penguins · All features continuous?
  5. Fit K-meansWhat does the selected starting group count produce for Palmer Penguins · All features continuous?
  6. Check the clustersAre the discovered Palmer Penguins · All features continuous groups separated enough and large enough to describe?
  7. Explain the clustersWhat do these groups look like in the original units of bill_length_mm, bill_depth_mm, and 2 more selected inputs for Palmer Penguins · All features continuous?
  8. Map the clustersDescribe two groups in the original units of bill_length_mm, bill_depth_mm, and 2 more selected inputs for Palmer Penguins · All features continuous; why is this grouping useful for the selected scenario?

Data policy Use only the supplied measurements for fitting. Keep group labels, scores and profiles aligned with the rows they describe.

Help Hints & explained solution

Choose the amount of help you need. Opening one hint does not reveal the others.

Hint 1 — Think

No score proves natural classes.

Hint 2 — Tools

StandardScaler, KMeans, silhouette_score and group profiles.

Revisit: Discovery checkpoint

Hint 3 — Approach

Combine geometry and scores with useful descriptions; state the chosen population and limitations.

Explained solution

This complete script replaces the editor contents. It includes the input setup and the workflow.

import numpy as np
import pandas as pd
df=pd.read_csv('data/ml-learning/ML-X17.csv')
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
import seaborn as sns
X=df[['bill_length_mm','bill_depth_mm','flipper_length_mm','body_mass_g']]
scaler=StandardScaler()
scaled=scaler.fit_transform(X)
rows=[]
for k in range(2,min(8,len(X)-1)+1):
    candidate=KMeans(n_clusters=k,n_init=20,random_state=42).fit(scaled)
    score=silhouette_score(scaled,candidate.labels_,sample_size=min(2000,len(X)),random_state=42)
    rows.append({'k':k,'inertia':candidate.inertia_,'silhouette':score})
evidence=pd.DataFrame(rows)
# Three is an illustrative choice to discuss against the evidence, not a unique optimum.
model=KMeans(n_clusters=3,n_init=20,random_state=42).fit(scaled)
labels=model.labels_
sizes=pd.Series(labels).value_counts().sort_index()
profiles=X.groupby(labels).mean()
fig,ax=plt.subplots(figsize=(6,4))
sns.scatterplot(data=X,x='bill_length_mm',y='bill_depth_mm',hue=labels,style=labels,ax=ax)
ax.set(xlabel='Bill length (mm)',ylabel='Bill depth (mm)',title='Exploratory Penguin groups')
ax.legend()
fig.savefig('clusters.png',dpi=150,bbox_inches='tight')
print(evidence)
View input setup in this script

These opening lines are included in the complete script above. They load the challenge input.

import numpy as np
import pandas as pd
df=pd.read_csv('data/ml-learning/ML-X17.csv')

Why these steps exist

  • The primary file contains only the four measurements, so species cannot influence fitting or k selection. Standardised geometry supports the k comparison, while aligned original-unit profiles and sizes support interpretation. A defensible grouping need not maximise one score or represent natural classes.

Another valid approach

Equivalent ordinary Python is welcome. A defensible candidate or grouping need not match the solution’s choice; objective evidence must remain consistent.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.