Skip to Content
机器学习速查手册
book

机器学习速查手册

by Matt Harrison
July 2025
Intermediate to advanced
320 pages
3h 10m
Chinese
O'Reilly Media, Inc.
Content preview from 机器学习速查手册

第 8 章 特征选择 特征选择

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

我们使用特征选择来选择对模型有用的特征。不相关的特征可能会对模型产生负面影响,相关的特征可能会使回归系数(或树模型中的特征重要性)不稳定或难以解释。

维度诅咒是另一个需要考虑的问题。随着数据维数的增加,数据会变得更加稀疏。随着维度的增加,邻域计算往往会失去作用。

另外,训练时间通常是列数的函数(有时比线性还差)。 如果列数简洁精确,就能在更短的时间内建立更好的模型。 我们将使用上一章中的agg_df 数据集来举例说明。请记住,这是在泰坦尼克号数据集中增加了一些额外的舱位信息列。 由于该数据集汇总了每个舱位的数值,因此会显示出许多相关性。其他选项包括 PCA 和查看树分类器的.feature_importances_ 。

共线列

我们可以使用之前定义的correlated_columns 函数或运行以下代码来查找相关系数达到或超过 .95 的列:

>>> limit = 0.95
>>> corr = agg_df.corr()
>>> mask = np.triu(
...     np.ones(corr.shape), k=1
... ).astype(bool)
>>> corr_no_diag = corr.where(mask)
>>> coll = [
...     c
...     for c in corr_no_diag.columns
...     if any(abs(corr_no_diag[c]) > threshold)
... ]
>>> coll
['pclass_min', 'pclass_max', 'pclass_mean',
 'sibsp_mean', 'parch_mean', 'fare_mean',
 'body_max', 'body_mean', 'sex_male', 'embarked_S']

黄砖Rank2 可视化器(如前所述)将绘制相关性热图。

rfpimp 软件包具有多重共线性可视化功能。plot_dependence_heatmap 函数会根据训练数据集中的其他列为每一列数值训练一个随机森林。依存值是预测该列的袋外估计值 R2 分数(见图 8-1)。

建议使用此图的方法是找到接近 1 的值。X 轴上的标签是预测 Y 轴标签的特征。如果一个特征预测了另一个特征,则可以移除被预测的特征(Y 轴上的特征)。在我们的示例中,fare 预测pclass,sibsp,parch, 和embarked_Q 。我们应该可以保留fare 并移除其他特征,从而获得相似的性能:

>>> rfpimp.plot_dependence_heatmap(
...     rfpimp.feature_dependence_matrix(X_train),
...     value_fontsize=12,
...     label_fontsize=14,
...     figsize=(8, 8),sn
... )
>>> fig = plt.gcf()
>>> fig.savefig(
...     "images/mlpr_0801.png",
...     dpi=300,
...     bbox_inches="tight",
... )
图 8-1. 依赖性热图。Pclass、sibsp、parch 和 embarked_Q 可以通过票价预测,因此我们可以将它们移除。

下面的代码显示,如果我们删除这些列,也能得到类似的分数: ...

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

雷达趋势观察:2025年7月

雷达趋势观察:2025年7月

Mike Loukides
R深度学习权威指南

R深度学习权威指南

Posts & Telecom Press, Joshua F. Wiley
Python高级编程(第2版)

Python高级编程(第2版)

Posts & Telecom Press, Michał Jaworski, Tarek Ziadé

Publisher Resources

ISBN: 9798341663046