knn算法python代码
时间: 2023-10-14 12:30:54 浏览: 61
knn算法python
下面是使用Python实现的KNN算法代码:
```python
import numpy as np
class KNN:
def __init__(self, k=3):
self.k = k
def euclidean_distance(self, x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
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 = [self.euclidean_distance(x, x_train) for x_train in self.X_train]
k_indices = np.argsort(distances)[:self.k]
k_nearest_labels = [self.y_train[i] for i in k_indices]
most_common = Counter(k_nearest_labels).most_common(1)
return most_common[0][0]
```
其中,KNN类包含了以下方法:
- `__init__(self, k=3)`:初始化KNN算法,其中k为最近邻的数量,默认值为3。
- `euclidean_distance(self, x1, x2)`:计算两个样本之间的欧几里得距离。
- `fit(self, X, y)`:用于训练KNN模型,其中X为训练集的特征矩阵,y为训练集的标签。
- `predict(self, X)`:用于预测新的数据,其中X为测试集的特征矩阵。
- `_predict(self, x)`:内部方法,用于预测单个数据。
使用KNN算法进行分类时,可以使用如下的步骤:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from collections import Counter
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
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)
```
以上代码使用了Iris数据集进行了分类,并输出了分类准确率。
阅读全文