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

机器学习速查手册

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

第 17 章 降维 降维

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

有许多技术可以将特征分解成更小的子集。这对于探索性数据分析、可视化、制作预测模型或聚类都很有用。

在本章中,我们将使用各种技术探索泰坦尼克号数据集。我们将研究 PCA、UMAP、t-SNE 和 PHATE。

以下是数据:

>>> ti_df = tweak_titanic(orig_df)
>>> std_cols = "pclass,age,sibsp,fare".split(",")
>>> X_train, X_test, y_train, y_test = get_train_test_X_y(
...     ti_df, "survived", std_cols=std_cols
... )
>>> X = pd.concat([X_train, X_test])
>>> y = pd.concat([y_train, y_test])

PCA

主成分分析(PCA)需要一个由行(样本)和列(特征)组成的矩阵(X)。PCA 会返回一个新矩阵,其列是原始列的线性组合。这些线性组合能使方差最大化。

每一列都与其他列正交(成直角)。列按方差递减的顺序排序。

Scikit-learn 已实现了这一模型。在运行算法之前,最好先将数据标准化。调用.fit 方法后,您将可以访问一个 .explained_variance_ratio_属性,该属性列出了每一列的方差百分比。

PCA 对于可视化二维(或三维)数据非常有用。它还可以作为预处理步骤,过滤掉数据中的随机噪音。它适合查找全局结构,但不适合查找局部结构,而且对线性数据效果很好。

在本例中,我们将对泰坦尼克号的特征运行 PCA。PCA 类是 scikit-learn 中的一个转换器;先调用.fit 方法教它如何获取主成分,然后调用.transform 将矩阵转换为主成分矩阵:

>>> from sklearn.decomposition import PCA
>>> from sklearn.preprocessing import (
...     StandardScaler,
... )
>>> pca = PCA(random_state=42)
>>> X_pca = pca.fit_transform(
...     StandardScaler().fit_transform(X)
... )
>>> pca.explained_variance_ratio_
array([0.23917891, 0.21623078, 0.19265028,
  0.10460882, 0.08170342, 0.07229959,
  0.05133752, 0.04199068])

>>> pca.components_[0]
arrayarray([-0.63368693,  0.39682566,
  0.00614498,  0.11488415,  0.58075352,
  -0.19046812, -0.21190808, -0.09631388])

实例参数:

n_components=None

要生成的组件数。如果None ,则返回与列数相同的数字。可以是浮点数(0,1),然后将根据需要创建尽可能多的分量以获得该方差比。

copy=True

如果True ,将在.fit 上对数据进行变异。

whiten=False

转换后对数据进行白化处理,以确保不相关的 ...

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
AI工程

AI工程

Chip Huyen

Publisher Resources

ISBN: 9798341663046