鸢尾花案例增加K值调优

鸢尾花案例增加K值调优案例

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV

def knn_iris_gscv():
    """
    用KNN算法对鸢尾花进行分类,添加网格搜索和交叉验证
    :return:
    """
    # 1)获取数据
    iris = load_iris()
    # 2)划分数据集
    x_train,x_test,y_train,y_test = train_test_split(iris.data, iris.target, random_state=6)
    # 3)特征工程:标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    # 注意:这里的测试集采用的是训练集中的平均值和标准差,所以采用的是 transform,而不是上面的 fit_transform
    x_test = transfer.transform(x_test)
    # 4)KNN算法预估器
    estimator = KNeighborsClassifier()
    # 加入网格搜索和交叉验证
    # 参数准备
    param_dict = {"n_neighbors":[1,3,5,7,9,11]}
    estimator = GridSearchCV(estimator, param_grid=param_dict, cv=10)
    estimator.fit(x_train, y_train) # x_train 是训练的数据,y_train 是数据对应的标签. 最终会得到训练模型
    # 5)模型评估
    # 方法一:直接对比真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("直接对比真实值和预测值:\n", y_test == y_predict)
    # 方法二:计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为:\n", score)

    # 最佳参数:best_params_
    print("最佳参数:\n", estimator.best_params_)
    # 最佳结果:best_score_
    print("最佳结果:\n", estimator.best_score_)
    # 最佳估计器:best_estimator_
    print("最佳估计器:\n", estimator.best_estimator_)
    # 交叉验证结果:cv_result_
    print("交叉验证结果:\n", estimator.cv_results_)
    return None

#测试代码
knn_iris_gscv()

运行结果

y_predict:
[0 2 0 0 2 1 2 0 2 1 2 1 2 2 1 1 2 1 1 0 0 2 0 0 1 1 1 2 0 1 0 1 0 0 1 2 1
2]
直接对比真实值和预测值:
[ True True True True True True True True True True True True
True True True False True True True True True True True True
True True True True True True True True True True False True
True True]
准确率为:
0.9473684210526315
最佳参数:
{‘n_neighbors’: 11}
最佳结果:
0.9734848484848484
最佳估计器:
KNeighborsClassifier(n_neighbors=11)
交叉验证结果:
{‘mean_fit_time’: array([0.00059927, 0.00040243, 0.00050223, 0.00039899, 0.00049894,
0.00069396]), ‘std_fit_time’: array([0.0004893 , 0.00049291, 0.00067713, 0.00066157, 0.00049894,
0.0004544 ]), ‘mean_score_time’: array([0.00129728, 0.00099287, 0.00118837, 0.00159571, 0.00109911,
0.00099838]), ‘std_score_time’: array([0.00063815, 0.00044629, 0.0003949 , 0.00048884, 0.00029721,
0.0004489 ]), ‘param_n_neighbors’: masked_array(data=[1, 3, 5, 7, 9, 11],
mask=[False, False, False, False, False, False],
fill_value=’?’,
dtype=object), ‘params’: [{‘n_neighbors’: 1}, {‘n_neighbors’: 3}, {‘n_neighbors’: 5}, {‘n_neighbors’: 7}, {‘n_neighbors’: 9}, {‘n_neighbors’: 11}], ‘split0_test_score’: array([1., 1., 1., 1., 1., 1.]), ‘split1_test_score’: array([0.91666667, 0.91666667, 1. , 0.91666667, 0.91666667,
0.91666667]), ‘split2_test_score’: array([1., 1., 1., 1., 1., 1.]), ‘split3_test_score’: array([1. , 1. , 1. , 1. , 0.90909091,
1. ]), ‘split4_test_score’: array([1., 1., 1., 1., 1., 1.]), ‘split5_test_score’: array([0.90909091, 0.90909091, 1. , 1. , 1. ,
1. ]), ‘split6_test_score’: array([1., 1., 1., 1., 1., 1.]), ‘split7_test_score’: array([0.90909091, 0.90909091, 0.90909091, 0.90909091, 1. ,
1. ]), ‘split8_test_score’: array([1., 1., 1., 1., 1., 1.]), ‘split9_test_score’: array([0.90909091, 0.81818182, 0.81818182, 0.81818182, 0.81818182,
0.81818182]), ‘mean_test_score’: array([0.96439394, 0.95530303, 0.97272727, 0.96439394, 0.96439394,
0.97348485]), ‘std_test_score’: array([0.04365767, 0.0604591 , 0.05821022, 0.05965639, 0.05965639,
0.05742104]), ‘rank_test_score’: array([5, 6, 2, 3, 3, 1])}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值