소개
이 실습은 두 가지 인기 있는 독립 성분 분석 기법인 FastICA 및 PCA 알고리즘의 사용법을 보여줍니다. 독립 성분 분석 (ICA) 은 다변량 신호를 최대 독립성을 갖는 덧셈 하위 구성 요소로 분리하는 방법입니다. 이 기법은 특징 공간에서 높은 비정규성을 가진 투영에 해당하는 방향을 찾습니다.
VM 팁
VM 시작이 완료되면 왼쪽 상단 모서리를 클릭하여 Notebook 탭으로 전환하여 연습을 위한 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
## 혼합 데이터
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("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()
요약
이 실험에서는 파이썬에서 FastICA 및 PCA 알고리즘을 사용하여 독립 성분 분석을 수행하고 matplotlib 를 사용하여 결과를 시각화하는 방법을 배웠습니다.