<jupyter_start><jupyter_text>This example shows that Kernel PCA is able to find a projection of the data that makes data linearly separable.  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 [PCA](http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.htmlsklearn.decomposition.PCA), [KernelPCA](http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.KernelPCA.htmlsklearn.decomposition.KernelPCA) and [make_circles](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_circles.htmlsklearn.datasets.make_circles).<jupyter_code>import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import numpy as np
import matplotlib.pyplot as plt

from sklearn.decomposition import PCA, KernelPCA
from sklearn.datasets import make_circles<jupyter_output><empty_output><jupyter_text>Calculations<jupyter_code>np.random.seed(0)

X, y = make_circles(n_samples=400, factor=.3, noise=.05)

kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=10)
X_kpca = kpca.fit_transform(X)
X_back = kpca.inverse_transform(X_kpca)
pca = PCA()
X_pca = pca.fit_transform(X)<jupyter_output><empty_output><jupyter_text>Plot Results<jupyter_code>fig = tools.make_subplots(rows=2, cols=2,
                          subplot_titles=("Original space",
                                          "Projection by PCA",
                                          "Projection by KPCA",
                                          "Original space after inverse transform"))
reds = y == 0
blues = y == 1

original_space1 = go.Scatter(x=X[reds, 0], 
                             y=X[reds, 1],
                             mode='markers',
                             showlegend=False,
                             marker=dict(color='red',
                                         line=dict(color='black', width=1))
                            )
original_space2 = go.Scatter(x=X[blues, 0],
                             y=X[blues, 1],
                             mode='markers',
                             showlegend=False,
                             marker=dict(color='blue',
                                         line=dict(color='black', width=1))
                             
                            )

X1, X2 = np.linspace(-1.5, 1.5, 50), np.linspace(-1.5, 1.5, 50)
X_grid = np.array([np.ravel(X1), np.ravel(X2)]).T

# projection on the first principal component (in the phi space)
Z_grid = kpca.transform(X_grid)[:, 0].reshape(X1.shape)
lines = go.Contour(x=X1, 
                   y=X2, 
                   z=Z_grid, 
                   showscale=False,
                   colorscale=[[0,'white'],[1, 'black']],
                   contours=dict(coloring='lines')
                  )

fig.append_trace(lines, 1, 1)
fig.append_trace(original_space1, 1, 1)
fig.append_trace(original_space2, 1, 1)

fig['layout']['xaxis1'].update(title='x<sub>1</sub>',
                               zeroline=False, showgrid=False)
fig['layout']['yaxis1'].update(title='x<sub>2</sub>',
                               zeroline=False, showgrid=False)


projection_pca1 = go.Scatter(x=X_pca[reds, 0], 
                             y=X_pca[reds, 1], 
                             mode='markers',
                             showlegend=False,
                             marker=dict(color='red',
                                         line=dict(color='black', width=1)) 
                            )
projection_pca2 = go.Scatter(x=X_pca[blues, 0], 
                             y=X_pca[blues, 1], 
                             mode='markers',
                             showlegend=False,
                             marker=dict(color='blue',
                                         line=dict(color='black', width=1))
                            )

fig.append_trace(projection_pca1, 1, 2)
fig.append_trace(projection_pca2, 1, 2)

fig['layout']['xaxis2'].update(title='1st principal component',
                               zeroline=False, showgrid=False)
fig['layout']['yaxis2'].update(title='2nd component',
                               zeroline=False, showgrid=False)

projection_kpca1 = go.Scatter(x=X_kpca[reds, 0], 
                              y=X_kpca[reds, 1],
                              mode='markers',
                              showlegend=False,
                              marker=dict(color='red',
                                          line=dict(color='black', width=1)) 
                             )
projection_kpca2 = go.Scatter(x=X_kpca[blues, 0], 
                              y=X_kpca[blues, 1], 
                              mode='markers',
                              showlegend=False,
                              marker=dict(color='blue',
                                          line=dict(color='black', width=1))  
                             )

fig.append_trace(projection_kpca1, 2, 1)
fig.append_trace(projection_kpca2, 2, 1)

fig['layout']['xaxis3'].update(title="1st principal component in space induced by Phi",
                               zeroline=False, showgrid=False)
fig['layout']['yaxis3'].update(title='2nd component',
                               zeroline=False, showgrid=False)

after_inverse1 = go.Scatter(x=X_back[reds, 0], 
                            y=X_back[reds, 1], 
                            mode='markers',
                            showlegend=False,
                            marker=dict(color='red',
                                        line=dict(color='black', width=1))  
                           )
after_inverse2 = go.Scatter(x=X_back[blues, 0],
                            y=X_back[blues, 1], 
                            mode='markers',
                            showlegend=False,
                            marker=dict(color='blue',
                                        line=dict(color='black', width=1))  
                           )

fig.append_trace(after_inverse1, 2, 2)
fig.append_trace(after_inverse2, 2, 2)

fig['layout']['xaxis4'].update(title='x<sub>1</sub>',
                               zeroline=False, showgrid=False)
fig['layout']['yaxis4'].update(title='x<sub>2</sub>',
                               zeroline=False, showgrid=False)

fig['layout'].update(height=800)
py.iplot(fig)<jupyter_output><empty_output><jupyter_text>License Authors:               <NAME>                    <NAME>        License:               BSD 3 clause<jupyter_code>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(
    'Kernel-PCA.ipynb', 'scikit-learn/plot-kernel-pca/', 'Kernel PCA | plotly',
    ' ',
    title = 'Kernel PCA | plotly',
    name = 'Kernel PCA',
    has_thumbnail='true', thumbnail='thumbnail/kernel-pca.jpg', 
    language='scikit-learn', page_type='example_index',
    display_as='decomposition', order=4,
    ipynb= '~Diksha_Gabha/2927')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.01706827309236948, 'mean_word_length': 1.88731884057971, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '521091', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2334, 'n_words': 365, 'path': '_posts/scikit/kernel-pca/Kernel-PCA.ipynb', 'repo_name': 'bmb804/documentation', 'size': '7986'}