A machine learning classification experiment applying the K-Nearest Neighbors (KNN) estimator from scikit-learn to the Iris dataset.
The project covers data exploration, feature visualization, model instantiation, training, and performance evaluation using a reproducible 75/25 train–test split (random_state=42).
The Iris dataset is one of the most popular datasets in classical machine learning and statistics.
It consists of 150 samples of iris flowers categorized into three species: Setosa, Versicolor, and Virginica.
Each flower is described by four features — sepal length, sepal width, petal length, and petal width.
This project applies the K-Nearest Neighbors (KNN) algorithm to classify flowers based on these features.
The model achieved a 100% accuracy score on the test set, highlighting the strength of KNN on well-separated datasets.
-
Dataset Loading
- Loaded the Iris dataset from
sklearn.datasets. - Inspected feature names, target classes, and data structure.
- Loaded the Iris dataset from
-
Exploratory Data Analysis (EDA)
- Visualized feature relationships using pair plots and scatter matrices.
- Identified feature separability across different species.
-
Data Splitting
- Used
train_test_split()to create training (75%) and testing (25%) sets. - Set
random_state=42for reproducibility.
- Used
-
Model Instantiation
- Imported and instantiated the KNN estimator from
sklearn.neighbors. - Created an instance of the estimator class to access
.fit(),.predict(), and.score()methods.
- Imported and instantiated the KNN estimator from
-
Model Training and Evaluation
- Trained the KNN model using the training set.
- Evaluated accuracy using the test set.
- Achieved a perfect accuracy of 1.00 (100%).
K-Nearest Neighbors (KNN) is a simple yet powerful non-parametric algorithm used for classification and regression.
It classifies a new data point based on the majority label of its K closest neighbors in the feature space.
Think of it like this —
“Tell me who your neighbors are, and I’ll tell you who you are.”
If a new flower is surrounded mostly by Setosa samples, KNN predicts it as Setosa.
In everyday terms, it’s like moving into a new neighborhood — if most houses nearby are painted blue, the new one will likely be blue too.
In scikit-learn, algorithms like KNN are implemented as estimator classes.
An estimator is a class that implements two main methods:
.fit()— to learn patterns from training data..predict()— to make predictions on new data.
Instantiating an object from an estimator class (e.g., knn = KNeighborsClassifier(n_neighbors=3)) is essential because it allows us to:
- Store model parameters and training results inside that object.
- Access important methods like
.score()for evaluation. - Keep code modular and reusable for multiple experiments.
| Metric | Score |
|---|---|
| Training Accuracy | 100% |
| Test Accuracy | 100% |
The model performed excellently due to the clear feature boundaries in the dataset.
- Python 3.x
- scikit-learn
- pandas
- matplotlib
- NumPy
- Experiment with different
kvalues to study their impact on accuracy. - Visualize decision boundaries.
- Compare KNN results with other algorithms (e.g., Logistic Regression, SVM).
- Add cross-validation and confusion matrix visualization.
Jeremiah Hemben
📧 [Email]:Jeremiahhemben@gmail.com
💼 LinkedIn
🧠 Exploring AI, Data Science, and Smart Construction Systems