Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Sc #5

Merged
merged 4 commits into from
This page is out of date. Refresh to see the latest.
View
4 scikits/learn/decomposition/dict_learning.py
@@ -8,6 +8,7 @@
from .sparse_pca import dict_learning, sparse_pca, _update_V_parallel
from ..base import BaseEstimator, TransformerMixin
+from ..linear_model import orthogonal_mp
class DictionaryLearning(BaseEstimator, TransformerMixin):
@@ -19,7 +20,7 @@ class DictionaryLearning(BaseEstimator, TransformerMixin):
Solves the optimization problem:
(U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1
(U,V)
- with || V_k ||_2 = 1 for all 0<= k < n_atoms
+ with || V_k ||_2 = 1 for all 0 <= k < n_atoms
Parameters
----------
@@ -130,6 +131,7 @@ def transform(self, X, y=None, method='omp', **kwargs):
X_new array, shape (n_samples, n_components)
Transformed data
"""
+ # XXX : kwargs is not documented
# XXX: parameters should be made explicit so we can have defaults
if method == 'omp':
View
45 scikits/learn/feature_extraction/image.py
@@ -6,13 +6,11 @@
# Gael Varoquaux <gael.varoquaux@normalesup.org>
# License: BSD
-
-from itertools import product
-
import numpy as np
from scipy import sparse
from ..utils.fixes import in1d
from ..utils import check_random_state
+from ..utils.fixes import product
from ..base import BaseEstimator
@@ -205,23 +203,18 @@ def extract_patches_2d(image, patch_size, max_patches=None, seed=None):
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
-
- >>> patches = extract_patches_2d(one_image, (4, 4), (2, 2))
+ >>> patches = extract_patches_2d(one_image, (2, 2))
>>> patches.shape
(9, 2, 2)
-
>>> patches[0]
array([[0, 1],
[4, 5]])
-
>>> patches[1]
array([[1, 2],
[5, 6]])
-
>>> patches[8]
array([[10, 11],
[14, 15]])
-
"""
i_h, i_w = image.shape[:2]
p_h, p_w = patch_size
@@ -245,19 +238,16 @@ def extract_patches_2d(image, patch_size, max_patches=None, seed=None):
raise ValueError("Invalid value for max_patches!")
rng = check_random_state(seed)
- patches = np.zeros((n_patches, p_h, p_w, n_colors), dtype=image.dtype)
+ patches = np.empty((n_patches, p_h, p_w, n_colors), dtype=image.dtype)
i_s = rng.randint(n_h, size=n_patches)
j_s = rng.randint(n_w, size=n_patches)
- for offset, (i, j) in enumerate(zip(i_s, j_s)):
- patches[offset] = image[i:i + p_h, j:j + p_w, :]
+ for p, i, j in zip(patches, i_s, j_s):
+ p[:] = image[i:i + p_h, j:j + p_w, :]
else:
n_patches = all_patches
- patches = np.zeros((n_patches, p_h, p_w, n_colors), dtype=image.dtype)
- offset = 0
- for i in xrange(n_h):
- for j in xrange(n_w):
- patches[offset] = image[i:i + p_h, j:j + p_w, :]
- offset += 1
+ patches = np.empty((n_patches, p_h, p_w, n_colors), dtype=image.dtype)
+ for p, (i, j) in zip(patches, product(xrange(n_h), xrange(n_w))):
+ p[:] = image[i:i + p_h, j:j + p_w, :]
# remove the color dimension if useless
if patches.shape[-1] == 1:
@@ -284,7 +274,7 @@ def reconstruct_from_patches_2d(patches, image_size):
Returns
-------
- image: array with shape
+ image: array with shape
"""
@@ -294,8 +284,9 @@ def reconstruct_from_patches_2d(patches, image_size):
# compute the dimensions of the patches array
n_h = i_h - p_h + 1
n_w = i_w - p_w + 1
- for offset, (i, j) in enumerate(product(xrange(n_h), xrange(n_w))):
- img[i:i + p_h, j:j + p_w] += patches[offset]
+ for p, (i, j) in zip(patches, product(xrange(n_h), xrange(n_w))):
+ img[i:i + p_h, j:j + p_w] += p
+
for i in xrange(i_h):
for j in xrange(i_w):
# divide by the amount of overlap
@@ -321,19 +312,29 @@ class PatchExtractor(BaseEstimator):
seed: int or RandomState
Seed for the random number generator used in case max_patches is used.
"""
-
def __init__(self, patch_size, max_patches=None, seed=None):
self.patch_size = patch_size
self.max_patches = max_patches
self.seed = seed
def fit(self, X, y=None):
+ """
+
+ XXX : docstring is missing
+
+ """
return self
def transform(self, X):
+ """
+
+ XXX : docstring is missing
+
+ """
patches = np.empty((0,) + self.patch_size)
for image in X:
partial_patches = extract_patches_2d(image, self.patch_size,
self.max_patches, self.seed)
+ # XXX : would be better to avoid a realloc for each image
patches = np.r_[patches, partial_patches]
return patches
View
2  scikits/learn/feature_extraction/tests/test_image.py
@@ -128,12 +128,14 @@ def test_reconstruct_patches_perfect_color():
lena_reconstructed = reconstruct_from_patches_2d(patches, lena.shape)
np.testing.assert_array_equal(lena, lena_reconstructed)
+
def test_patch_extractor_max_patches():
lenas = _make_images()
extr = PatchExtractor(patch_size=(8, 8), max_patches=100, seed=0)
patches = extr.transform(lenas)
assert patches.shape == (len(lenas) * 100, 8, 8)
+
def test_patch_extractor_all_patches():
lenas = _make_images()
i_h, i_w = lenas.shape[1:3]
Something went wrong with that request. Please try again.