🧠 AI with Python - 📉 KNN Classifier


Description:

Ever wondered how machines can tell flowers apart just by looking at numbers? That’s exactly what the K-Nearest Neighbors (KNN) algorithm does!

Today’s AI Spark dives into how we can use KNN to classify Iris flowers based on their features — and it’s much simpler than you’d expect.


Dataset

We’re using the famous Iris dataset, which contains 150 rows of data with:

  • Sepal Length
  • Sepal Width
  • Petal Length
  • Petal Width

…along with the species of the flower (Setosa, Versicolor, Virginica).


What is KNN?

K-Nearest Neighbors is a lazy learning algorithm. It doesn’t actually “learn” during training — instead, it memorizes the data and classifies new points based on the majority vote of the closest K neighbors.

Think of it as:

“Show me the 3 flowers most similar to this one — whichever species is most common among them is the prediction.”


Steps Involved:

Let’s break down what we did:

1. Load the dataset

Using sklearn.datasets.load_iris().

2. Split into training and testing

We keep 20% of the data aside for testing using train_test_split().

3. Train the model

from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)

We used k = 3 — so the classifier looks at the 3 closest neighbors to make a prediction.

4. Make predictions

predictions = model.predict(X_test)

5. Evaluate it

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)

Sample Output:

Accuracy: 1.0
Prediction for sample [5.1, 3.5, 1.4, 0.2]: setosa

📘 Quick Recap:

Step Tool/Function Used
Load dataset load_iris()
Train-Test Split train_test_split()
Train model KNeighborsClassifier()
Evaluate accuracy_score()

Conclusion

KNN may be simple, but it’s surprisingly powerful for many real-world tasks — especially classification problems where “similarity” matters.


Code Snippet:

# Import required libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score


# Load dataset and split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=42)


# Train KNN model
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)


# Evaluate the model
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

Link copied!

Comments

Add Your Comment

Comment Added!