如何用python实现KNN算法
时间: 2024-04-30 18:19:51 浏览: 113
利用 Python 实现 KNN 算法
KNN是一种简单而有效的分类算法,这里提供一个Python实现的KNN算法示例:
```python
import numpy as np
class KNN:
def __init__(self, k=3):
self.k = k
def fit(self, X_train, y_train):
self.X_train = X_train
self.y_train = y_train
def predict(self, X_test):
y_pred = []
for x in X_test:
distances = []
for i, x_train in enumerate(self.X_train):
distance = np.sqrt(np.sum(np.square(x - x_train)))
distances.append((distance, self.y_train[i]))
distances.sort()
k_nearest_neighbors = distances[:self.k]
k_nearest_labels = [label for _, label in k_nearest_neighbors]
y_pred.append(max(set(k_nearest_labels), key=k_nearest_labels.count))
return y_pred
```
使用示例:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)
knn = KNN(k=3)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
这里使用了sklearn中的鸢尾花数据集进行测试,将数据集划分为训练集和测试集,然后使用KNN算法进行分类,并计算分类准确率。
阅读全文