Skip to content

Iris Flower Classification

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

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

Pair plot

Pair plot
import seaborn as sns
 
sns.pairplot(df, hue="target")
Pair plot
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

Train a classifier
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))
Train a classifier
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

🧪 Try It Yourself

Exercise 1 – Class balance

Exercise 2 – Mean feature by species

Exercise 3 – Train/test split

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did