用python实现KNN算法
时间: 2023-11-10 20:14:51 浏览: 102
python实现KNN算法
5星 · 资源好评率100%
KNN算法是一种常用的分类算法,下面是用Python实现KNN算法的示例代码:
```python
import numpy as np
import math
# 计算欧几里得距离
def euclidean_distance(x1, x2):
distance = 0
for i in range(len(x1)):
distance += (x1[i] - x2[i]) ** 2
return math.sqrt(distance)
# KNN算法
def knn(X_train, y_train, X_test, k):
predictions = []
for i in range(len(X_test)):
distances = []
for j in range(len(X_train)):
distance = euclidean_distance(X_test[i], X_train[j])
distances.append((distance, y_train[j]))
distances.sort()
neighbors = distances[:k]
classes = {}
for neighbor in neighbors:
if neighbor[1] in classes:
classes[neighbor[1]] += 1
else:
classes[neighbor[1]] = 1
predictions.append(max(classes, key=classes.get))
return predictions
```
其中,euclidean_distance函数用于计算欧几里得距离,knn函数实现了KNN算法,X_train和y_train分别表示训练集的特征和标签,X_test表示测试集的特征,k表示最近邻的个数。返回predictions列表,包含每个测试样本的预测结果。
阅读全文