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)
相关推荐
中冕—霍格沃兹软件开发测试3 分钟前
探索性测试:思维驱动下的高效缺陷狩猎
人工智能·科技·开源·appium·bug
cnfalcon4 分钟前
ESP-IDF AI硬件开发技术问题记录
人工智能·esp-idf
陈佬昔没带相机5 分钟前
从罗永浩 x MiniMax 闫俊杰对谈中,一窥 AI 时代软件公司岗位变化
人工智能·程序员·敏捷开发
老马啸西风7 分钟前
成熟企业级技术平台-09-加密机 / 密钥管理服务 KMSS(Key Management & Security Service)
人工智能·深度学习·算法·职场和发展
2301_801821718 分钟前
前期工作总结
人工智能
Ulana27 分钟前
计算机基础10大高频考题解析
java·人工智能·算法
windfantasy199028 分钟前
NCT与GESP哪个更好?线上监考与线下考点的便利性对比
人工智能
执笔论英雄30 分钟前
【LORA】
人工智能
Jerryhut43 分钟前
Bev感知特征空间算法
人工智能
xian_wwq1 小时前
【学习笔记】基于人工智能的火电机组全局性能一体化优化研究
人工智能·笔记·学习·火电