Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

🌸 Iris KNN Classifier

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).


🧠 Project Overview

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.


⚙️ Methodology

  1. Dataset Loading

    • Loaded the Iris dataset from sklearn.datasets.
    • Inspected feature names, target classes, and data structure.
  2. Exploratory Data Analysis (EDA)

    • Visualized feature relationships using pair plots and scatter matrices.
    • Identified feature separability across different species.
  3. Data Splitting

    • Used train_test_split() to create training (75%) and testing (25%) sets.
    • Set random_state=42 for reproducibility.
  4. 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.
  5. 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%).

🤖 About the KNN Algorithm

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.


🧩 Estimator Classes in Scikit-learn

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.

📈 Results

Metric Score
Training Accuracy 100%
Test Accuracy 100%

The model performed excellently due to the clear feature boundaries in the dataset.


🧰 Tech Stack

  • Python 3.x
  • scikit-learn
  • pandas
  • matplotlib
  • NumPy

🚀 Next Steps

  • Experiment with different k values 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.

📚 References


✨ Author

Jeremiah Hemben
📧 [Email]:Jeremiahhemben@gmail.com
💼 LinkedIn
🧠 Exploring AI, Data Science, and Smart Construction Systems

About

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 analysis, model instantiation, training, and performance evaluation using a reproducible 75/25 train–test split

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages