Python for data · lesson 11 of 11
Parts in this lesson
Apd.read_csv, isna().sum()0.9: load and look
Bdropna(subset=features)0.9: drop only what the model can’t use
CX[y == name].mean(axis=0)0.3 and 0.4: one class’s mean of each feature
Dplt.subplots(2, 2)0.8: four plots in one figure
E## What I noticedyour findings, in a Markdown cell

Project: a first look at data

Last updated

Before anyone trains a model, they look at the data. In this project you'll do that properly for the Palmer penguins: load the file, find and drop what's missing, turn the table into X and y, compare the three species with NumPy, draw four plots, and write down what you see. Stage 2 trains a classifier on this same data, and what you notice here will tell you whether it has a chance.

By the end of this lesson you can

  • Load a real dataset, find its missing values, and keep every row the model can use
  • Turn a table into a feature matrix and a label array, and summarise each class with masks and axes
  • Draw four labelled plots in one figure, coloured by class
  • Write down, in words, what the data suggests a model could learn

Plan it

The question: can three species of penguin be told apart from four measurements of their bodies? You won't build a model yet. You'll find out whether there's anything for a model to learn.

The data: penguins.csv, 344 penguins measured at Palmer Station, Antarctica, between 2007 and 2009 by Kristen Gorman and colleagues. It's released under CC0, so anyone can use it for anything. Each row is one bird: its species (Adelie, Chinstrap or Gentoo), its island, bill length and depth in mm, flipper length in mm, body mass in g, sex and year.

What you'll make: one notebook that, run from the top,

  1. loads the file and reports what's missing,
  2. keeps every penguin with all four measurements, as X (features) and y (species),
  3. prints each species' mean of each feature,
  4. draws four labelled plots in one figure,
  5. ends with a Markdown cell saying what you noticed.

Each step below is a few cells of that notebook. On your own computer, make it in the ml-course folder from lesson 0.10 as 00-penguins.ipynb, with penguins.csv beside it. Here, the notebooks below run in the page.

Load the penguins

Start the way every dataset starts (lesson 0.9): load it, check its size, count what's missing.

00-penguins.ipynb

344 rows and 8 columns. Two penguins have none of the four measurements, and 11 have no sex. The question doesn't need sex, so those 9 extra penguins are still useful.

Clean it, and make X and y

Keep each penguin that has all four measurements, then split the table into the features and the labels:

00-penguins.ipynb
  • dropna(subset=features) drops only the 2 penguins with no measurements (lesson 0.9). Plain dropna() would have dropped 11.
  • X is (342, 4): 342 penguins, 4 features. y is (342,): one species for each row of X.
  • names is the three species in alphabetical order. sorted(set(y)) finds the different values; you'll use names to loop over the classes.

Printing how many rows were dropped is a habit worth keeping. When the next version of a dataset arrives and the number jumps from 2 to 200, you'll see it.

Summaries by species

Now compare the species. For each one, a mask picks its rows (lesson 0.3) and mean(axis=0) gives one mean per feature (lesson 0.4):

00-penguins.ipynb

Each line is a species, how many penguins, and its means of bill length, bill depth, flipper length and body mass. Read it before the plots:

  • Gentoo stands out on two features: flippers 21 to 27 mm longer and bodies about 1,350 g heavier than either of the others. But their bills are the shallowest.
  • Adelie and Chinstrap are almost the same size and weight. What separates them is bill length: 38.8 mm against 48.8 mm.
  • No single feature separates all three. Bill length mixes up Chinstrap and Gentoo; mass mixes up Adelie and Chinstrap.

The second and third cells stack the means into a (3, 4) array, one row per species, and use argmax(axis=0) to say which species is largest on each feature. That's the same argmax that turns a model's scores into answers, used here on summaries.

Four plots

Means hide the overlap: two species can have different averages and still have many birds that look alike. Plots show it. This cell draws four in a 2 × 2 grid (lesson 0.8), each species in its own colour:

00-penguins.ipynb
  • Body mass by species (top left): three overlapping histograms. Gentoo's sits mostly to the right of the others, which overlap almost completely. This is where the heavy tail in lesson 0.8's histogram came from: it was the Gentoos.
  • Flipper length and mass (top right): Gentoo is a separate cloud at the top right. Adelie and Chinstrap are mixed together.
  • Bill length and depth (bottom left): three clouds, mostly apart. Two features together separate what no one feature could.
  • Penguins of each species (bottom right): how many of each. Chinstrap has fewer than half as many as Adelie.

alpha=0.6 makes the histograms see-through so the overlap shows. The loop over names is how every plot in the course colours by class: one call per class, each with a label, then legend().

Write what you noticed

A notebook that's only code says what you did, not what you found. End with a Markdown cell in your own words. The note below is editable: change it, add to it, or replace it with what you saw.

00-penguins.ipynb
  1. What I noticed

    • Gentoo penguins are the heaviest, with the longest flippers: flipper length or mass alone picks most of them out.
    • Bill length alone separates Adelie from the other two, but not Chinstrap from Gentoo. Bill depth does that: Gentoo bills are shallower.
    • So two features together (bill length and bill depth) should be enough to tell all three species apart.
    • (Your own notes here.)

Good notes are specific ("Gentoo bills are shallower: 15.0 mm against about 18.3"), say how you know (which summary or plot), and say what it means for what comes next. Here, that's the answer to the question in step 1: yes, there's something to learn, and a model will probably need at least two features to learn it.

Finished

You've done what every machine learning project does before training: looked at the data until you know what's in it. Your notebook

  • loads the data and reports what's missing,
  • keeps every usable row, as X with shape (342, 4) and y with shape (342,),
  • compares the classes with masks and axes,
  • shows them in four labelled plots,
  • says what that means in words.

Before you call it finished, restart the kernel and run every cell from the top (lesson 0.10). If it runs without an error and shows the same numbers, it's done.

Try next:

  • Add a fifth feature: is island useful? Count each species on each island with a mask, or with pd.crosstab(penguins["species"], penguins["island"]).
  • Standardise X (lesson 0.5) and print each species' means again. Which features now look most different between species?
  • Colour the bill scatter plot by sex instead of species, for the Adelies only. Is there a pattern?

Stage 1 teaches the maths a model is made of. Stage 2 comes back to these penguins and trains your first classifier on them.

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning