下面是一个简单的KNN算法的Python代码示例:
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]
使用示例:
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]
这个示例中使用的是欧氏距离作为距离度量方法,同时实现了一个简单的KNN类,其中的fit()
方法用于训练模型,predict()
方法用于预测新样本的标签。KNN类的_predict()
方法用于计算单个样本的预测结果。