当前位置:首页 > 编程笔记 > 正文
已解决

KNN(K近邻)水仙花的分类(含答案)

来自网友在路上 179879提问 提问时间:2023-10-26 23:17:55阅读次数: 79

最佳答案 问答题库798位专家为你答疑解惑

题目

以下采用K-NN算法来解决水仙花的分类问题,每个样本有两个特征,第一个为水仙花的花萼长度,第二个为水仙花 的花萼宽度,具体数据见表,

    1)设置k=3, 采用欧式距离,分析分类精度为多少?

    2)使用网格搜索方式找到最佳参数,并预测

    3)可视化

 

 我的数据集合就是这个

excel数据展示

 代码

import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormapdef model_selection(x_train, y_train):params = {'n_neighbors': [3,5,7,8,10], 'p': [1,2]}model = KNeighborsClassifier()gs = GridSearchCV(model, params, verbose=2, cv=5)gs.fit(x_train, y_train)print("Best Model:", gs.best_params_, "Accuracy:", gs.best_score_)return gs.best_estimator_def read():filename = r"data/shuixianhua.xlsx"data = pd.read_excel(filename, header=None)x1 = data.iloc[1:, [0, 1]].valuesx2 = data.iloc[1:, [3, 4]].values# print(x2)y1 = data.iloc[1:, 2].valuesy2 = data.iloc[1:, 5].valuesx = np.vstack((x1, x2))  # 竖向合并y = np.hstack((y1, y2))  # 横向合并y = y.astype(int)return x, y
.....
需要代码,我不给了,呜呜呜

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"KNN(K近邻)水仙花的分类(含答案)":http://eshow365.cn/6-25505-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!