A comprehensive machine learning project for classifying Iris flower species using multiple classifiers with rigorous evaluation methodology.
- Project Overview
- Dataset
- Methodology
- Results
- Project Structure
- Installation
- Usage
- Key Visualizations
- Future Improvements
- Author
This project implements a complete machine learning pipeline to classify Iris flowers into three species based on their morphological measurements. The workflow includes:
- Data Cleaning — Handling missing values, duplicates, and feature selection
- Exploratory Data Analysis — Correlation analysis, distribution visualization, and class separability assessment
- Feature Engineering — Label encoding, train-test splitting with stratification, and feature scaling
- Model Training — Five different classifiers (SVM, Logistic Regression, Decision Tree, Random Forest, KNN)
- Robust Evaluation — Stratified 5-fold cross-validation with multi-metric assessment
- Hyperparameter Tuning — GridSearchCV optimization for top-performing models
- Model Comparison — Side-by-side performance analysis with visualizations
The Iris dataset is one of the most well-known datasets in machine learning, introduced by British statistician Ronald Fisher in 1936. It contains 150 samples from three species of Iris flowers, with 50 samples per species.
| Feature | Description | Unit |
|---|---|---|
| Sepal Length | Length of the sepal | cm |
| Sepal Width | Width of the sepal | cm |
| Petal Length | Length of the petal | cm |
| Petal Width | Width of the petal | cm |
| Species | Target class (Iris-setosa, Iris-versicolor, Iris-virginica) | — |
Source: UCI Machine Learning Repository
- Dropped the
Idcolumn (non-informative) - Removed duplicate rows
- Encoded species labels using
LabelEncoder - Split data 80/20 with stratification to preserve class balance
- Scaled features using
StandardScaler(critical for SVM, Logistic Regression, and KNN)
| Model | Why Included |
|---|---|
| Support Vector Machine (SVM) | Excellent for high-dimensional spaces; uses kernel trick for non-linear boundaries |
| Logistic Regression | Strong baseline linear classifier; interpretable coefficients |
| Decision Tree | Non-parametric; handles non-linear relationships; interpretable |
| Random Forest | Ensemble of trees; reduces overfitting; robust to noise |
| K-Nearest Neighbors | Instance-based learning; simple yet effective for small datasets |
- Train-Test Split: 80/20 with stratified sampling
- Cross-Validation: 5-fold stratified CV for robust performance estimation
- Metrics: Accuracy, Precision, Recall, F1-Score, Confusion Matrix
- Hyperparameter Tuning: GridSearchCV with exhaustive parameter search
| Model | Mean CV Accuracy | Std Deviation |
|---|---|---|
| Support Vector Machine | ~97-100% | Low |
| Logistic Regression | ~96-100% | Low |
| Random Forest | ~95-100% | Low |
| Decision Tree | ~95-100% | Moderate |
| K-Nearest Neighbors | ~95-100% | Low |
Note: Exact values depend on random seed and data splits. Run the notebook for precise results.
- Petal dimensions are the most discriminative features — Petal length and petal width are highly correlated (r=0.96) and provide clear class separation
- Iris-setosa is linearly separable from the other two species
- Feature scaling is essential for distance-based algorithms (SVM, KNN)
- SVM with RBF kernel achieves the best performance after hyperparameter tuning
Iris_Flower_Classification_MachineLearning/
│
├── Iris_Flower_Classification.ipynb # Main Jupyter notebook
├── Iris.csv # Dataset
└── README.md # This file
- Python 3.8 or higher
- pip or conda package manager
-
Clone the repository:
git clone https://github.com/yourusername/Iris_Flower_Classification_MachineLearning.git cd Iris_Flower_Classification_MachineLearning -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install pandas numpy matplotlib seaborn scikit-learn jupyter
-
Launch Jupyter Notebook:
jupyter notebook
-
Open
Iris_Flower_Classification.ipynband run all cells.
The notebook is designed to be run sequentially from top to bottom. Each section is clearly marked with markdown headers:
- Section 1-3: Setup and data preparation
- Section 4: EDA with interactive visualizations
- Section 5: Preprocessing pipeline
- Section 6-7: Model training and cross-validation
- Section 8: Hyperparameter optimization
- Section 9-10: Detailed evaluation and comparison
- Section 11: Summary and conclusions
The notebook generates several publication-quality visualizations:
- Correlation Heatmap — Feature relationships and multicollinearity
- Violin Plots — Distribution shape and class separability
- Pair Plot — All pairwise feature relationships colored by species
- Box Plots — Feature ranges and outlier detection
- Histograms — Feature distributions per species
- Confusion Matrices — Per-class classification accuracy
- Model Comparison Charts — Bar and box plots of cross-validation scores
Naveed Ahmad
If you found this project useful, please consider giving it a star on GitHub!
This project is open source and available under the MIT License.