knn用于水果数据集分类

knn⽤于⽔果数据集分类
knn算法流程:
若k取⽆穷⼤,那么测试数据就取决于每⼀类的占⽐,归属于占⽐最⼤的那⼀类。
⾸先观察数据集,利⽤mass,height,width,color_score四列特征进⾏⽔果分类。
g=sns.pairplot(data=fruits_df,hue='fruit_name',vars=['mass','width','height','color_score'])
姜斌是谁
然后利⽤sns.pairplot查看两两特征之间的关系,可看出对⾓线是每⼀类的直⽅图,mass和width⼏乎呈线性关系。
再利⽤width,height,color_score,建⽴三维图,看出绿⾊可以容易区分,对于更⾼维的数据可以采⽤pca降维然后进⾏查看。
knn代码:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import seaborn as sns
ighbors import KNeighborsClassifier
ics import accuracy_score
import ml_visualization
#利⽤k近邻法分离
#分离训练集与测试集
del_selection import train_test_split
fruits_ad_table('fruit_data_')
print(fruits_df)
print('样本个数:',len(fruits_df))
#创建⽬标标签和名称的字典
fruits_name_dict=dict(zip(fruits_df['fruit_label'],fruits_df['fruit_name']))
#print(fruits_df['fruit_label'])
print(fruits_name_dict)
#划分数据集
X=fruits_df[['mass','width','height','color_score']]
# print(X)
y=fruits_df['fruit_label']
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=1/4,random_state=0)
print('X_train=\n',X_train)
print('y_train=\n',y_train)
print('数据集样本数:{},训练集样本数:{},测试集样本数:{}'.format(len(X),len(X_train),len(X_test))) #
#可视化查看特征变量,对⾓线就是直⽅图,其余是两两直接的关系
g=sns.pairplot(data=fruits_df,hue='fruit_name',vars=['mass','width','height','color_score'])
plt.savefig('1.jpg')
#三维查看
label_color_dict={1:'red',2:'green',3:'blue',4:'yellow'}
colors=list(map(lambda label: label_color_dict[label],y_train))
print('colors=\n',colors)
fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')
ax.scatter(X_train['width'],X_train['height'],X_train['color_score'],c=colors,marker='o',s=100)
ax.set_xlabel('width')
ax.set_ylabel('height')
正交试验设计
ax.set_zlabel('color_score')
plt.show()
# #建⽴knn模型
acc_scores=[]
for k in range(1,20):
knn=KNeighborsClassifier(n_neighbors=k)
#训练模型
knn.fit(X_train,y_train)
#预测
y_pred=knn.predict(X_test)
杨静雅
# print('y_pred=',y_pred)
# print('y_test=\n',y_test)
acc=accuracy_score(y_test,y_pred)
acc_scores.append(acc)
plt.figure()
plt.xlabel('k')
plt.ylabel('accuarcy')
plt.plot(acc_scores,marker='o')
高频变压器设计软件plt.show()
print('准确率:',acc)
研究性学习教案ml_visualization.plot_fruit_knn(X_train,y_train,5)
可视化代码:ml_visualization.py
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
lors import ListedColormap
from sklearn import neighbors
import graphviz
import export_graphviz
import matplotlib.patches as mpatches
def plot_fruit_knn(X, y, n_neighbors):
"""
在“⽔果数据集”上对 height 和 width ⼆维数据进⾏kNN训练
并绘制出结果
"""
X_mat = X[['height', 'width']].as_matrix()
y_mat = y.as_matrix()
职业病范围和职业病患者处理办法的规定
# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF', '#AFAFAF'])    cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF', '#AFAFAF'])
clf = neighbors.KNeighborsClassifier(n_neighbors)
clf.fit(X_mat, y_mat)
# Plot the decision boundary by assigning a color in the color map
# to each mesh point.
mesh_step_size = .01  # step size in the mesh
plot_symbol_size = 50
x_min, x_max = X_mat[:, 0].min() - 1, X_mat[:, 0].max() + 1
y_min, y_max = X_mat[:, 1].min() - 1, X_mat[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, mesh_step_size),
np.arange(y_min, y_max, mesh_step_size))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
# Plot training points
plt.scatter(X_mat[:, 0], X_mat[:, 1], s=plot_symbol_size, c=y, cmap=cmap_bold,                edgecolor='black')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
patch0 = mpatches.Patch(color='#FF0000', label='apple')
patch1 = mpatches.Patch(color='#00FF00', label='mandarin')
patch2 = mpatches.Patch(color='#0000FF', label='orange')
patch3 = mpatches.Patch(color='#AFAFAF', label='lemon')
plt.legend(handles=[patch0, patch1, patch2, patch3])
plt.xlabel('height (cm)')
plt.ylabel('width (cm)')
plt.show()
结果:可看出k为5时acc最⾼。
⽽对于回归的話,对于k==3,相邻的三个值取平均,也可以利⽤距离加权。

本文发布于:2024-09-22 10:26:23,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/326147.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:训练   数据   特征   查看   设计   标签
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议