当前位置: 首页 > news >正文

Python 机器学习实战 第6章 机器学习的通用工作流程实例

Python 机器学习 第6章 机器学习的通用工作流程实例

内容概要

第6章介绍了机器学习的通用工作流程,涵盖了从问题定义到模型部署和维护的全过程。本章强调了数据的重要性,并详细讲解了如何定义任务、开发模型以及部署模型。通过本章,读者将掌握解决实际机器学习问题的系统方法。
在这里插入图片描述

主要内容

  1. 定义任务

    • 问题框架:理解问题背景、业务逻辑和数据可用性。
    • 数据收集:获取高质量的训练数据,并确保数据的代表性。
    • 数据理解:探索和可视化数据,检查数据的完整性和潜在问题。
    • 选择成功指标:确定如何衡量模型的成功,如准确率、精确率和召回率。
  2. 开发模型

    • 数据准备:包括向量化、归一化和处理缺失值。
    • 选择评估协议:如保留验证集、K折交叉验证和迭代K折验证。
    • 击败基准模型:通过特征工程和选择合适的模型架构来提高模型性能。
    • 开发过拟合模型:增加模型容量以确保模型能够学习数据中的模式。
    • 正则化和调优模型:通过调整超参数、添加Dropout等方法来提高泛化性能。
  3. 部署模型

    • 解释工作并设定期望:与利益相关者沟通模型的性能和限制。
    • 部署推理模型:通过REST API、设备端部署或浏览器内部署等方式将模型投入生产。
    • 监控模型:在生产环境中持续监控模型的性能和业务影响。
    • 维护模型:定期更新模型以应对概念漂移和数据变化。

关键代码和算法

6.2.1 数据准备

# 数据归一化
x = train_data
x -= x.mean(axis=0)
x /= x.std(axis=0)# 处理缺失值
# 对于数值特征,用平均值填充缺失值
mean_value = x.mean(axis=0)
x[:, feature_index] = x[:, feature_index].fillna(mean_value)

6.2.2 选择评估协议

# 简单保留验证法
num_validation_samples = 10000
np.random.shuffle(data)
validation_data = data[:num_validation_samples]
training_data = data[num_validation_samples:]# K折交叉验证
k = 3
num_validation_samples = len(data) // k
np.random.shuffle(data)
validation_scores = []
for fold in range(k):validation_data = data[num_validation_samples * fold: num_validation_samples * (fold + 1)]training_data = np.concatenate([data[:num_validation_samples * fold], data[num_validation_samples * (fold + 1):]])model = get_model()model.fit(training_data, ...)validation_score = model.evaluate(validation_data, ...)validation_scores.append(validation_score)
validation_score = np.average(validation_scores)

6.2.3 击败基准模型

# 定义一个简单的基线模型
model = keras.Sequential([layers.Dense(16, activation="relu"),layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="rmsprop",loss="binary_crossentropy",metrics=["accuracy"])
history_baseline = model.fit(train_data, train_labels,epochs=10,batch_size=512,validation_split=0.4)

6.2.4 开发过拟合模型

# 增加模型容量
model = keras.Sequential([layers.Dense(512, activation="relu"),layers.Dense(512, activation="relu"),layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="rmsprop",loss="binary_crossentropy",metrics=["accuracy"])
history_overfit = model.fit(train_data, train_labels,epochs=10,batch_size=512,validation_split=0.4)

6.2.5 正则化和调优模型

# 添加Dropout
model = keras.Sequential([layers.Dense(16, activation="relu"),layers.Dropout(0.5),layers.Dense(16, activation="relu"),layers.Dropout(0.5),layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="rmsprop",loss="binary_crossentropy",metrics=["accuracy"])
history_dropout = model.fit(train_data, train_labels,epochs=10,batch_size=512,validation_split=0.4)# 添加L2正则化
from tensorflow.keras import regularizers
model = keras.Sequential([layers.Dense(16,kernel_regularizer=regularizers.l2(0.002),activation="relu"),layers.Dense(16,kernel_regularizer=regularizers.l2(0.002),activation="relu"),layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="rmsprop",loss="binary_crossentropy",metrics=["accuracy"])
history_l2_reg = model.fit(train_data, train_labels,epochs=10,batch_size=512,validation_split=0.4)

精彩语录

  1. 中文:机器学习模型的目标是泛化,即在从未见过的数据上表现良好。
    英文原文:The purpose of a machine learning model is to generalize: to perform accurately on never-before-seen inputs.
    解释:这句话强调了机器学习的最终目标,即模型在新数据上的表现。

  2. 中文:数据的重要性远远超过算法。
    英文原文:The point that data matters more than algorithms was most famously made in a 2009 paper by Google researchers titled “The Unreasonable Effectiveness of Data.”
    解释:这句话强调了数据质量对模型性能的关键影响。

  3. 中文:技术从来不是中立的。
    英文原文:Technology is never neutral. If your work has any impact on the world, this impact has a moral direction: technical choices are also ethical choices.
    解释:这句话提醒我们在技术选择中要考虑伦理问题。

  4. 中文:模型的性能会随时间变化而下降。
    英文原文:As soon as your model has launched, you should be getting ready to train the next generation that will replace it.
    解释:这句话强调了模型维护和更新的重要性。

  5. 中文:机器学习的工作流程是一个整体。
    英文原文:You are now familiar with the big picture—the entire spectrum of what machine learning projects entail.
    解释:这句话提醒我们要从整体上理解机器学习项目。

总结

通过本章的学习,读者将对机器学习的通用工作流程有一个清晰的理解,并掌握如何从问题定义到模型部署和维护的全过程。通过实践示例,读者可以学习如何有效地定义任务、开发模型并将其部署到生产环境中。这些知识将为解决实际问题提供坚实的基础。


http://www.mrgr.cn/news/98378.html

相关文章:

  • 大数据面试问答-Spark
  • 嵌入式程序设计英语
  • Spring Security6 从源码慢速开始
  • HarmonyOS:使用Refresh组件实现页面下拉刷新上拉加载更多
  • PVE 8.4.1 安装 KDE Plasma 桌面环境 和 PVE换源
  • linux中查看.ypc二进制文件
  • Linux服务之网络共享
  • Melos 发布pub.dev
  • 30学Java第十天——类加载的过程
  • 【动手学强化学习】番外7-MAPPO应用框架2学习与复现
  • AWS Redshift的使用场景及一些常见问题
  • 绿算轻舟系列FPGA加速卡:驱动数字化转型的核心动力
  • electron-builder参数详解
  • ukui-greeter编译与安装
  • C/C++的数据类型
  • 数据库原理及应用mysql版陈业斌实验三
  • mongodb 安装配置
  • AI 项目详细开发步骤指南
  • antv x6使用(支持节点排序、新增节点、编辑节点、删除节点、选中节点)
  • 【Java集合】HashMap源码深度分析