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

题目

以下采用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 ListedColormap

def 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]].values
    x2 = data.iloc[1:, [3, 4]].values
    # print(x2)
    y1 = data.iloc[1:, 2].values
    y2 = data.iloc[1:, 5].values
    x = np.vstack((x1, x2))  # 竖向合并
    y = np.hstack((y1, y2))  # 横向合并

    y = y.astype(int)
    return x, y

def plot_decision_boundary(x, y, model):
    h = 0.02  # Step size in the mesh

    # Create color maps
    cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA'])
    cmap_bold = ListedColormap(['#FF0000', '#00FF00'])

    x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
    y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

    Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    plt.figure()
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
    plt.scatter(x[:, 0], x[:, 1], c=y, cmap=cmap_bold, edgecolor='k', s=20)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title("KNN Decision Boundaries")
    plt.show()

if __name__ == '__main__':
    x, y = read()
    best_model = model_selection(x, y)
    plot_decision_boundary(x, y, best_model)
相关推荐
pen-ai13 分钟前
【NLP】15. NLP推理方法详解 --- 动态规划:序列标注,语法解析,共同指代
人工智能·自然语言处理·动态规划
Chaos_Wang_20 分钟前
NLP高频面试题(二十九)——大模型解码常见参数解析
人工智能·自然语言处理
Acrelhuang27 分钟前
8.3MW屋顶光伏+光储协同:上海汽车变速器低碳工厂的能源革命-安科瑞黄安南
大数据·数据库·人工智能·物联网·数据库开发
区块链蓝海27 分钟前
沉浸式体验测评|AI Ville:我在Web3小镇“生活”了一周
人工智能·web3·生活
whaosoft-14342 分钟前
51c自动驾驶~合集15
人工智能
花楸树43 分钟前
前端搭建 MCP Client(Web版)+ Server + Agent 实践
前端·人工智能
用户87612829073741 小时前
前端ai对话框架semi-design-vue
前端·人工智能
量子位1 小时前
稚晖君刚挖来的 90 后机器人大牛:逆袭履历堪比爽文男主
人工智能·llm
量子位1 小时前
200 亿机器人独角兽被曝爆雷,官方回应来了
人工智能·llm
机器之心1 小时前
细节厘米级还原、实时渲染,MTGS方法突破自动驾驶场景重建瓶颈
人工智能