使用 Python 进行集成学习算法速成课程。
7 天内掌握 Python 集成学习。
集成学习指的是结合两个或更多模型预测的机器学习模型。
集成是机器学习的一种高级方法,当预测能力和技能比使用简单易懂的模型更重要时,通常会使用它。因此,在像 百万美元 Netflix 大奖 和 Kaggle 竞赛 这样的机器学习竞赛中,顶尖和获胜的参与者经常使用集成方法。
像 scikit-learn Python 这样的现代机器学习库提供了一套先进的集成学习方法,这些方法易于配置和正确使用,并且没有数据泄漏(这是使用集成算法时常见的问题)。
在这门速成课程中,你将了解如何在七天内开始并自信地将集成学习算法应用到你的 Python 预测建模项目中。
这是一篇内容丰富且重要的文章。您可能想把它加入书签。
通过我的新书《使用 Python 的集成学习算法》**启动你的项目**,其中包括所有示例的**分步教程**和 **Python 源代码**文件。
让我们开始吧。

使用 Python 的集成机器学习(7 天迷你课程)
图片由 anoldent 提供,保留部分权利。
本速成课程适合谁?
在开始之前,让我们确保您来对了地方。
本课程面向可能了解一些应用机器学习的开发人员。也许你知道如何使用流行的工具从头到尾解决预测建模问题,或者至少是大多数主要步骤。
本课程的课程假设您具备以下几点:
- 您熟悉基本的Python编程。
- 您可能了解一些基本的NumPy用于数组操作。
- 您可能了解一些基本的 scikit-learn 建模知识。
您不需要是
- 成为数学高手!
- 成为机器学习专家!
本速成课程将把你从一个了解一点机器学习的开发人员,培养成为一个能够有效、熟练地将集成学习算法应用于预测建模项目的开发人员。
注意:本速成课程假设您有一个可用的 Python 3 SciPy 环境,并且至少安装了 NumPy。如果您的环境需要帮助,您可以按照这里的分步教程进行操作。
速成课程概览
本速成课程分为七节课。
您可以每天完成一节课(推荐),或者在一天内完成所有课程(硬核)。这真的取决于您的可用时间和热情程度。
下面列出了七节课,它们将帮助你开始并高效地使用 Python 进行数据准备
- 第一课:什么是集成学习?
- 第二课:Bagging 集成
- 第三课:随机森林集成
- 第四课:AdaBoost 集成
- 第五课:梯度提升集成
- 第六课:投票集成
- 第七课:堆叠集成
每节课可能需要 60 秒到 30 分钟。请慢慢来,按照自己的节奏完成课程。在下面的评论中提问,甚至发布结果。
这些课程可能会要求你自行寻找解决问题的方法。我会给你提示,但每节课的重点之一是迫使你学习在哪里寻找有关算法和 Python 中最佳工具的帮助。(**提示**:*我博客上都有答案;请使用搜索框*。)
在评论中发布您的结果;我会为您加油!
坚持下去;不要放弃。
第 01 课:什么是集成学习?
在本课程中,您将了解什么是集成学习以及它为何重要。
应用机器学习通常涉及在数据集上拟合和评估模型。
鉴于我们无法事先知道哪个模型在数据集上表现最好,这可能涉及大量的试错,直到我们找到一个对我们的项目表现良好或最佳的模型。
另一种方法是准备多个不同的模型,然后结合它们的预测。
这被称为集成机器学习模型,或简称为集成,而寻找表现良好的集成模型的过程被称为“集成学习”。
尽管实现这一目标的方法几乎是无限的,但实际中最常讨论和使用的集成学习技术大概分为三类。
它们的流行在很大程度上归因于它们易于实现并在各种预测建模问题上的成功。
它们是
- **Bagging**,例如 Bagged 决策树和随机森林。
- **Boosting**,例如 AdaBoost 和梯度提升。
- **Stacking**,例如投票和使用元模型。
使用集成而不是单个模型主要有两个原因,它们是相关的;它们是:
- 可靠性:集成可以减少预测的方差。
- 技能:集成可以比单个模型获得更好的性能。
这些都是机器学习项目中重要的考虑因素,有时我们可能更喜欢模型的一个或两个属性。
您的任务
在本课中,您必须列出集成学习的三个应用。
这些可能是著名的例子,例如机器学习竞赛,或者您在教程、书籍或研究论文中遇到的例子。
请在下面的评论中发布您的答案。我很想看看您能想出什么。
在下一课中,您将学习如何开发和评估 bagging 集成。
第 02 课:Bagging 集成
在本课程中,您将学习**自助聚合**或 bagging 集成。
Bagging 通过创建训练数据集的样本并在每个样本上拟合决策树来工作。
训练数据集的差异导致拟合决策树的差异,进而导致这些树所做预测的差异。然后使用简单的统计数据(例如投票或平均)组合集成成员所做的预测。
该方法的关键在于准备数据集的每个样本以训练集成成员的方式。示例(行)是从数据集中随机抽取,但有放回。有放回意味着如果选择了一行,它将被返回到训练数据集,以便在同一训练数据集中可能再次选择。
这被称为自助样本,该技术由此得名。
Bagging 在 scikit-learn 中可通过 BaggingClassifier 和 BaggingRegressor 类使用,它们默认使用决策树作为基础模型,您可以通过“n_estimators”参数指定要创建的树的数量。
下面列出了用于分类的 bagging 集成评估的完整示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 用于分类的 bagging 集成评估示例 from numpy import mean from numpy import std from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.ensemble import BaggingClassifier # 创建合成分类数据集 X, y = make_classification(random_state=1) # 配置集成模型 model = BaggingClassifier(n_estimators=50) # 配置重采样方法 cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1) # 使用重采样方法在数据集上评估集成 n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1) # 报告集成性能 print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores))) |
您的任务
在本课中,您必须运行该示例并查看评估模型的结果。
为了获得额外加分,请评估在集成中使用更多决策树的效果,甚至更改所使用的基本学习器。
请在下面的评论中发布您的答案。我很想看看您能想出什么。
在下一课中,您将学习如何开发和评估随机森林集成。
第 03 课:随机森林集成
在本课程中,您将发现随机森林集成。
随机森林是 bagging 集成的扩展。
与 bagging 类似,随机森林集成在训练数据集的不同自助样本上拟合决策树。
与 bagging 不同,随机森林还会对每个数据集的特征(列)进行采样。
具体来说,在构建每棵决策树时,会在数据中选择分裂点。随机森林不会在选择分裂点时考虑所有特征,而是将特征限制为随机子集,例如,如果有 10 个特征,则选择 3 个。
随机森林集成在 scikit-learn 中可通过 RandomForestClassifier 和 RandomForestRegressor 类使用。您可以通过“n_estimators”参数指定要创建的树的数量,并通过“max_features”参数指定在每个分裂点要考虑的随机选择的特征数量,该参数默认为数据集中特征数量的平方根。
下面列出了用于分类的随机森林集成评估的完整示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 用于分类的随机森林集成评估示例 from numpy import mean from numpy import std from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.ensemble import RandomForestClassifier # 创建合成分类数据集 X, y = make_classification(random_state=1) # 配置集成模型 model = RandomForestClassifier(n_estimators=50) # 配置重采样方法 cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1) # 使用重采样方法在数据集上评估集成 n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1) # 报告集成性能 print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores))) |
您的任务
在本课中,您必须运行该示例并查看评估模型的结果。
为了获得额外加分,评估在集成中使用更多决策树的效果,或者调整在每个分裂点考虑的特征数量。
请在下面的评论中发布您的答案。我很想看看您能想出什么。
在下一课中,您将学习如何开发和评估 AdaBoost 集成。
第 04 课:AdaBoost 集成
在本课程中,您将学习自适应提升或 AdaBoost 集成。
提升(Boosting)涉及顺序地向集成中添加模型,其中新模型试图纠正集成中先前模型所犯的错误。因此,添加的集成成员越多,集成预计犯的错误就越少,至少在数据支持的限制内以及在训练数据集过拟合之前是这样。
提升的想法最初是一个理论概念,而 AdaBoost 算法是实现基于提升的集成算法的第一个成功方法。
AdaBoost 通过在加权的训练数据集版本上拟合决策树来工作,使得树更关注先前成员做错的示例(行),而较少关注先前模型做对的示例。
AdaBoost 不使用完整的决策树,而是使用非常简单的树,这些树在进行预测之前仅对一个输入变量做出单个决策。这些短树被称为决策桩。
AdaBoost 在 scikit-learn 中可通过 AdaBoostClassifier 和 AdaBoostRegressor 类使用,它们默认使用决策树(决策桩)作为基础模型,您可以通过“n_estimators”参数指定要创建的树的数量。
下面列出了用于分类的 AdaBoost 集成评估的完整示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 用于分类的 AdaBoost 集成评估示例 from numpy import mean from numpy import std from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.ensemble import AdaBoostClassifier # 创建合成分类数据集 X, y = make_classification(random_state=1) # 配置集成模型 model = AdaBoostClassifier(n_estimators=50) # 配置重采样方法 cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1) # 使用重采样方法在数据集上评估集成 n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1) # 报告集成性能 print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores))) |
您的任务
在本课中,您必须运行该示例并查看评估模型的结果。
为了获得额外加分,评估在集成中使用更多决策树的效果,甚至更改所使用的基本学习器(注意,它必须支持加权训练数据)。
请在下面的评论中发布您的答案。我很想看看您能想出什么。
在下一课中,您将学习如何开发和评估梯度提升集成。
第 05 课:梯度提升集成
在本课程中,您将学习梯度提升集成。
梯度提升是提升集成算法的框架,也是 AdaBoost 的扩展。
它将提升重新定义为统计框架下的加性模型,并允许使用任意损失函数使其更灵活,并使用损失惩罚(收缩)来减少过拟合。
梯度提升还将 bagging 的思想引入到集成成员中,例如对训练数据集的行和列进行采样,这被称为随机梯度提升。
它是一种非常成功的结构化或表格数据集成技术,尽管由于模型是顺序添加的,拟合模型可能会很慢。已经开发出更高效的实现,例如流行的极端梯度提升(XGBoost)和轻量级梯度提升机(LightGBM)。
梯度提升在 scikit-learn 中可通过 GradientBoostingClassifier 和 GradientBoostingRegressor 类使用,它们默认使用决策树作为基础模型。您可以通过“n_estimators”参数指定要创建的树的数量,并通过“learning_rate”参数(默认为 0.1)指定控制每棵树贡献的学习率。
下面列出了用于分类的梯度提升集成评估的完整示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 用于分类的梯度提升集成评估示例 from numpy import mean from numpy import std from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.ensemble import GradientBoostingClassifier # 创建合成分类数据集 X, y = make_classification(random_state=1) # 配置集成模型 model = GradientBoostingClassifier(n_estimators=50) # 配置重采样方法 cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1) # 使用重采样方法在数据集上评估集成 n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1) # 报告集成性能 print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores))) |
您的任务
在本课中,您必须运行该示例并查看评估模型的结果。
为了获得额外加分,评估在集成中使用更多决策树的效果或尝试不同的学习率值。
请在下面的评论中发布您的答案。我很想看看您能想出什么。
在下一课中,您将学习如何开发和评估投票集成。
第 06 课:投票集成
在本课程中,您将学习投票集成。
投票集成使用简单的统计方法来组合来自多个模型的预测。
通常,这涉及在相同的训练数据集上拟合多种不同类型的模型,然后在回归情况下计算平均预测,或者在分类情况下选择投票最多的类别标签,这被称为硬投票。
在分类问题中,当预测类别标签的概率时,也可以使用投票,通过求和预测概率并选择总和概率最大的标签。这被称为软投票,当集成中使用的基础模型原生支持预测类别概率时,它更受青睐,因为它可能带来更好的性能。
投票集成在 scikit-learn 中可通过 VotingClassifier 和 VotingRegressor 类使用。可以向模型提供一个基础模型列表作为参数,列表中每个模型都必须是一个包含名称和模型的元组,例如 ('lr', LogisticRegression())。分类使用的投票类型可以通过“voting”参数指定,并设置为“soft”或“hard”之一。
下面列出了用于分类的投票集成评估的完整示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 用于分类的投票集成评估示例 from numpy import mean from numpy import std from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.ensemble import VotingClassifier from sklearn.naive_bayes import GaussianNB 从 sklearn.线性模型 导入 LogisticRegression # 创建合成分类数据集 X, y = make_classification(random_state=1) # 配置集成中使用的模型 models = [('lr', LogisticRegression()), ('nb', GaussianNB())] # 配置集成模型 model = VotingClassifier(models, voting='soft') # 配置重采样方法 cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1) # 使用重采样方法在数据集上评估集成 n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1) # 报告集成性能 print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores))) |
您的任务
在本课中,您必须运行该示例并查看评估模型的结果。
为了获得额外加分,请评估在集成中尝试不同类型的模型或甚至将投票类型从软投票更改为硬投票的效果。
请在下面的评论中发布您的答案。我很想看看您能想出什么。
在下一课中,您将学习如何开发和评估堆叠集成。
第 07 课:堆叠集成
在本课程中,您将学习堆叠泛化或堆叠集成。
堆叠涉及结合多种不同类型的基础模型的预测,这与投票非常相似。
与投票的重要区别在于,堆叠使用另一个机器学习模型来学习如何最好地组合基础模型的预测。这通常是一个线性模型,例如用于回归问题的线性回归或用于分类问题的逻辑回归,但也可以是您喜欢的任何机器学习模型。
元模型是在基础模型对样本外数据所做的预测上训练的。
这涉及对每个基础模型使用k 折交叉验证,并存储所有折叠外预测。然后,基础模型在整个训练数据集上训练,而元模型在折叠外预测上训练,并学习信任哪个模型、信任程度以及在何种情况下信任。
尽管堆叠在内部使用 k 折交叉验证来训练元模型,但您可以以任何您喜欢的方式评估堆叠模型,例如通过训练-测试拆分或 k 折交叉验证。模型的评估与此内部的重采样-训练过程是分开的。
堆叠集成在 scikit-learn 中可通过 StackingClassifier 和 StackingRegressor 类使用。可以向模型提供一个基础模型列表作为参数,列表中每个模型都必须是一个包含名称和模型的元组,例如 ('lr', LogisticRegression())。元学习器可以通过“final_estimator”参数指定,重采样策略可以通过“cv”参数指定,并且可以简单地设置为一个整数,表示交叉验证折叠的数量。
下面列出了用于分类的堆叠集成评估的完整示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 用于分类的堆叠集成评估示例 from numpy import mean from numpy import std from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.ensemble import StackingClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.tree import DecisionTreeClassifier 从 sklearn.线性模型 导入 LogisticRegression # 创建合成分类数据集 X, y = make_classification(random_state=1) # 配置集成中使用的模型 models = [('knn', KNeighborsClassifier()), ('tree',DecisionTreeClassifier())] # 配置集成模型 model = StackingClassifier(models, final_estimator=LogisticRegression(), cv=3) # 配置重采样方法 cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1) # 使用重采样方法在数据集上评估集成 n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1) # 报告集成性能 print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores))) |
您的任务
在本课中,您必须运行该示例并查看评估模型的结果。
为了获得额外加分,评估在集成中尝试不同类型的模型以及不同元模型来组合预测的效果。
请在下面的评论中发布您的答案。我很想看看您能想出什么。
这是最后一课。
结束!
(看看您取得了多大的进步)
您做到了。干得好!
花点时间回顾一下您已经走了多远。
您发现了:
- 什么是集成学习,以及为什么要在预测建模项目中使用它。
- 如何使用自助聚合(Bagging)集成。
- 如何将随机森林集成作为 Bagging 的扩展来使用。
- 如何使用自适应提升或 AdaBoost 集成。
- 如何使用梯度提升集成。
- 如何使用投票集成来组合模型的预测。
- 如何学习使用堆叠集成来组合模型的预测。
总结
您对这个迷你课程的学习情况如何?
您喜欢这个速成课程吗?
您有任何问题吗?有没有遇到什么难点?
告诉我。在下面留言。
作业 1。
Bagging:用于医学诊断/预后的机器学习——医学诊断算法规则提取(REMED)。对于此类需要预测准确性以及所得模式的可理解性和可靠性的领域,采用符号分类器(例如决策树和规则),其基于三个阶段:(i) 选择最相关的属性;(ii) 选择分离每个类别示例的划分;(iii) 构建允许对示例进行分类的模型;这是因为通过它们,将生成人类可理解的模型,能够为医务人员提供新的视角,帮助他们在医学诊断/预后的艰难任务中做出正确决策。
Boosting:通过应用分类树和 Boosting 技术预测哥伦比亚公司的财务脆弱性。应用预测企业破产的技术:分类树和 Boosting。它详细介绍了方法程序、数据净化和处理标准,以及模型估计过程,最后提出了在更大程度上解释企业破产的财务指标集:偿付能力和负债水平;因此,作为最有效的预测模型,利用分类率和基于 ROC 曲线下面积的度量等标准。
Stacking:聚合方法在气象时间序列异常值检测中的应用。该应用允许将 3 个非聚合分类器(向量支持回归、ARIMA、贝叶斯网络)和 3 个聚合分类器(堆叠、Bagging 和 AdaBoost)应用于 3 个气象测量数据集(降水、最高温度和太阳辐射)。比较分类器。首先,通过对每个数据集进行多次测试来平均分类器。然后,通过统计假设检验,比较分类器获得的均值,以确定观察到的差异是否显著。最后,对结果进行了分析,重点是比较聚合分类器与每个数据集中表现最好的非聚合算法的性能。总的来说,发现通过使用聚合方法检测某些单变量时间序列中的点异常值,可以显著提高性能。然而,为了实现这种改进,必须满足一些条件,尽管这些条件因添加的方法而异,但通常旨在提高基础分类器的多样性。当这些条件不满足时,聚合方法的性能与数据集中表现最好的非聚合算法没有显著差异。
干得好!
第 01 课:什么是集成学习?
感谢您的精彩帖子,
为了避免冗长的文本,我只列出集成学习的三个应用名称。
1. 医学:神经科学、元古代、医学诊断等。
2. 人脸识别
3. 欺诈检测
任务 2
n_estimators=50 > 平均准确率:0.953 (0.067)
n_estimators=70 > 平均准确率:0.947 (0.072) –
n_estimators=100 > 平均准确率:0.943 (0.080) –
n_estimators=130 > 平均准确率:0.947 (0.072) +
n_estimators=140 > 平均准确率:0.950 (0.067) +
n_estimators=150 > 平均准确率:0.943 (0.080) –
n_estimators=190 > 平均准确率:0.953 (0.067) +
n_estimators=200 > 平均准确率:0.950 (0.067) –
n_estimators=300 > 平均准确率:0.943 (0.080) –
n_estimators=350 > 平均准确率:0.953 (0.067) +
:(
干得好。
任务 3
n_estimators=50 , n_splits=10 > 平均准确率:0.957 (0.067)
n_estimators=100 , n_splits=10 > 平均准确率:0.960 (0.066) +
n_estimators=200 , n_splits=10 > 平均准确率:0.957 (0.067) –
n_estimators=250 , n_splits=10 > 平均准确率:0.960 (0.066) +
n_estimators=300 , n_splits=10 > 平均准确率:0.957 (0.072) –
n_estimators=500 , n_splits=10 > 平均准确率:0.957 (0.072)
n_estimators=999 , n_splits=10 > 平均准确率:0.957 (0.072)
n_estimators=50 , n_splits=2 > 平均准确率:0.963 (0.014)
n_estimators=50 , n_splits=5 > 平均准确率:0.953 (0.046) –
n_estimators=50 , n_splits=10 > 平均准确率:0.957 (0.067) +
n_estimators=50 , n_splits=15 > 平均准确率:0.958 (0.084) +
n_estimators=50 , n_splits=20 > 平均准确率:0.960 (0.080) +
n_estimators=50 , n_splits=30 > 平均准确率:0.965 (0.102) ++
n_estimators=50 , n_splits=45 > 平均准确率:0.963 (0.126) –
n_estimators=50 , n_splits=49 > 平均准确率:0.959 (0.137) –
n_estimators=100 , n_splits=30 > 平均准确率:0.961 (0.107)
n_estimators=250 , n_splits=30 > 平均准确率:0.965 (0.102) +++
干得好!
任务 4:AdaBoost 集成
::: n_splits=10 ::
n_estimators=50 > 平均准确率:0.947 (0.088)
n_estimators=70 > 平均准确率:0.953 (0.088) +
n_estimators=100 > 平均准确率:0.953 (0.088) .
n_estimators=130 > 平均准确率:0.957 (0.088) +++
n_estimators=140 > 平均准确率:0.957 (0.088) .
n_estimators=150 > 平均准确率:0.957 (0.088) .
n_estimators=190 > 平均准确率:0.957 (0.088) .
n_estimators=200 > 平均准确率:0.957 (0.088) .
n_estimators=300 > 平均准确率:0.953 (0.099) –
n_estimators=350 > 平均准确率:0.953 (0.099) .
::: n_estimators=130 ::
n_splits=2 > 平均准确率:0.950 (0.015) –
n_splits=8 > 平均准确率:0.944 (0.088) –
n_splits=16 > 平均准确率:0.957 (0.111) +
n_splits=20 > 平均准确率:0.960 (0.088) +
n_splits=24 > 平均准确率:0.963 (0.102) +
n_splits=28 > 平均准确率:0.966 (0.095) +++
n_splits=30 > 平均准确率:0.965 (0.102) –
n_splits=32 > 平均准确率:0.957 (0.124) –
::: n_estimators=130 , n_splits=28 ::
n_repeats=1 > 平均准确率:0.973 (0.077) +++
n_repeats=3 > 平均准确率:0.966 (0.095) –
n_repeats=5 > 平均准确率:0.963 (0.099) –
n_repeats=7 > 平均准确率:0.963 (0.098) .
n_repeats=10 > 平均准确率:0.962 (0.104) –
n_repeats=12 > 平均准确率:0.962 (0.104) .
n_repeats=18 > 平均准确率:0.959 (0.108) –
n_repeats=22 > 平均准确率:0.958 (0.108) –
n_estimators=130 , n_splits=28, n_repeats=1 > 平均准确率:0.973 (0.077)
B)
干得好!
任务 5
::: n_splits=10 ::
n_estimators=50 > 平均准确率:0.927 (0.100)
n_estimators=70 > 平均准确率:0.927 (0.089) .
n_estimators=100 > 平均准确率:0.927 (0.089) .
n_estimators=130 > 平均准确率:0.923 (0.099) –
n_estimators=140 > 平均准确率:0.923 (0.099) .
n_estimators=150 > 平均准确率:0.923 (0.099) .
n_estimators=190 > 平均准确率:0.923 (0.099) .
n_estimators=200 > 平均准确率:0.927 (0.089) +++
n_estimators=220 > 平均准确率:0.923 (0.099) –
n_estimators=250 > 平均准确率:0.923 (0.099) .
n_estimators=270 > 平均准确率:0.920 (0.098) –
n_estimators=300 > 平均准确率:0.920 (0.098) .
n_estimators=350 > 平均准确率:0.923 (0.099) +
n_estimators=360 > 平均准确率:0.927 (0.089) +
n_estimators=370 > 平均准确率:0.923 (0.099) – .
::: n_estimators=200 ::
n_splits=2 > 平均准确率:0.933 (0.034)
n_splits=8 > 平均准确率:0.934 (0.064) +
n_splits=16 > 平均准确率:0.934 (0.099) .
n_splits=20 > 平均准确率:0.927 (0.115) –
n_splits=24 > 平均准确率:0.935 (0.126) +
n_splits=28 > 平均准确率:0.947 (0.112) +++
n_splits=30 > 平均准确率:0.941 (0.126) –
n_splits=32 > 平均准确率:0.938 (0.140) –
::: n_estimators=200 , n_splits=28 ::
n_repeats=1 > 平均准确率:0.946 (0.103)
n_repeats=3 > 平均准确率:0.943 (0.116) –
n_repeats=5 > 平均准确率:0.943 (0.114) .
n_repeats=7 > 平均准确率:0.940 (0.120) –
n_repeats=10 > 平均准确率:0.938 (0.128) –
n_repeats=12 > 平均准确率:0.934 (0.132) –
n_repeats=18 > 平均准确率:0.937 (0.131) +
n_repeats=22 > 平均准确率:0.938 (0.131) +++
n_repeats=24 > 平均准确率:0.934 (0.134) –
n_repeats=26 > 平均准确率:0.936 (0.132) +
n_repeats=30 > 平均准确率:0.936 (0.134) .
干得好。
任务 6
GNB: GaussianNB
LR: LogisticRegression
RFC: RandomForestClassifier
SGD: SGDClassifier
models=LR, GNB | voting=soft > 平均准确率:0.960 (0.061) +++
models=LR, GNB | voting=hard > 平均准确率:0.957 (0.062) –
models=LR, GNB, RFC | voting=soft > 平均准确率:0.960 (0.061) +++
models=LR, GNB, RFC | voting=hard > 平均准确率:0.953 (0.067) –
models=SGD, LR, SVC | voting=hard > 平均准确率:0.943 (0.067)
干得漂亮!
任务 7
DTC: DecisionTreeClassifier
KNC: KNeighborsClassifier
LR: LogisticRegression
LSVC: LinearSVC
RFC: RandomForestClassifier
::: final_estimator=LR ::
models=KNC, DTC > 平均准确率:0.930 (0.090)
models=RFC, LSVR > 平均准确率:0.910 (0.098)
models=KNC, RFC, LSVR > 平均准确率:0.910 (0.098)
models= KNC, DTC, RFC, LSVR > 平均准确率:0.943 (0.067)
::: final_estimator=SVC ::
models=KNC, DTC > 平均准确率:0.933 (0.083)
models=RFC, LSVR > 平均准确率:0.910 (0.098)
models=KNC, RFC, LSVR > 平均准确率:0.913 (0.088)
models= KNC, DTC, RFC, LSVR > 平均准确率:0.943 (0.072)
::: final_estimator=LinearSVC ::
models=KNC, DTC > 平均准确率:0.923 (0.088)
models=RFC, LSVR > 平均准确率:0.943 (0.067)
models=KNC, RFC, LSVR > 平均准确率:0.947 (0.067) +++
models= KNC, DTC, RFC, LSVR > 平均准确率:0.947 (0.067)+++
这是一个很棒的帖子!非常感谢!
干得好。
集成机器学习的一个应用可能是多类别分类。
干得好。
您的速成课程总能以非常清晰的方式帮助理解概念。感谢您的出色工作。
不客气。
下面列出了一些集成学习的应用
肺部异常检测
语音情感识别
预测加密货币时间序列
高光谱图像分析
感谢您的宝贵讲座,先生
感谢您的评论。希望您也喜欢其他帖子!
集成的问题在于其鲁棒性。对此有什么评论:例如,假设你对 2015 年至 2016 年的数据进行了集成。你使用上述数据(2015-2016)获得了模型设置和校准。
现在,使用这种固定的模型设置/校准,使用 2014 年至 2015 年的数据。
与 2015-2016 年的结果相比,2014 年至 2015 年的结果如何?
对此有什么评论吗?
嗨 Mecobio…我没有像你描述的那样实验数据。我很乐意知道这种实现会有什么发现。
AdaBoost 可以应用于朴素贝叶斯吗?朴素贝叶斯作为基础估计器?
嗨 helmi…您可能会对以下资源感兴趣
https://dergipark.org.tr/en/download/article-file/2013021
我可以使用 boosting 来 SMOTE 吗?哪种方法更能改善 SMOTE 的 boosting 或 bagging?
嗨 maram…以下资源可能会对您有所帮助
https://machinelearning.org.cn/smote-oversampling-for-imbalanced-classification/
集成学习的应用
集成学习是一种机器学习技术,它结合多个独立模型的预测以产生更准确和鲁棒的预测。集成学习是深度学习中相当常见的策略,并已应用于解决各种问题。分类和回归:集成方法广泛用于分类和回归任务。像随机森林和梯度提升这样的算法创建决策树的集成来进行预测。这些集成通常通过减少过拟合和提高预测准确性来优于单个决策树。
1. 异常检测
2. 疾病检测
3. 遥感
4. 欺诈检测
集成技术可用于构建一个对噪声数据更具鲁棒性,并且能更好地区分正常和异常实例的模型。一种常见的方法是结合使用不同的异常检测算法作为集成,以提高整体性能。
第 2 天:Bagging 集成
# 用于分类的 bagging 集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
来自 sklearn.ensemble 的 BaggingClassifier
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成模型
model = BaggingClassifier(n_estimators=50)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
答案在此
平均准确率:0.950 (0.072)
平均准确率为 0.950,标准差为 0.072,这表明被评估的模型或系统表现相当好。0.950 的准确率意味着它能高精度地正确分类或预测结果,而低标准差则表明性能在不同运行或样本之间是一致的。
第 3 天:随机森林集成
# 用于分类的随机森林集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import RandomForestClassifier
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成模型
model = RandomForestClassifier(n_estimators=50)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
# 此外,我们可以评估在集成中更改决策树的数量(n_estimators)和在每个分割点考虑的特征数量(max_features)的影响
# 集成中决策树的数量(我尝试了不同的值)
n_estimators_values = [10, 50, 100, 200]
结果如下
n_estimators=10, max_features=auto
平均准确率:0.940 (0.076)
n_estimators=10, max_features=sqrt
平均准确率:0.940 (0.076)
n_estimators=10, max_features=log2
平均准确率:0.940 (0.076)
n_estimators=50, max_features=auto
平均准确率:0.950 (0.067)
n_estimators=50, max_features=sqrt
平均准确率:0.950 (0.067)
n_estimators=50, max_features=log2
平均准确率:0.950 (0.067)
n_estimators=100, max_features=auto
平均准确率:0.953 (0.067)
n_estimators=100, max_features=sqrt
平均准确率:0.953 (0.067)
n_estimators=100, max_features=log2
平均准确率:0.953 (0.067)
n_estimators=200, max_features=auto
平均准确率:0.957 (0.067)
n_estimators=200, max_features=sqrt
平均准确率:0.957 (0.067)
n_estimators=200, max_features=log2
平均准确率:0.957 (0.067)
从结果可以看出,存在一些差异。随着“n”值的增加,准确率也在一定程度上增加。
第 4 天:AdaBoost 集成
# AdaBoost 分类集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import AdaBoostClassifier
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成模型
model = AdaBoostClassifier(n_estimators=50)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
平均准确率:0.947 (0.088)
该合成数据集上包含 50 棵决策树的 AdaBoost 集成的平均准确率约为 0.94/0,标准差约为 0.088。
为了获得额外加分,让我们实验集成中不同数量的决策树并更改基础学习器
更改决策树的数量
我们可以尝试不同的 n_estimators 值,看看它如何影响性能。让我们尝试 100 棵决策树
# 将决策树的数量更改为 100
model = AdaBoostClassifier(n_estimators=100)
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
print(‘Mean Accuracy with 100 Decision Trees: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
使用 100 棵决策树的平均准确率:0.950 (0.089)
更改基础学习器
AdaBoost 允许您更改基础学习器。让我们尝试使用不同的基础学习器,例如最大深度为 2 的决策树
from sklearn.tree import DecisionTreeClassifier
# 将基础学习器更改为最大深度为 2 的决策树
base_estimator = DecisionTreeClassifier(max_depth=2)
model = AdaBoostClassifier(base_estimator=base_estimator, n_estimators=50)
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
print(‘Mean Accuracy with Decision Tree Base Learner (Max Depth 2): %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
使用决策树基础学习器(最大深度为 2)的平均准确率:0.940 (0.076)
第 5 天:梯度提升集成
要运行提供的代码并在合成分类数据集上评估梯度提升分类器,请使用以下步骤
# 用于分类的梯度提升集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import GradientBoostingClassifier
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成模型
model = GradientBoostingClassifier(n_estimators=50)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
平均准确率:0.930 (0.090)
让我们通过修改模型定义来评估在集成中使用更多决策树或不同学习率值的影响。
model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)
# 用于分类的梯度提升集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import GradientBoostingClassifier
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成模型
model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
平均准确率:0.923 (0.099)
第 5 天:梯度提升集成
要运行提供的代码并在合成分类数据集上评估梯度提升分类器,请使用以下步骤
# 用于分类的梯度提升集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import GradientBoostingClassifier
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成模型
model = GradientBoostingClassifier(n_estimators=50)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
平均准确率:0.930 (0.090)
让我们通过修改模型定义来评估在集成中使用更多决策树或不同学习率值的影响。
model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)
# 用于分类的梯度提升集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import GradientBoostingClassifier
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成模型
model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
平均准确率:0.923 (0.099)
增加树的数量可能会导致更长的训练时间,但可能会提高准确性
第 6 天:投票集成
# 用于分类的投票集成评估示例
from numpy import mean
from numpy import std
从 sklearn.datasets 导入 make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
来自 sklearn.ensemble 的 VotingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成中使用的模型
models = [(‘lr’, LogisticRegression()), (‘nb’, GaussianNB())]
# 配置集成模型
model = VotingClassifier(models, voting=’soft’)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
平均准确率:0.960 (0.061)
将集成更改为使用硬投票并向集成添加支持向量机 (SVM) 分类器的示例
来自 sklearn.ensemble 的 VotingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC # 导入 SVC
# 创建合成分类数据集
X, y = make_classification(random_state=1)
# 配置集成中使用的模型(包括 SVM)
models = [(‘lr’, LogisticRegression()), (‘nb’, GaussianNB()), (‘svm’, SVC())]
# 配置集成模型(硬投票)
model = VotingClassifier(models, voting=’hard’)
# 配置重采样方法
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 使用重采样方法在数据集上评估集成
n_scores = cross_val_score(model, X, y, scoring=’accuracy’, cv=cv, n_jobs=-1)
# 报告集成性能
print(‘Mean Accuracy (Hard Voting): %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
平均准确率(硬投票):0.957 (0.062)
应用包括
1. 欺诈检测
2. 疾病检测
3. 遥感
感谢 Jenkins 的反馈!如果您有任何问题,请告诉我们!
好的!会的。谢谢你
第 5 天
平均准确率:0.930 (0.090)