使用FastICA进行盲源分离#

一个从噪声数据中估计源的示例。

独立成分分析(ICA) 用于在给定噪声测量的情况下估计源。想象3个乐器同时演奏,3个麦克风记录混合信号。ICA用于恢复源,即每个乐器演奏的内容。重要的是,PCA在恢复我们的 乐器 时失败了,因为相关信号反映了非高斯过程。

生成示例数据#

import numpy as np
from scipy import signal

np.random.seed(0)
n_samples = 2000
time = np.linspace(0, 8, n_samples)

s1 = np.sin(2 * time)  # Signal 1 : sinusoidal signal
s2 = np.sign(np.sin(3 * time))  # Signal 2 : square signal
s3 = signal.sawtooth(2 * np.pi * time)  # Signal 3: saw tooth signal

S = np.c_[s1, s2, s3]
S += 0.2 * np.random.normal(size=S.shape)  # Add noise

S /= S.std(axis=0)  # Standardize data
# Mix data
A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]])  # Mixing matrix
X = np.dot(S, A.T)  # Generate observations

拟合ICA和PCA模型#

from sklearn.decomposition import PCA, FastICA

# Compute ICA
ica = FastICA(n_components=3, whiten="arbitrary-variance")
S_ = ica.fit_transform(X)  # Reconstruct signals
A_ = ica.mixing_  # Get estimated mixing matrix

# 我们可以通过还原解混过程来 `证明` ICA模型的适用性。
assert np.allclose(X, np.dot(S_, A_.T) + ica.mean_)

# 为了比较,计算PCA
pca = PCA(n_components=3)
H = pca.fit_transform(X)  # Reconstruct signals based on orthogonal components

绘制结果#

import matplotlib.pyplot as plt

plt.figure()

models = [X, S, S_, H]
names = [
    "Observations (mixed signal)",
    "True Sources",
    "ICA recovered signals",
    "PCA recovered signals",
]
colors = ["red", "steelblue", "orange"]

for ii, (model, name) in enumerate(zip(models, names), 1):
    plt.subplot(4, 1, ii)
    plt.title(name)
    for sig, color in zip(model.T, colors):
        plt.plot(sig, color=color)

plt.tight_layout()
plt.show()
Observations (mixed signal), True Sources, ICA recovered signals, PCA recovered signals

Total running time of the script: (0 minutes 0.180 seconds)

Related examples

sphx_glr_auto_examples_decomposition_plot_ica_vs_pca.py

二维点云上的快速独立成分分析

正交匹配追踪

正交匹配追踪

增量PCA

增量PCA

使用核PCA进行图像去噪

使用核PCA进行图像去噪

Gallery generated by Sphinx-Gallery