首页 分享 头歌基于KNN的鸢尾花数据集分类

头歌基于KNN的鸢尾花数据集分类

来源:花匠小妙招 时间:2024-12-17 20:38

#调用库

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.colors import ListedColormap

from sklearn import neighbors, datasets

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

from sklearn.metrics import confusion_matrix

#数据集加载

dataset = datasets.load_iris()

X = dataset.data[:, :2]

y = dataset.target

attributes_dict = {0:"sepal_length",1:"sepal_width"}

for attribute in attributes_dict:

########## Begin ##########

#计算该特征在数据集中的最大值

print("{} 最大值:{}".format(attributes_dict[attribute], np.max(X[:,attribute])))

#计算该特征在数据集中的最小值

print("{} 最小值:{}".format(attributes_dict[attribute], np.min(X[:,attribute])))

#计算该特征在数据集中的平均值

# round 函数将float数据格式化小数点后一位

print("{} 平均值:{}".format(attributes_dict[attribute], round(np.average(X[:, attribute]),1)))

########## End ##########

print("-------------------------------------")

#数据集划分

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

#训练KNN

########### Begin ##########

#使用K最近邻分类器对数据进行分类,并计算混淆矩阵(取最近的5个邻居作为参考样本,使用闵可夫斯基距离来衡量样本之间的距离,p参数设置为2)

classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)

#模型训练

classifier.fit(X_train, y_train)

#模型预测

y_pred = classifier.predict(X_test)

#生成混淆矩阵cm,用于评估分类器的性能。

cm = confusion_matrix(y_test, y_pred)

print('cm',cm)

########### End ##########

#训练可视化

X_set, y_set = X_test, y_test

X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),

np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))

plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),

alpha = 0.75, cmap = ListedColormap(('red', 'black', 'orange')))

plt.xlim(X1.min(), X1.max())

plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(y_set)):

color_list = [[[1,0,0],[0,1,0],[0,0,1]][i]]

count = np.sum((y_set == j)==True)

plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],

c = color_list*count, label = j)

plt.title('K-NN (Test set-k=5)')

plt.xlabel('Sepal Length')

plt.ylabel('Sepal Width')

plt.legend()

plt.show()

plt.savefig('/data/workspace/myshixun/step1/reality/pic')

#可视化测试结果

X_set, y_set = X_test, y_test

X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),

np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))

plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),

alpha = 0.75, cmap = ListedColormap(('red', 'black', 'orange')))

plt.xlim(X1.min(), X1.max())

plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(y_set)):

color_list = [[[1,0,0],[0,1,0],[0,0,1]][i]]

count = np.sum((y_set == j)==True)

plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],

c = color_list*count, label = j)

plt.title('K-NN (Test set-k=5)')

plt.xlabel('Sepal Length')

plt.ylabel('Sepal Width')

plt.legend()

plt.show()

plt.savefig('/data/workspace/myshixun/step1/reality/pic1')

相关知识

【机器学习】基于KNN算法实现鸢尾花数据集的分类
KNN算法实现鸢尾花数据集分类
KNN分类算法介绍,用KNN分类鸢尾花数据集(iris)
原生python实现knn分类算法(鸢尾花数据集)
Python原生代码实现KNN算法(鸢尾花数据集)
【机器学习】利用KNN对Iris鸢尾花数据集进行分类
【python机器学习】KNN算法实现回归(基于鸢尾花数据集)
Knn算法实现鸢尾花分类
实验一:鸢尾花数据集分类
【机器学习】KNN算法实现鸢尾花分类

网址: 头歌基于KNN的鸢尾花数据集分类 https://www.huajiangbk.com/newsview1153790.html

所属分类:花卉
上一篇: 植物病理学 第13章及14章 植
下一篇: 机器学习实战第1天:鸢尾花分类任

推荐分享