Show Sidebar Hide Sidebar

# Incremental PCA in Scikit-learn

Incremental principal component analysis (IPCA) is typically used as a replacement for principal component analysis (PCA) when the dataset to be decomposed is too large to fit in memory. IPCA builds a low-rank approximation for the input data using an amount of memory which is independent of the number of input data samples. It is still dependent on the input data features, but changing the batch size allows for control of memory usage.

This example serves as a visual check that IPCA is able to find a similar projection of the data to PCA (to a sign flip), while only processing a few samples at a time. This can be considered a “toy example”, as IPCA is intended for large datasets which do not fit in main memory, requiring incremental approaches.

#### New to Plotly?¶

You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version¶

In [1]:
import sklearn
sklearn.__version__

Out[1]:
'0.18'

### Imports¶

This tutorial imports load_iris, PCA and IncrementalPCA.

In [2]:
print(__doc__)

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, IncrementalPCA

Automatically created module for IPython interactive environment


### Calculations¶

In [3]:
iris = load_iris()
X = iris.data
y = iris.target

n_components = 2
ipca = IncrementalPCA(n_components=n_components, batch_size=10)
X_ipca = ipca.fit_transform(X)

pca = PCA(n_components=n_components)
X_pca = pca.fit_transform(X)

colors = ['navy', 'turquoise', 'darkorange']

for X_transformed, title in [(X_ipca, "Incremental PCA"), (X_pca, "PCA")]:
if "Incremental" in title:
err = np.abs(np.abs(X_pca) - np.abs(X_ipca)).mean()


### Plot Results¶

In [4]:
fig = tools.make_subplots(rows=1, cols=2,
subplot_titles=("Incremental PCA of iris dataset<br>"
"Mean absolute unsigned error %.6f" % err,
"PCA of iris dataset")
)

This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]


In [5]:
col = 1
legend = True

for X_transformed, title in [(X_ipca, "Incremental PCA"), (X_pca, "PCA")]:

for color, i, target_name in zip(colors, [0, 1, 2], iris.target_names):
if(col==2):
legend=False

pca = go.Scatter(x=X_transformed[y == i, 0],
y=X_transformed[y == i, 1],
showlegend=legend,
mode='markers',
marker=dict(
color=color),
name=target_name
)
fig.append_trace(pca, 1, col)
col+=1

for i in map(str, range(1, 3)):
x = 'xaxis' + i
y = 'yaxis' + i

fig['layout'][x].update(zeroline=False, showgrid=False)
fig['layout'][y].update(zeroline=False, showgrid=False)

In [6]:
py.iplot(fig)

Out[6]:

Authors:

    Kyle Kastner



    BSD 3 clause