流形学习比较

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,我们将比较不同的流形学习算法以执行非线性降维。这样做的目的是在保留原始数据基本特征的同时降低数据集的维度。

我们将使用S曲线数据集,这是一个常用于降维的数据集。我们将使用局部线性嵌入、等距映射嵌入、多维缩放、谱嵌入和T分布随机邻域嵌入等算法。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问Jupyter Notebook进行练习。

有时,你可能需要等待几秒钟让Jupyter Notebook完成加载。由于Jupyter Notebook的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向Labby提问。课程结束后提供反馈,我们将立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills ml/sklearn -.-> lab-49091{{"流形学习比较"}} end

数据集准备

我们首先生成S曲线数据集。

import matplotlib.pyplot as plt
from matplotlib import ticker

## unused but required import for doing 3d projections with matplotlib < 3.2
import mpl_toolkits.mplot3d  ## noqa: F401

from sklearn import manifold, datasets

n_samples = 1500
S_points, S_color = datasets.make_s_curve(n_samples, random_state=0)

局部线性嵌入

局部线性嵌入(Locally Linear Embedding,LLE)是一系列局部主成分分析,通过全局比较来找到最佳的非线性嵌入。我们将使用四种不同的LLE方法,即标准方法、局部切空间对齐、海森特征映射和改进的局部线性嵌入。

params = {
    "n_neighbors": n_neighbors,
    "n_components": n_components,
    "eigen_solver": "auto",
    "random_state": 0,
}

lle_standard = manifold.LocallyLinearEmbedding(method="standard", **params)
S_standard = lle_standard.fit_transform(S_points)

lle_ltsa = manifold.LocallyLinearEmbedding(method="ltsa", **params)
S_ltsa = lle_ltsa.fit_transform(S_points)

lle_hessian = manifold.LocallyLinearEmbedding(method="hessian", **params)
S_hessian = lle_hessian.fit_transform(S_points)

lle_mod = manifold.LocallyLinearEmbedding(method="modified", **params)
S_mod = lle_mod.fit_transform(S_points)

等距映射嵌入

等距映射(Isomap)旨在寻找一种低维嵌入,该嵌入能保持所有点之间的测地距离。

isomap = manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components, p=1)
S_isomap = isomap.fit_transform(S_points)

多维缩放

多维缩放(Multidimensional scaling,MDS)旨在寻找数据的低维表示,其中的距离能很好地反映原始高维空间中的距离。

md_scaling = manifold.MDS(
    n_components=n_components,
    max_iter=50,
    n_init=4,
    random_state=0,
    normalized_stress=False,
)
S_scaling = md_scaling.fit_transform(S_points)

用于非线性降维的谱嵌入

此实现使用拉普拉斯特征映射(Laplacian Eigenmaps),它通过图拉普拉斯矩阵的谱分解来找到数据的低维表示。

spectral = manifold.SpectralEmbedding(
    n_components=n_components, n_neighbors=n_neighbors
)
S_spectral = spectral.fit_transform(S_points)

T分布随机邻域嵌入

它将数据点之间的相似度转换为联合概率,并试图最小化低维嵌入的联合概率与高维数据的联合概率之间的库尔贝克-莱布勒散度(Kullback-Leibler divergence)。

t_sne = manifold.TSNE(
    n_components=n_components,
    perplexity=30,
    init="random",
    n_iter=250,
    random_state=0,
)
S_t_sne = t_sne.fit_transform(S_points)

总结

流形学习(Manifold Learning)是一种非线性降维方法。在本实验中,我们比较了不同的流形学习算法,如局部线性嵌入(Locally Linear Embeddings)、等距映射嵌入(Isomap Embedding)、多维缩放(Multidimensional Scaling)、谱嵌入(Spectral Embedding)和T分布随机邻域嵌入(T-distributed Stochastic Neighbor Embedding),以便对S曲线数据集进行非线性降维。