python实现knn算法鸢尾花代码
时间: 2023-08-30 18:09:10 浏览: 121
下面是使用Python实现KNN算法的鸢尾花代码:
``` python
# 导入需要用到的库
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# 加载鸢尾花数据集
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)
# 定义KNN分类器并进行训练
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
# 使用训练好的模型进行预测
y_pred = knn.predict(X_test)
# 计算模型的准确率
accuracy = np.mean(y_pred == y_test)
print("准确率:", accuracy)
```
代码中使用了`sklearn`库中的`KNeighborsClassifier`类来实现KNN算法,同时使用了`train_test_split`函数将数据集分成训练集和测试集,最后计算了模型的准确率。
相关问题
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
```
python实现knn算法鸢尾花
KNN(K-Nearest Neighbors)算法是一种常用的分类算法,它的基本思想是找到距离待分类样本最近的K个已知类别的样本点,然后将待分类样本归为出现最多的类别。
下面是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
class KNN:
def __init__(self, k):
self.k = k
def fit(self, X_train, y_train):
self.X_train = X_train
self.y_train = y_train
def predict(self, X_test):
y_pred = []
for x in X_test:
distances = []
for i in range(len(self.X_train)):
distance = np.sqrt(np.sum((x - self.X_train[i]) ** 2))
distances.append((distance, self.y_train[i]))
distances = sorted(distances)
k_nearest_neighbors = distances[:self.k]
k_nearest_neighbors_labels = [label for _, label in k_nearest_neighbors]
most_common_label = max(set(k_nearest_neighbors_labels), key=k_nearest_neighbors_labels.count)
y_pred.append(most_common_label)
return y_pred
data = load_iris()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = KNN(k=5)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
上述代码首先导入必要的库,然后定义了一个KNN类,其中`__init__`方法初始化了一个K值,`fit`方法用于训练模型,`predict`方法用于对测试样本进行预测。在`predict`方法中,首先计算测试样本与训练样本之间的距离,然后将距离最近的K个样本点的标签保存到一个列表中,最后统计标签列表中出现最多的标签作为预测结果。
接下来,我们加载鸢尾花数据集并将其分为训练集和测试集。然后,我们使用KNN模型对训练集进行训练,并使用测试集进行预测。最后,我们计算模型的精度并打印出来。
阅读全文