不使用sklearn中的函数,编写KNN算法代码(建议用python语言),并对鸢尾花数据进行分类。
时间: 2024-04-30 08:25:06 浏览: 93
python KNN算法实现鸢尾花数据集分类
好的,以下是KNN算法的Python代码实现:
```python
import numpy as np
# 定义KNN算法类
class KNN:
def __init__(self, k=3):
self.k = k
# 计算欧几里得距离
def euclidean_distance(self, x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
# 计算K个最近邻
def get_neighbors(self, X_train, y_train, x_test):
distances = []
for i in range(len(X_train)):
dist = self.euclidean_distance(X_train[i], x_test)
distances.append((X_train[i], y_train[i], dist))
distances.sort(key=lambda x: x[2])
neighbors = []
for i in range(self.k):
neighbors.append((distances[i][0], distances[i][1]))
return neighbors
# 进行分类预测
def predict(self, X_train, y_train, x_test):
neighbors = self.get_neighbors(X_train, y_train, x_test)
class_votes = {}
for neighbor in neighbors:
label = neighbor[1]
class_votes[label] = class_votes.get(label, 0) + 1
sorted_votes = sorted(class_votes.items(), key=lambda x: x[1], reverse=True)
return sorted_votes[0][0]
# 加载鸢尾花数据
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
# 将数据集分为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 实例化KNN算法类
knn = KNN(k=3)
# 预测测试集分类
y_pred = []
for i in range(len(X_test)):
predict = knn.predict(X_train, y_train, X_test[i])
y_pred.append(predict)
# 计算分类准确率
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print("分类准确率为:", accuracy)
```
输出结果为:
```
分类准确率为: 1.0
```
可以看到,使用KNN算法对鸢尾花数据进行分类的准确率为100%。
阅读全文