Python 机器学习实战 第6章 机器学习的通用工作流程实例
Python 机器学习 第6章 机器学习的通用工作流程实例
内容概要
第6章介绍了机器学习的通用工作流程,涵盖了从问题定义到模型部署和维护的全过程。本章强调了数据的重要性,并详细讲解了如何定义任务、开发模型以及部署模型。通过本章,读者将掌握解决实际机器学习问题的系统方法。
主要内容
-
定义任务
- 问题框架:理解问题背景、业务逻辑和数据可用性。
- 数据收集:获取高质量的训练数据,并确保数据的代表性。
- 数据理解:探索和可视化数据,检查数据的完整性和潜在问题。
- 选择成功指标:确定如何衡量模型的成功,如准确率、精确率和召回率。
-
开发模型
- 数据准备:包括向量化、归一化和处理缺失值。
- 选择评估协议:如保留验证集、K折交叉验证和迭代K折验证。
- 击败基准模型:通过特征工程和选择合适的模型架构来提高模型性能。
- 开发过拟合模型:增加模型容量以确保模型能够学习数据中的模式。
- 正则化和调优模型:通过调整超参数、添加Dropout等方法来提高泛化性能。
-
部署模型
- 解释工作并设定期望:与利益相关者沟通模型的性能和限制。
- 部署推理模型:通过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)
精彩语录
-
中文:机器学习模型的目标是泛化,即在从未见过的数据上表现良好。
英文原文:The purpose of a machine learning model is to generalize: to perform accurately on never-before-seen inputs.
解释:这句话强调了机器学习的最终目标,即模型在新数据上的表现。 -
中文:数据的重要性远远超过算法。
英文原文: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.”
解释:这句话强调了数据质量对模型性能的关键影响。 -
中文:技术从来不是中立的。
英文原文: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.
解释:这句话提醒我们在技术选择中要考虑伦理问题。 -
中文:模型的性能会随时间变化而下降。
英文原文:As soon as your model has launched, you should be getting ready to train the next generation that will replace it.
解释:这句话强调了模型维护和更新的重要性。 -
中文:机器学习的工作流程是一个整体。
英文原文:You are now familiar with the big picture—the entire spectrum of what machine learning projects entail.
解释:这句话提醒我们要从整体上理解机器学习项目。
总结
通过本章的学习,读者将对机器学习的通用工作流程有一个清晰的理解,并掌握如何从问题定义到模型部署和维护的全过程。通过实践示例,读者可以学习如何有效地定义任务、开发模型并将其部署到生产环境中。这些知识将为解决实际问题提供坚实的基础。