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, predictions), 51):

        print(f"x{i} = {x:.3f} → {p}")

    

    # --- Plot ---

    plt.scatter(train, [0] * 50, 

                c=["blue" if lbl == "Class1" else "red" for lbl in train_labels], 

                label="Train", marker='o')

    

    plt.scatter(test, [1] * 50, 

                c=["blue" if pred == "Class1" else "red" for pred in predictions], 

                label="Test", marker='x')

    

    plt.yticks([0, 1], ['Train', 'Test'])

    plt.title(f"k = {k} | Accuracy = {accuracy}")

    plt.grid(True)

    plt.legend()

    plt.xlabel("Data")

    plt.tight_layout()

    plt.show()


Comments

Popular posts from this blog

pgm 2

pgm 4