Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

Iris Flower Classification

Classic classification project - explore Iris features, visualize pairs, and build a simple classifier.

Goal

Use the Iris dataset to:

  • Perform EDA
  • Visualize feature relationships
  • Train a simple classifier

Analysis pipeline

diagram Iris classification pipeline mermaid
From raw measurements to a trained baseline classifier.

Load dataset

Python
import pandas as pd
from sklearn.datasets import load_iris
 
iris = load_iris(as_frame=True)
df = iris.frame
print(df.head())

Pair plot

Python
import seaborn as sns
 
sns.pairplot(df, hue="target")

Visualize it

Petal length and petal width separate the three species almost perfectly — much better than sepal measurements. This is exactly why the pairplot is step one: it tells you which features will make the classifier's job easy.

sketch Petal length vs petal width, by species p5.js
Each dot is one flower; the three species form clearly separate clusters.

Train/test split + model

Python
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
 
X = df.drop(columns=["target"])
y = df["target"]
 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
 
clf = LogisticRegression(max_iter=1000)
clf.fit(X_train, y_train)
print("Accuracy:", clf.score(X_test, y_test))

Deliverable

  • Which features separate classes best?
  • Any overlap between species?
  • Baseline accuracy

Check your understanding

quizCheck yourself
  1. Why is the pair plot the first step before training the classifier?

    Show answer

    A — It shows whether the classes separate before you trust an accuracy number — Looking at the data first tells you which features will make the classifier's job easy.

  2. Which features separate the three species almost perfectly?

    Show answer

    A — Petal length and petal width — The lesson notes petal measurements separate species much better than sepal measurements.

  3. What does stratify=y do in train_test_split?

    Show answer

    A — Keeps the class proportions the same in train and test — With only 150 samples, stratifying prevents a skewed class mix in a small test set.

  4. What does test_size=0.2 mean?

    Show answer

    A — 20 percent of rows are held out for testing — The model trains on the other 80 percent and is evaluated on the held-out 20 percent.

🧪 Try It Yourself

Exercise 1 – Class balance

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Mean feature by species

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Train/test split

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did