0
点赞
收藏
分享

微信扫一扫

vue3 + axios 中断取消接口请求

IT程序员 2023-10-24 阅读 22

题目

 

 我的数据集合就是这个

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)
举报

相关推荐

0 条评论