Created
May 11, 2025 21:20
-
-
Save plushycat/303c758e33a09472ad13a091e6c04bbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from collections import Counter | |
# Generate data | |
np.random.seed(42) # Added for reproducibility | |
data = np.random.rand(40) | |
train_data, test_data = data[:20], data[20:] | |
train_labels = ["Class1" if x <= 0.5 else "Class2" for x in train_data] | |
# Define KNN classifier | |
def knn_predict(train_data, train_labels, test_data, k): | |
return [Counter(sorted([(abs(x-point), label) for x, label in zip(train_data, train_labels)], | |
key=lambda x: x[0])[:k]).most_common(1)[0][0] for point in test_data] | |
# Test with multiple k values | |
k_values = [1, 2, 3, 4, 5, 20, 30] | |
results = {k: knn_predict(train_data, train_labels, test_data, k) for k in k_values} | |
# Print results | |
print("--- k-Nearest Neighbors Classification ---") | |
for k, labels in results.items(): | |
print(f"\nResults for k = {k}:") | |
for i, (point, label) in enumerate(zip(test_data, labels)): | |
print(f"Point x{i+21} (value: {point:.4f}) is classified as {label}") | |
# Visualize results | |
for k, classified_labels in results.items(): | |
plt.figure(figsize=(10, 8)) | |
# Plot training data | |
plt.scatter(train_data, [0]*len(train_data), | |
c=["blue" if l == "Class1" else "red" for l in train_labels], | |
marker="o", label="Training Data") | |
# Plot test data by class | |
class_points = {"Class1": [], "Class2": []} | |
for point, label in zip(test_data, classified_labels): | |
class_points[label].append(point) | |
for label, color in [("Class1", "blue"), ("Class2", "red")]: | |
if class_points[label]: | |
plt.scatter(class_points[label], [1]*len(class_points[label]), | |
c=color, marker="x", label=f"{label} (Test)") | |
plt.title(f"k-NN Classification Results for k = {k}") | |
plt.xlabel("Data Points"), plt.ylabel("Classification Level") | |
plt.legend(), plt.grid(True) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment