def ica(X): """ Wrapper function for Independent Component Analysis of scikits.learn.decomposition """ from scikits.learn.decomposition import FastICA model = FastICA() model.fit(X.T) Y = model.transform(X.T).T return Y / Y.std(0)
from scikits.learn.decomposition import PCA, FastICA ############################################################################### # Generate sample data S = np.random.standard_t(1.5, size=(2, 10000)) S[0] *= 2. # Mix data A = [[1, 1], [0, 2]] # Mixing matrix X = np.dot(A, S) # Generate observations pca = PCA() S_pca_ = pca.fit(X.T).transform(X.T).T ica = FastICA() S_ica_ = ica.fit(X).transform(X) # Estimate the sources S_ica_ /= S_ica_.std(axis=1)[:, np.newaxis] ############################################################################### # Plot results def plot_samples(S, axis_list=None): pl.scatter(S[0], S[1], s=2, marker='o', linewidths=0, zorder=10) if axis_list is not None: colors = [(0, 0.6, 0), (0.6, 0, 0)] for color, axis in zip(colors, axis_list): axis /= axis.std() x_axis, y_axis = axis
def run_ica(data, ncomponents): logging.debug("running ica: %i" % ncomponents) ica = FastICA(ncomponents) ica.fit(data) return ica
from scikits.learn.decomposition import PCA, FastICA ############################################################################### # Generate sample data S = np.random.standard_t(1.5, size=(10000, 2)) S[0] *= 2. # Mix data A = np.array([[1, 1], [0, 2]]) # Mixing matrix X = np.dot(S, A.T) # Generate observations pca = PCA() S_pca_ = pca.fit(X).transform(X) ica = FastICA() S_ica_ = ica.fit(X).transform(X) # Estimate the sources S_ica_ /= S_ica_.std(axis=0) ############################################################################### # Plot results def plot_samples(S, axis_list=None): pl.scatter(S[:,0], S[:,1], s=2, marker='o', linewidths=0, zorder=10) if axis_list is not None: colors = [(0, 0.6, 0), (0.6, 0, 0)] for color, axis in zip(colors, axis_list): axis /= axis.std() x_axis, y_axis = axis
############################################################################### # Generate sample data np.random.seed(0) n_samples = 2000 time = np.linspace(0, 10, n_samples) s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal S = np.c_[s1, s2].T S += 0.2 * np.random.normal(size=S.shape) # Add noise S /= S.std(axis=1)[:, np.newaxis] # Standardize data # Mix data A = [[1, 1], [0.5, 2]] # Mixing matrix X = np.dot(A, S) # Generate observations # Compute ICA ica = FastICA() S_ = ica.fit(X).transform(X) # Get the estimated sources A_ = ica.get_mixing_matrix() # Get estimated mixing matrix assert np.allclose(X, np.dot(A_, S_)) ############################################################################### # Plot results pl.figure() pl.subplot(3, 1, 1) pl.plot(S.T) pl.title('True Sources') pl.subplot(3, 1, 2) pl.plot(X.T) pl.title('Observations (mixed signal)') pl.subplot(3, 1, 3)
############################################################################### # Generate sample data np.random.seed(0) n_samples = 2000 time = np.linspace(0, 10, n_samples) s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal S = np.c_[s1, s2] 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], [0.5, 2]]) # Mixing matrix X = np.dot(S, A.T) # Generate observations # Compute ICA ica = FastICA() S_ = ica.fit(X).transform(X) # Get the estimated sources A_ = ica.get_mixing_matrix() # Get estimated mixing matrix assert np.allclose(X, np.dot(S_, A_.T)) ############################################################################### # Plot results pl.figure() pl.subplot(3, 1, 1) pl.plot(S) pl.title('True Sources') pl.subplot(3, 1, 2) pl.plot(X) pl.title('Observations (mixed signal)') pl.subplot(3, 1, 3) pl.plot(S_)