下面是一個簡單的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()
方法用于計算單個樣本的預測結果。