四、分类算法 - 模型选择与调优

目录

1、模型选择与调优

[1.1 交叉验证](#1.1 交叉验证)

[1.2 超参数搜索 - 网格搜索](#1.2 超参数搜索 - 网格搜索)

[1.3 模型选择与调优 API](#1.3 模型选择与调优 API)

[1.4 鸢尾花案例增加K值调优](#1.4 鸢尾花案例增加K值调优)


  1. sklearn转换器和估算器
  2. KNN算法
  3. 模型选择和调优
  4. 朴素贝叶斯算法
  5. 决策树
  6. 随机森林

1、模型选择与调优

1.1 交叉验证

1.2 超参数搜索 - 网格搜索

1.3 模型选择与调优 API

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

python 复制代码
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler

def knn_iris():
    # 用KNN 算法对鸢尾花进行分类
    # 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)
    x_test = transfer.transform(x_test)
    # 4、KNN 算法预估器
    estimator = KNeighborsClassifier(n_neighbors=3)
    estimator.fit(x_train,y_train)
    # 5、模型评估
    # 方法1 :直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n",y_predict)
    print("直接比对真实值和预测值:\n",y_test == y_predict)
    # 方法2:计算准确率
    score = estimator.score(x_test,y_test)
    print("准确率为:\n",score)
    return None

def knn_iris_gscv():
    # 用KNN 算法对鸢尾花进行分类,添加网格搜索和交叉验证
    # 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)
    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)

    # 5、模型评估
    # 方法1 :直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n",y_predict)
    print("直接比对真实值和预测值:\n",y_test == y_predict)
    # 方法2:计算准确率
    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_results_
    print("交叉验证结果:\n",estimator.cv_results_)

    return None

if __name__ == "__main__":
    # 代码1 :用KNN算法对鸢尾花进行分类
    knn_iris()
    # 代码2 :用KNN算法对鸢尾花进行分类,添加网格搜索和交叉验证
    knn_iris_gscv()
相关推荐
zhishidi3 小时前
深度学习中的优化器
人工智能·深度学习
糖果店的幽灵3 小时前
2026 年最强Obsidian保姆级教程,10分钟打造你的第二大脑
人工智能·langgraph
巫山老妖3 小时前
AI驱动的Flutter工程:如何让AI生成代码不跑偏
人工智能
深度学习lover3 小时前
<数据集>yolo 小麦麦穗识别<目标检测>
人工智能·yolo·目标检测·计算机视觉·数据集·小麦麦穗识别
火山引擎开发者社区3 小时前
让每台终端都成为智能入口:ArkClaw × 云沙箱的规模化实战
人工智能
阳光是sunny3 小时前
LangGraph实战教程:一文搞懂图的状态(State)管理
前端·人工智能·后端
数智化管理手记3 小时前
财务大数据怎么管住资金风险?财务大数据和财务数智化到底怎么结合?
大数据·网络·数据库·人工智能·数据挖掘
AI导出鸭4 小时前
如何让deepseek生成word文档 ?「AI 导出鸭」苹果版:从API流式解析到Pages级渲染,硬核攻克公式裂变与表格回流的终极方案。
人工智能·chatgpt·word·cocoa·ai导出鸭
涛声依旧god4 小时前
如何打造一个 AI Agent 自动写作并一键发布技术文章的自动化系统
人工智能·ai·自动化·ai编程
AI_AGENT_DEV_AI4 小时前
AI 自动化阅卷系统的开发
人工智能