Untitled
unknown
plain_text
10 months ago
3.0 kB
15
Indexable
import numpy as np
from collections import Counter
class KNN:
def __init__(self, k=3):
"""Initialize the KNN model with the number of neighbors (k)"""
self.k = k
def fit(self, X_train, y_train):
"""Fit the model with training data"""
self.X_train = X_train
self.y_train = y_train
def predict(self, X_test):
"""Predict the class labels for the given test data"""
predictions = [self._predict(x) for x in X_test]
return np.array(predictions)
def _predict(self, x):
"""Predict the class label for a single sample"""
distances = [self._euclidean_distance(x, x_train) for x_train in self.X_train]
k_indices = np.argsort(distances)[:self.k]
k_nearest_labels = [self.y_train[i] for i in k_indices]
most_common = Counter(k_nearest_labels).most_common(1)
return most_common[0][0]
def _euclidean_distance(self, x1, x2):
"""Compute the Euclidean distance between two points"""
return np.sqrt(np.sum((x1 - x2) ** 2))
def accuracy(self, X_test, y_test):
"""Calculate the accuracy of the model on the test dataset"""
predictions = self.predict(X_test)
correct = np.sum(predictions == y_test)
accuracy = correct / len(y_test) * 100
return accuracy
# Function to generate a dataset for Class A and Class B
def generate_data_for_80_accuracy():
# Training data for Class A (Class 0) and Class B (Class 1)
X_train_class_a = np.array([[160, 55], [165, 58], [162, 57], [170, 62], [168, 60],
[155, 50], [159, 54], [160, 56], [163, 59], [167, 61]]) # Class A
X_train_class_b = np.array([[180, 85], [185, 90], [190, 95], [175, 80], [185, 88],
[178, 85], [182, 92], [177, 78], [184, 93], [188, 100]]) # Class B
X_train = np.vstack([X_train_class_a, X_train_class_b])
# Labels: class 0 for Class A, class 1 for Class B
y_train = np.array([0]*10 + [1]*10)
# Test data: 5 points, some near the decision boundary
X_test = np.array([
[155, 52], # Class A
[160, 58], # Class A
[167, 70], # Near boundary
[180, 85], # Class B
[190, 98] # Class B
])
# Correct labels for test data
y_test = np.array([0, 0, 1, 1, 1])
return X_train, y_train, X_test, y_test
# Main function
if __name__ == "__main__":
# Generate data for Class A and Class B
X_train, y_train, X_test, y_test = generate_data_for_80_accuracy()
# Ask for the value of k
k = int(input("Enter the value of k (number of nearest neighbors): "))
# Initialize the KNN model
knn = KNN(k)
knn.fit(X_train, y_train)
# Evaluate on the test dataset
accuracy = knn.accuracy(X_test, y_test)
print(f"\nAccuracy: {accuracy:.2f}%")
Editor is loading...
Leave a Comment