Skip to content

Commit

Permalink
Merge pull request #2 from agramfort/sparsepca
Browse files Browse the repository at this point in the history
Sparse PCA
  • Loading branch information
vene committed Jun 15, 2011
2 parents c0187b9 + d124bde commit 6b0b36d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
34 changes: 19 additions & 15 deletions examples/decomposition/plot_sparse_pca.py
Expand Up @@ -7,33 +7,37 @@
the digits dataset containing only the digit 3.
"""


print __doc__

# Authors: Vlad Niculae, Alexandre Gramfort
# License: BSD

import numpy as np
import matplotlib.pyplot as plt
import pylab as pl

from scikits.learn.decomposition import SparsePCA
from scikits.learn.datasets import load_digits

###############################################################################
# Load data and fit the model
rows, cols = 4, 3
digits = load_digits()
threes = digits.data[digits.target == 3]
threes -= threes.mean(axis=0) # todo: use preprocessors
# low tolerance for high speed
model = SparsePCA(n_components=rows * cols, tol=1e-3)
threes -= threes.mean(axis=0) # XXX: use preprocessors
model = SparsePCA(n_components=rows * cols, alpha=5)
model.fit(threes)
span = np.max(np.abs(model.components_))

fig = plt.figure(figsize=(1.2 * cols, 1.4 * rows))
###############################################################################
# Plot sparse components
fig = pl.figure(figsize=(1.2 * cols, 1.4 * rows))
for i, comp in enumerate(model.components_):
plt.subplot(rows, cols, i + 1)
plt.imshow(np.reshape(comp, (8, 8)), interpolation='nearest',
vmin=-span, vmax=span, cmap=plt.cm.PuOr)
plt.xticks(())
plt.yticks(())
plt.subplots_adjust(0.01, 0.15, 0.99, 0.99, 0.04, 0.)
pl.subplot(rows, cols, i + 1)
pl.imshow(np.reshape(comp, (8, 8)), interpolation='nearest',
vmin=-span, vmax=span, cmap=pl.cm.PuOr)
pl.xticks(())
pl.yticks(())
pl.subplots_adjust(0.01, 0.15, 0.99, 0.99, 0.04, 0.)
cax = fig.add_axes([0.1, 0.06, 0.8, 0.04])
plt.colorbar(cax=cax, orientation='horizontal')
plt.show()
pl.colorbar(cax=cax, orientation='horizontal')
pl.show()
5 changes: 5 additions & 0 deletions scikits/learn/decomposition/sparse_pca.py
@@ -1,3 +1,8 @@
""" Matrix factorization with Sparse PCA
"""
# Author: Vlad Niculae, Gael Varoquaux, Alexandre Gramfort
# License: BSD

import time
import sys
from math import sqrt
Expand Down
3 changes: 3 additions & 0 deletions scikits/learn/decomposition/tests/test_sparse_pca.py
@@ -1,3 +1,6 @@
# Author: Vlad Niculae
# License: BSD

import numpy as np
from .. import SparsePCA
from numpy.testing import assert_array_almost_equal, assert_equal
Expand Down

0 comments on commit 6b0b36d

Please sign in to comment.