PCA vs ICA

PCA

import numpy
from sklearn import decomposition

x1 = numpy.random.normal(size=100)
x2 = numpy.random.normal(size=100)
x3 = x1+x2
x = numpy.c_[x1, x2, x3]

# pca = decomposition.PCA()
# pca.fit(x)
# print(pca.explained_variance_)
pca = decomposition.PCA(n_components=2)
x_ = pca.fit_transform(x)
print(x_.shape)
print(pca.explained_variance_)
print(pca.components_)

ICAa

import numpy
from scipy import signal
from sklearn import decomposition

time = numpy.linspace(0, 10, 2000)
x1 = numpy.sin(2*time)
x2 = numpy.sign(numpy.sin(3*time))
x3 = signal.sawtooth(2*numpy.pi*time)
x = numpy.c_[x1, x2, x3]
x += .2*numpy.random.normal(size=x.shape)
x /= x.std(axis=0)
a = numpy.array([[1, 1, 1], [0.5, 2, 1], [1.5, 1, 2]])
X = x.dot(a.T)
ica = decomposition.FastICA()
x_ = ica.fit_transform(X)
a_ = ica.mixing_.T
print(numpy.allclose(X, x_.dot(a_)+ica.mean_))

 

你可能感兴趣的:(PCA vs ICA)