-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn.py
47 lines (39 loc) · 1.62 KB
/
knn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import numpy as np
from collections import Counter
# Function to calculate the Euclidean distance between two points
def euclidean_distance(x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
class KNN:
def __init__(self, k=3):
self.k = k
# Fit the model with the training data
def fit(self, X, y):
self.X_train = X
self.y_train = y
# Predict the class labels for the provided data
def predict(self, X):
predicted_labels = [self._predict(x) for x in X]
return np.array(predicted_labels)
# Helper function to predict the class label for a single data point
def _predict(self, x):
# Compute distances between x and all examples in the training set
distances = [euclidean_distance(x, x_train) for x_train in self.X_train]
# Sort by distance and return indices of the first k neighbors
#find the indices of the smallest values in an array of distances.
k_indices = np.argsort(distances)[:self.k]
# Extract the labels of the k nearest neighbor training samples
k_nearest_labels = [self.y_train[i] for i in k_indices]
# Return the most common class label
most_common = Counter(k_nearest_labels).most_common(1)
return most_common[0][0]
# Example usage
if __name__ == "__main__":
# Sample data
X_train = np.array([[1, 2], [2, 3], [3, 4], [6, 7], [7, 8], [8, 9]])
y_train = np.array([0, 0, 0, 1, 1, 1])
X_test = np.array([[2, 3], [6, 6]])
# Create KNN classifier
knn = KNN(k=3)
knn.fit(X_train, y_train)
predictions = knn.predict(X_test)
print("Predictions:", predictions)