<jupyter_start><jupyter_text>An example of estimating sources from noisy data.[Independent component analysis (ICA)](http://scikit-learn.org/stable/modules/decomposition.htmlica) is used to estimate sources given noisy measurements. Imagine 3 instruments playing simultaneously and 3 microphones recording the mixed signals. ICA is used to recover the sources ie. what is played by each instrument. Importantly, PCA fails at recovering our instruments since the related signals reflect non-Gaussian processes.  New to Plotly?Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).You can set up Plotly to work in [online](https://plot.ly/python/getting-started/initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/start-plotting-online).We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!  Version<jupyter_code>import sklearn
sklearn.__version__<jupyter_output><empty_output><jupyter_text>Imports This tutorial imports [FastICA](http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.FastICA.htmlsklearn.decomposition.FastICA) and [PCA](http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.htmlsklearn.decomposition.PCA).<jupyter_code>print(__doc__)

import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import numpy as np
from scipy import signal

from sklearn.decomposition import FastICA, PCA<jupyter_output>Automatically created module for IPython interactive environment<jupyter_text>Calculations Generate sample data<jupyter_code>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

# Compute ICA
ica = FastICA(n_components=3)
S_ = ica.fit_transform(X)  # Reconstruct signals
A_ = ica.mixing_  # Get estimated mixing matrix

# We can `prove` that the ICA model applies by reverting the unmixing.
assert np.allclose(X, np.dot(S_, A_.T) + ica.mean_)

# For comparison, compute PCA
pca = PCA(n_components=3)
H = pca.fit_transform(X)  # Reconstruct signals based on orthogonal components<jupyter_output><empty_output><jupyter_text>Plot Results<jupyter_code>models = [X, S, S_, H]
names = ('Observations (mixed signal)',
         'True Sources',
         'ICA recovered signals',
         'PCA recovered signals')
colors = ['red', 'steelblue', 'orange']

fig = tools.make_subplots(rows=4, cols=1,
                          subplot_titles=names,
                          print_grid=False)
row = 1
for ii, (model, name) in enumerate(zip(models, names), 1):
    for sig, color in zip(model.T, colors):
        trace = go.Scatter(y=sig, 
                           showlegend=False,
                           line=dict(color=color)
                           )
        fig.append_trace(trace, row, 1)

    row+=1
    
fig['layout'].update(height=800)

py.iplot(fig)

from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'blind-source-separation-using-FastICA.ipynb', 'scikit-learn/plot-ica-blind-source-separation/', 'Blind source separation using FastICA | plotly',
    ' ',
    title = 'Blind source separation using FastICA| plotly',
    name = 'Blind source separation using FastICA',
    has_thumbnail='true', thumbnail='thumbnail/blind-source.jpg', 
    language='scikit-learn', page_type='example_index',
    display_as='decomposition', order=4,
    ipynb= '~Diksha_Gabha/2924')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12516614975631368, 'fraction_numerical': 0.01595037660611431, 'mean_word_length': 4.375, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '359257', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1448, 'n_words': 355, 'path': '_posts/scikit/blind-source-separation-using-FastICA/blind-source-separation-using-FastICA.ipynb', 'repo_name': 'bmb804/documentation', 'size': '4514'}