使用python实现knn算法_使用python实现KNN算法
时间: 2023-07-30 19:10:33 浏览: 163
KNN(K-Nearest Neighbors)算法是一种非常简单但又非常有效的分类和回归方法。它的原理是:在训练集中找出与测试数据最接近的K个数据,然后根据这K个数据的分类,确定测试数据的分类。
下面是使用Python实现KNN算法的步骤:
1. 导入必要的库
```python
import numpy as np
from collections import Counter
```
2. 定义KNN类
```python
class KNN:
def __init__(self, k=3):
self.k = k
```
3. 定义距离函数
```python
def euclidean_distance(self, x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
```
4. 训练模型
```python
def fit(self, X, y):
self.X_train = X
self.y_train = y
```
5. 预测函数
```python
def predict(self, X):
y_pred = [self._predict(x) for x in X]
return np.array(y_pred)
```
6. 内部预测函数
```python
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]
```
完整代码如下:
```python
import numpy as np
from collections import Counter
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算法进行分类的示例代码:
```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 = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 构建KNN模型
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)
```
注意:KNN算法的效果非常依赖于数据的质量和特征的选取,因此在实际应用中需要进行多次尝试和调整。
阅读全文