AI & ML
Iris Species Classification: The Machine Learning Workflow
End-to-end Iris species classification using Python and scikit-learn, covering data validation, EDA, model comparison, evaluation, interpretation & prediction.
Iris Species Classification: The Machine Learning Workflow
Machine learning projects are often explained from the model training step, but in real projects, the model is only one part of the full workflow. Before training a model, we need to understand the data, validate it, explore patterns, compare approaches, evaluate results, and interpret the model’s behavior.
In this project, I worked on the classic Iris Species Classification problem using Python and scikit-learn. The full Jupyter Notebook is available on GitHub, and I have also created a video walk through for this project.
GitHub Repository: rajks24/iris-species-classification
Project Objective
The Iris dataset contains three flower species:
- Setosa
- Versicolor
- Virginica
Each flower has four numeric features:
- Sepal length
- Sepal width
- Petal length
- Petal width
The task is to build a supervised machine learning model that can classify a flower into one of the three species.
Step 1: Data Loading and Quality Checks
The first step was to load the dataset and inspect its structure. Before jumping into modeling, I checked the basic data quality:
- Data types
- Missing values
- Duplicate records
- Unique values
- Class distribution
The dataset has 150 observations, 4 numeric predictors, and 3 balanced target classes with 50 records per species. There were no missing values, and the target distribution was perfectly balanced. This made the dataset suitable for a straightforward classification workflow. A key learning point here is that even for clean datasets, data-quality checks should not be skipped. They help confirm whether the dataset is ready for modeling.
Step 2: Exploratory Data Analysis
Exploratory data analysis helped reveal which features are useful for classification. The descriptive statistics and boxplots showed that petal length and petal width vary much more across species than the sepal measurements. This gave an early signal that petal features may be more important for classification. The pairplot made this even clearer. Setosa was clearly separated from the other two species, while versicolor and virginica had some overlap. This visual pattern later helped explain the model’s errors. Most confusion happened between versicolor and virginica, which matched what we observed during EDA.
Step 3: Correlation and Feature Relevance
The correlation heatmap showed that petal length and petal width are highly correlated. This means they carry similar information. At the same time, feature relevance analysis showed that both petal features are strongly related to the target classes. In a larger dataset, we might consider feature selection, dimensionality reduction, or more advanced preprocessing. But for this educational project, all four features were retained because the dataset is small and the selected model can handle this level of redundancy.
Step 4: Model Training Strategy
The dataset was split into training and test sets using a stratified 80/20 split. This ensured that each species remained equally represented in both sets. The model comparison was performed using cross-validation on the training data only. This is important because the final test set should remain untouched until the end. I compared multiple models:
- Logistic Regression
- K-Nearest Neighbors
- Random Forest
- Dummy baseline
For models that need scaling, preprocessing was kept inside a scikit-learn Pipeline. This avoids data leakage during cross-validation because the scaler is fitted only on the training fold.
Step 5: Model Comparison
The cross-validation results showed that Logistic Regression and K-Nearest Neighbors performed very similarly, both achieving strong accuracy and macro F1 scores. Random Forest was also close, while the dummy baseline performed much worse, confirming that the models were learning useful patterns from the data. I selected Logistic Regression as the final model because it performed strongly, was simple, produced probabilities, and was easier to interpret. This is an important point: the best model is not always the most complex one. For small and structured datasets, a simpler model can often be the better choice.
Step 6: Final Evaluation
After selecting the final model, I trained it on the full training set and evaluated it once on the holdout test set. The model achieved:
- Test accuracy: 0.933
- Correct predictions: 28 out of 30
Setosa was classified perfectly, which matched the EDA findings. The two errors were between versicolor and virginica, which also matched the visual overlap seen earlier. This is a good example of why EDA matters. It helps us understand not only model performance, but also why certain errors happen.
Step 7: Error Analysis and Confidence
Instead of only looking at accuracy, I also checked prediction confidence. Both incorrect predictions were low-confidence cases. This means the model was uncertain, not confidently wrong. This is useful in real-world machine learning systems. If a model is uncertain, we can design the system to send such cases for manual review rather than blindly trusting every prediction. For production systems, this would require more data and proper threshold validation, but the idea is important.
Step 8: Feature Importance
Permutation importance showed that petal width and petal length had the strongest impact on model performance. This confirmed what we already saw in the visual analysis. The petal features are the most useful predictors for classifying iris species. However, feature importance should not be treated as causation. It only tells us how much the model depends on a feature for prediction.
Key Takeaways
This project is a simple but complete example of a supervised machine learning workflow. The main takeaways are:
- Always validate the dataset before modeling.
- Use EDA to understand patterns and expected errors.
- Keep preprocessing inside pipelines to avoid leakage.
- Use cross-validation for model comparison.
- Keep the test set untouched until final evaluation.
- Look beyond accuracy by analyzing errors and confidence.
- Use feature importance to understand model behavior.
Conclusion
The Iris Species Classification project is small, but it covers many important machine learning concepts. It shows how to move from data understanding to model training, evaluation, interpretation, and prediction in a structured way. For anyone learning machine learning, this is a useful foundation before moving into larger real-world datasets and more complex models. You can find the complete notebook and code in the GitHub repository, and the full video walk through is available on YouTube.