1️⃣ Import Libraries
import numpy as np
Imports NumPy library.
Used for numerical operations and generating random data.
import [Link] as plt
Imports Matplotlib for data visualization.
plt is used to create graphs.
from collections import Counter
Imports Counter.
Used to count occurrences of labels and find the most common
class.
2️⃣ Generate Random Dataset
data = [Link](100)
Generates 100 random numbers between 0 and 1.
These numbers act as data points.
3️⃣ Split Data into Training and Testing
train_data = data[:50]
Selects the first 50 points as training data.
train_labels = ["Class1" if x <= 0.5 else "Class2" for x in train_data]
Assigns labels to training data.
If value ≤ 0.5 → Class1
If value > 0.5 → Class2
Example
0.3 → Class1
0.8 → Class2
test_data = data[50:]
Remaining 50 points are used as test data.
4️⃣ Distance Function
def distance(x1, x2):
return abs(x1 - x2)
Calculates distance between two points.
Uses absolute difference.
Since the data is 1-dimensional, this works like Euclidean distance.
Example
distance(0.6,0.4) = |0.6-0.4| = 0.2
5️⃣ KNN Function
def knn(train_data, train_labels, test_point, k):
Defines KNN classifier function.
Parameters:
o train_data → training dataset
o train_labels → labels of training data
o test_point → new point to classify
o k → number of neighbors
6️⃣ Calculate Distance from Test Point
d = [(distance(test_point, train_data[i]), train_labels[i]) for i in
range(len(train_data))]
Calculates distance between test point and every training
point.
Stores result as (distance, label).
Example:
[(0.1,'Class1'), (0.2,'Class2'), (0.05,'Class1')]
7️⃣ Sort Distances
[Link]()
Sorts the list based on distance (smallest first).
Closest neighbors appear at the top.
8️⃣ Select k Nearest Neighbors
neighbors = [label for _, label in d[:k]]
Takes first k elements from sorted list.
Extracts their labels.
Example (k=3):
['Class1','Class1','Class2']
9️⃣ Find Most Common Class
return Counter(neighbors).most_common(1)[0][0]
Steps:
1. Counter(neighbors) → counts labels
2. most_common(1) → returns most frequent label
3. [0][0] → extracts class name
Example:
Class1 appears 2 times → predicted class = Class1
🔟 Set Value of k
k=3
Number of neighbors used for classification.
1️⃣1️⃣ Predict Labels for Test Data
predictions = [knn(train_data, train_labels, x, k) for x in test_data]
Runs KNN algorithm for every test point.
Stores predicted class labels.
Example:
['Class1','Class2','Class1',...]
1️⃣2️⃣ Plot Training Data
[Link](train_data, [0]*50, c=["blue" if l=="Class1" else "red" for l in
train_labels], label="Training")
Creates scatter plot for training data.
All points plotted at y = 0.
Colors:
o Blue → Class1
o Red → Class2
1️⃣3️⃣ Plot Test Data
[Link](test_data, [1]*50, c=["blue" if p=="Class1" else "red" for p in
predictions], marker="x", label="Test")
Plots test data points.
Placed at y = 1.
marker="x" shows they are test points.
1️⃣4️⃣ Add Graph Title
[Link]("KNN Classification")
Adds title to the graph.
1️⃣5️⃣ Add Legend
[Link]()
Displays training and test labels.
1️⃣6️⃣ Display Graph
[Link]()
Shows the final classification graph.