使用快速独立成分分析(FastICA)和主成分分析(PCA)进行独立成分分析

Machine LearningMachine LearningBeginner
立即练习

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

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

简介

本实验展示了两种流行的独立成分分析技术——FastICA 和 PCA 算法的使用。独立成分分析(ICA)是一种将多变量信号分离为最大程度独立的加性子成分的方法。该技术在特征空间中找到对应于具有高非高斯性投影的方向。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/AdvancedDataAnalysisandDimensionalityReductionGroup(["Advanced Data Analysis and Dimensionality Reduction"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/AdvancedDataAnalysisandDimensionalityReductionGroup -.-> sklearn/decomposition("Matrix Decomposition") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/decomposition -.-> lab-49162{{"使用快速独立成分分析(FastICA)和主成分分析(PCA)进行独立成分分析"}} ml/sklearn -.-> lab-49162{{"使用快速独立成分分析(FastICA)和主成分分析(PCA)进行独立成分分析"}} end

生成样本数据

在这一步中,我们使用一个高度非高斯过程(自由度较低的双学生t分布)来生成样本数据。

import numpy as np

from sklearn.decomposition import PCA, FastICA

rng = np.random.RandomState(42)
S = rng.standard_t(1.5, size=(20000, 2))
S[:, 0] *= 2.0

## 混合数据
A = np.array([[1, 1], [0, 2]])  ## 混合矩阵

X = np.dot(S, A.T)  ## 生成观测值

使用主成分分析(PCA)算法

在这一步中,我们使用主成分分析(PCA)算法在原始特征空间中找到与解释最大方差的方向相对应的正交方向。

pca = PCA()
S_pca_ = pca.fit(X).transform(X)

使用快速独立成分分析(FastICA)算法

在这一步中,我们使用快速独立成分分析(FastICA)算法,该算法在特征空间中找到与具有高非高斯性的投影相对应的方向。

ica = FastICA(random_state=rng, whiten="arbitrary-variance")
S_ica_ = ica.fit(X).transform(X)  ## 估计源信号

S_ica_ /= S_ica_.std(axis=0)

绘制结果

在这一步中,我们使用matplotlib绘制结果。

import matplotlib.pyplot as plt

def plot_samples(S, axis_list=None):
    plt.scatter(
        S[:, 0], S[:, 1], s=2, marker="o", zorder=10, color="steelblue", alpha=0.5
    )
    if axis_list is not None:
        for axis, color, label in axis_list:
            axis /= axis.std()
            x_axis, y_axis = axis
            plt.quiver(
                (0, 0),
                (0, 0),
                x_axis,
                y_axis,
                zorder=11,
                width=0.01,
                scale=6,
                color=color,
                label=label,
            )

    plt.hlines(0, -3, 3)
    plt.vlines(0, -3, 3)
    plt.xlim(-3, 3)
    plt.ylim(-3, 3)
    plt.xlabel("x")
    plt.ylabel("y")


plt.figure()
plt.subplot(2, 2, 1)
plot_samples(S / S.std())
plt.title("真实独立源")

axis_list = [(pca.components_.T, "橙色", "主成分分析(PCA)"), (ica.mixing_, "红色", "快速独立成分分析(ICA)")]
plt.subplot(2, 2, 2)
plot_samples(X / np.std(X), axis_list=axis_list)
legend = plt.legend(loc="lower right")
legend.set_zorder(100)

plt.title("观测值")

plt.subplot(2, 2, 3)
plot_samples(S_pca_ / np.std(S_pca_, axis=0))
plt.title("主成分分析(PCA)恢复的信号")

plt.subplot(2, 2, 4)
plot_samples(S_ica_ / np.std(S_ica_))
plt.title("快速独立成分分析(ICA)恢复的信号")

plt.subplots_adjust(0.09, 0.04, 0.94, 0.94, 0.26, 0.36)
plt.show()

总结

在本实验中,我们学习了如何在Python中使用快速独立成分分析(FastICA)和主成分分析(PCA)算法来执行独立成分分析,以及如何使用matplotlib可视化结果。