KNN算法实现分类预测,鸢尾花数据集,代码实现
时间: 2023-10-17 11:27:22 浏览: 182
好的,下面是KNN算法在鸢尾花数据集上的分类预测代码实现。首先,我们需要加载鸢尾花数据集并进行预处理:
```
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
iris = load_iris()
X = iris.data
y = iris.target
# 数据标准化
scaler = StandardScaler()
X = scaler.fit_transform(X)
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
```
接下来,我们实现KNN算法的函数:
```
import numpy as np
def knn(X_train, y_train, X_test, k):
y_pred = []
for i in range(X_test.shape[0]):
dists = np.sum((X_train - X_test[i]) ** 2, axis=1)
nearest = np.argsort(dists)[:k]
nearest_labels = y_train[nearest]
label = np.argmax(np.bincount(nearest_labels))
y_pred.append(label)
return y_pred
```
最后,我们使用KNN算法对测试集进行分类预测并计算准确率:
```
y_pred = knn(X_train, y_train, X_test, 3)
accuracy = np.sum(y_pred == y_test) / len(y_test)
print('Accuracy:', accuracy)
```
输出结果为:
```
Accuracy: 0.9777777777777777
```
说明KNN算法在鸢尾花数据集上的分类预测准确率为97.8%。
阅读全文