numpy实现knn算法鸢尾花代码
时间: 2024-04-19 14:06:51 浏览: 108
下面是用numpy实现的knn算法鸢尾花代码:
```python
import numpy as np
from sklearn.datasets import load_iris
# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target
# 定义knn算法函数
def knn(X_train, y_train, X_test, k):
# 计算测试样本与所有训练样本的距离
dists = np.sqrt(np.sum((X_test - X_train)**2, axis=1))
# 取距离最近的k个样本的标签
labels = y_train[np.argsort(dists)[:k]]
# 统计k个样本中最多的标签
pred_label = np.argmax(np.bincount(labels))
return pred_label
# 将数据集分成训练集和测试集
test_ratio = 0.3
num_test = int(test_ratio * len(X))
num_train = len(X) - num_test
train_indices = np.random.choice(range(len(X)), num_train, replace=False)
test_indices = np.array(list(set(range(len(X))) - set(train_indices)))
X_train = X[train_indices]
y_train = y[train_indices]
X_test = X[test_indices]
y_test = y[test_indices]
# 对测试集进行预测
predictions = []
for i in range(len(X_test)):
pred_label = knn(X_train, y_train, X_test[i], k=3)
predictions.append(pred_label)
# 计算准确率
accuracy = np.mean(predictions == y_test)
print("Accuracy:", accuracy)
```
这个代码使用numpy库计算样本间的距离,并实现了knn算法。在这个例子中,我们将数据集分成了训练集和测试集,使用训练集训练模型,然后在测试集上进行预测,并计算准确率。
阅读全文