How to write KNN algorithm Python code?

Here is a simple Python code example for the KNN algorithm.

import numpy as np
from collections import Counter

def euclidean_distance(x1, x2):
    return np.sqrt(np.sum((x1 - x2) ** 2))

class KNN:
    def __init__(self, k=3):
        self.k = k
    
    def fit(self, X, y):
        self.X_train = X
        self.y_train = y
    
    def predict(self, X):
        y_pred = [self._predict(x) for x in X]
        return np.array(y_pred)
    
    def _predict(self, x):
        # 计算所有训练样本与待预测样本的距离
        distances = [euclidean_distance(x, x_train) for x_train in self.X_train]
        # 根据距离排序并获取前k个样本的索引
        k_indices = np.argsort(distances)[:self.k]
        # 获取前k个样本的标签
        k_labels = [self.y_train[i] for i in k_indices]
        # 返回出现次数最多的标签作为预测结果
        most_common = Counter(k_labels).most_common(1)
        return most_common[0][0]

Example of use:

X_train = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]])
y_train = np.array([0, 0, 1, 1, 0, 1])

knn = KNN(k=3)
knn.fit(X_train, y_train)

X_test = np.array([[2, 3], [6, 9], [1, 1]])
y_pred = knn.predict(X_test)

print(y_pred)  # 输出:[0, 1, 0]

In this example, the Euclidean distance is used as the distance metric method, and a simple KNN class is implemented, where the fit() method is used to train the model, the predict() method is used to predict the labels of new samples. The _predict() method of the KNN class is used to calculate the prediction result for a single sample.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds