Machine Learning Iris Flower Classification Project

Machine Learning project 09

Machine Learning Iris Flower Classification Project

Build and evaluate a complete iris flower classification workflow with reproducible Python code, explicit metrics, and honest limitations.

Explore Machine Learning training in VizagView all project ideas

Dataset and objective

The classic built-in Iris dataset with 150 flowers, four measurements, and three species labels.

Skills practised

  • Multiclass classification
  • Feature scaling
  • KNN
  • Confusion matrices

Requirements

  • Python 3.10 or later
  • A terminal or command prompt
  • python -m pip install scikit-learn
  • About 45-60 minutes to build and review

Machine Learning workflow

  1. Data: The classic built-in Iris dataset with 150 flowers, four measurements, and three species labels.
  2. Preprocessing: A stratified holdout split preserves class balance, and StandardScaler is applied inside the model pipeline.
  3. Model: Five-neighbour KNN classifies species from scaled sepal and petal measurements.
  4. Evaluation: Confusion matrix and per-species precision, recall, and F1 scores.

Complete Python code

Save the code as ml_iris_classification.py. The random state and data handling are included so the result can be reproduced and reviewed.

"""Classify iris species with a scaled k-nearest-neighbours pipeline."""

from __future__ import annotations

from sklearn.datasets import load_iris
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler


def train_model(random_state: int = 42):
    iris = load_iris(as_frame=True)
    x_train, x_test, y_train, y_test = train_test_split(
        iris.data,
        iris.target,
        test_size=0.25,
        random_state=random_state,
        stratify=iris.target,
    )
    model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5))
    model.fit(x_train, y_train)
    predictions = model.predict(x_test)
    return model, iris, y_test, predictions


def main() -> None:
    _, iris, y_test, predictions = train_model()
    print("Iris Flower Classification")
    print("Confusion matrix:")
    print(confusion_matrix(y_test, predictions))
    print(classification_report(y_test, predictions, target_names=iris.target_names, zero_division=0))
    print("This classic dataset is useful for learning but too small to represent field deployment.")


if __name__ == "__main__":
    main()

How the pipeline works

A stratified holdout split preserves class balance, and StandardScaler is applied inside the model pipeline.

Five-neighbour KNN classifies species from scaled sepal and petal measurements. Confusion matrix and per-species precision, recall, and F1 scores.

Run the project

  1. Create and activate a virtual environment.
  2. Install the dependencies with python -m pip install scikit-learn.
  3. Run python ml_iris_classification.py.
  4. Review every printed metric together with the limitation below; a single score never proves deployment readiness.

How to interpret the evaluation

Classification projects report class-aware metrics so majority classes do not hide weak performance. Regression projects report errors in target units and include R-squared or a simple baseline where appropriate. Always verify the split strategy matches how new data will arrive.

Accuracy, ethics, and safety limits

The small, clean historical dataset is ideal for learning but does not represent messy field measurements or biodiversity decisions.

Ways to extend the project

Plot decision regions, tune K with cross-validation, compare logistic regression and trees, and examine how scaling changes neighbour distances.

Continue learning Machine Learning

Try the next project, return to the Softenant project library, or explore the Machine Learning course in Vizag for guided data preparation, model evaluation, and portfolio feedback.