Skip to Content
基于scikit-learn和PyTorch的实践机器学习
book

基于scikit-learn和PyTorch的实践机器学习

by Aurélien Géron
October 2025
Intermediate to advanced
878 pages
12h 53m
Chinese
O'Reilly Media, Inc.
Content preview from 基于scikit-learn和PyTorch的实践机器学习

第 6 章 集合学习和随机森林

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

假设 你随机向数千人提出一个复杂的问题,然后汇总他们的答案。在很多情况下,你会发现这个汇总答案比专家的答案更好。这就是所谓的群众智慧。 同样,如果将一组预测因子(如分类器或回归因子)的预测结果汇总在一起,往往会得到比最佳单个预测因子更好的预测结果。一组预测因子被称为集合; 因此,这种技术被称为集合学习,集合学习算法被称为集合方法。

作为集合方法的一个例子,你可以训练一组决策树 分类器,每个分类器都对训练集的不同随机子集进行训练。然后,你可以得到所有单个决策树的预测结果,得票最多的类就是集合的预测结果(见第 5 章最后一个练习)。这样的决策树集合被称为随机森林,尽管它很简单,但却是当今最强大的机器学习算法之一。

正如第 2 章中所讨论的,在项目接近尾声时,一旦你已经构建了几个不错的预测器,就会经常使用集合方法,将它们组合成一个更好的预测器。事实上,在机器学习竞赛中,获胜的解决方案往往涉及多个集合方法--最著名的是在Netflix 大奖竞赛中。不过,集合学习也有一些缺点:与使用单一模型相比,集合学习需要更多的计算资源(包括训练和推理),部署和管理起来可能更加复杂,而且预测结果也更难解释。但优点往往大于缺点。

在本章中,我们将研究最流行的集合方法,包括投票分类器、装袋和粘贴集合、随机森林、提升和堆叠集合。

投票分类器

假设 你已经训练了几个分类器,每个分类器的准确率都在 80% 左右。你可能有一个逻辑回归分类器、一个 SVM 分类器、一个随机森林分类器、一个k 近邻分类器,也许还有其他一些分类器(见图 6-1)。

Diagram illustrating the training of diverse classifiers, including logistic regression, SVM, random forest, and others, highlighting their role in ensemble learning.
图 6-1. 训练不同的分类器

要创建一个更好的分类器,有一个非常简单的方法,那就是汇总每个分类器的预测结果:获得最多选票的类别就是组合的预测结果。这种得票最多的分类器被称为硬投票分类器 (见图 6-2)。

Diagram illustrating a hard voting classifier, where diverse predictors contribute their predictions, with the majority vote determining the ensemble's final prediction for a new instance.
图 6-2. 硬投票分类器预测

令人惊讶的是,这种投票分类器的准确率往往高于组合中的最佳分类器。事实上,即使每个分类器都是弱学习者 (这意味着它只比随机猜测稍好一些),只要集合中有足够多的弱学习者,而且它们足够多样化(即它们关注数据的不同方面,并犯下不同类型的错误),集合仍然可以成为强学习者 (实现高准确率)。

这怎么可能呢?下面的类比可以帮助我们揭开这个谜团。假设你有一枚略有偏差的硬币,正面出现的概率为 51%,反面出现的概率为 49%。如果你抛掷 1000 次,一般会得到 510 次正面和 490 次反面,因此正面占多数。如果你计算一下,就会发现抛掷 1000 次后得到正面占多数的概率接近 75%。抛掷硬币的次数越多,概率就越高(例如,抛掷 10,000 次后,概率将超过 97%)。这是大数定律的结果: ,抛掷硬币的次数越多,正面的比例就会越来越接近正面的概率(51%)。图 6-3显示了 10 次有偏差的抛硬币。您可以看到,随着抛掷次数的增加,人头比率越来越接近 51%。最终,所有 10 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

编写整洁的Python代码(第2版)

编写整洁的Python代码(第2版)

Posts & Telecom Press, Mariano Anaya
生成式人工智能可视化

生成式人工智能可视化

Priyanka Vergadia, Valliappa Lakshmanan
AI工程

AI工程

Chip Huyen

Publisher Resources

ISBN: 0642572270117