Python代码KNN算法鸢尾花数据分析
时间: 2023-05-22 12:00:48 浏览: 146
可以回答。以下是一个简单的 Python 代码实现 KNN 算法对鸢尾花数据进行分类的示例:
```
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# 加载鸢尾花数据集
iris = load_iris()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
# 创建 KNN 分类器
knn = KNeighborsClassifier(n_neighbors=3)
# 训练模型
knn.fit(X_train, y_train)
# 预测测试集
y_pred = knn.predict(X_test)
# 输出准确率
print("Accuracy:", knn.score(X_test, y_test))
```
这段代码使用了 scikit-learn 库中的 KNeighborsClassifier 类来实现 KNN 算法,对鸢尾花数据进行分类,并输出了模型在测试集上的准确率。
相关问题
python实现knn算法鸢尾花代码
以下是使用Python实现knn算法的鸢尾花代码示例:
```python
import numpy as np
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.3, random_state=42)
# 定义knn算法
def knn(X_train, X_test, y_train, y_test, k):
predictions = []
for i in range(len(X_test)):
distances = []
for j in range(len(X_train)):
distance = np.sqrt(np.sum(np.square(X_test[i] - X_train[j])))
distances.append((distance, j))
distances.sort()
neighbors = []
for m in range(k):
neighbors.append(distances[m][1])
classes = {}
for n in neighbors:
if y_train[n] in classes:
classes[y_train[n]] += 1
else:
classes[y_train[n]] = 1
sorted_classes = sorted(classes.items(), key=lambda x: x[1], reverse=True)
predictions.append(sorted_classes[0][0])
return predictions
# 测试不同的k值
for k in range(1, 10):
y_pred = knn(X_train, X_test, y_train, y_test, k)
accuracy = accuracy_score(y_test, y_pred)
print("k=%d, accuracy=%.2f%%" % (k, accuracy * 100))
```
以上代码中,我们首先加载鸢尾花数据集,然后将数据集分割为训练集和测试集。接着,我们定义了一个knn算法函数,该函数接受训练集、测试集、训练集标签、测试集标签和k值作为参数,返回测试集的预测标签。在测试阶段,我们分别尝试k值为1到9的情况,并计算预测准确率。
python实现knn算法鸢尾花源代码
以下是使用Python实现knn算法对鸢尾花进行分类的代码:
```python
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# 加载鸢尾花数据集
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)
# 定义knn算法函数
def knn(X_train, y_train, X_test, k):
distances = []
targets = []
for i in range(len(X_train)):
distance = np.sqrt(np.sum(np.square(X_test - X_train[i, :])))
distances.append([distance, i])
distances = sorted(distances)
for i in range(k):
index = distances[i][1]
targets.append(y_train[index])
return np.bincount(targets).argmax()
# 对测试集进行预测
predictions = []
for i in range(len(X_test)):
predictions.append(knn(X_train, y_train, X_test[i, :], 3))
# 计算预测准确率
accuracy = np.sum(np.array(predictions) == y_test) / len(y_test)
print("预测结果:", predictions)
print("真实结果:", y_test)
print("预测准确率:", accuracy)
```
输出结果:
```
预测结果: [2, 0, 2, 0, 0, 2, 2, 2, 2, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 2]
真实结果: [2 0 2 0 0 1 2 2 2 0 1 1 1 0 0 1 1 1 0 0 2 2 2 2 0 2 2 0 0 2]
预测准确率: 0.9333333333333333
```
阅读全文