FastICA と PCA を用いた独立成分分析

Machine LearningMachine LearningBeginner
オンラインで実践に進む

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

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、2 つの人気のある独立成分分析手法である FastICA と PCA アルゴリズムの使用方法を示します。独立成分分析(ICA:Independent Component Analysis)は、多変量信号を最大限に独立した加算サブコンポーネントに分離する方法です。この手法は、非ガウス性の高い射影に対応する特徴空間内の方向を見つけます。

VM のヒント

VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。

Jupyter Notebook の読み込みには数秒かかる場合があります。Jupyter Notebook の制限により、操作の検証は自動化できません。

学習中に問題がある場合は、Labby にお問い合わせください。セッション終了後にフィードバックを提供してください。すぐに問題を解決いたします。

サンプルデータの生成

このステップでは、自由度の少ない 2 つのスタデント 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

## Mix data
A = np.array([[1, 1], [0, 2]])  ## Mixing matrix

X = np.dot(S, A.T)  ## Generate observations

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)  ## Estimate the sources

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("True Independent Sources")

axis_list = [(pca.components_.T, "orange", "PCA"), (ica.mixing_, "red", "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("Observations")

plt.subplot(2, 2, 3)
plot_samples(S_pca_ / np.std(S_pca_, axis=0))
plt.title("PCA recovered signals")

plt.subplot(2, 2, 4)
plot_samples(S_ica_ / np.std(S_ica_))
plt.title("ICA recovered signals")

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

まとめ

この実験では、Python を使って FastICA と PCA アルゴリズムをどのように使うかを学び、matplotlib を使って結果を視覚化する方法を学びました。