pgm5
import numpy as np import matplotlib.pyplot as plt from collections import Counter # --- Data Setup --- np.random.seed(42) data = np.random.rand(100) train, test = data[:50], data[50:] train_labels = ["Class1" if x <= 0.5 else "Class2" for x in train] true_labels = ["Class1" if x <= 0.5 else "Class2" for x in test] # --- KNN Function --- def knn(x, train_data, labels, k): distances = sorted(((abs(x - xi), yi) for xi, yi in zip(train_data, labels))) nearest = distances[:k] return Counter(yi for _, yi in nearest).most_common(1)[0][0] # --- Classification and Plotting --- k_values = [1, 3, 5, 20, 30] for k in k_values: predictions = [knn(x, train, train_labels, k) for x in test] accuracy = round(np.mean([pred == true for pred, true in zip(predictions, true_labels)]), 2) print(f"\n--- k = {k} | Accuracy: {accuracy} ---") for i, (x, p) in enumerate(zip(test, predi...