8000 DOC Adds an example to PatchExtractor (#12819) · thomasjpfan/scikit-learn@16a07bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 16a07bf

Browse files
CatChenalthomasjpfan
authored andcommitted
DOC Adds an example to PatchExtractor (scikit-learn#12819)
* Finalizes fix for scikit-learn#12202 from abandonned PR by @parul-l * Completes 12202 fix abandoned by @parul-l * Completes 12202 fix abandoned by @parul-l * Extends 12202 fix over feature_extraction/image.py * Closes scikit-learn#12202; white space removal * Closes scikit-learn#12202; white space removal2 * Closes scikit-learn#12202; 3.5 compliance; added >>> in docstring code. * Closes scikit-learn#12202; indentation discrep. * Closes scikit-learn#12202; indentation discrep.2 * Example output formating; @jnotham * Example output formating; forgot flake8 * Closes scikit-learn#12202; Removed excessive indentation in docstring (#wimlds) * Closes scikit-learn#12202; Fixed inconsistent indentation in docstring (#wimlds) * Closes scikit-learn#12202 (#wimlds); intentation, v3.5 compliance * Closes scikit-learn#12202 (#wimlds); Output format issue solved with addition of # doctest: +NORMALIZE_WHITESPACE +DONT_ACCEPT_BLANKLINE +ELLIPSIS for print statements. * Closes scikit-learn#12202 (#wimlds); Output format issue solved with addition of # doctest: +NORMALIZE_WHITESPACE +DONT_ACCEPT_BLANKLINE for print statements. * Closes scikit-learn#12202 (#wimlds); Testing doctest direc.: removed DONT_ACCEPT_BLANKLINE (@jnothmam, @reshamas) * Closes scikit-learn#12202 (#wimlds); Removed blank lines in doctest example.
1 parent fdd457f commit 16a07bf

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

sklearn/feature_extraction/image.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _make_edges_3d(n_x, n_y, n_z=1):
5151

5252

5353
def _compute_gradient_3d(edges, img):
54-
n_x, n_y, n_z = img.shape
54+
_, n_y, n_z = img.shape
5555
gradient = np.abs(img[edges[0] // (n_y * n_z),
5656
(edges[0] % (n_y * n_z)) // n_z,
5757
(edges[0] % (n_y * n_z)) % n_z] -
@@ -331,33 +331,33 @@ def extract_patches_2d(image, patch_size, max_patches=None, random_state=None):
331331
Returns
332332
-------
333333
patches : array, shape = (n_patches, patch_height, patch_width) or
334-
(n_patches, patch_height, patch_width, n_channels)
335-
The collection of patches extracted from the image, where `n_patches`
336-
is either `max_patches` or the total number of patches that can be
337-
extracted.
334+
(n_patches, patch_height, patch_width, n_channels)
335+
The collection of patches extracted from the image, where `n_patches`
336+
is either `max_patches` or the total number of patches that can be
337+
extracted.
338338
339339
Examples
340340
--------
341-
>>> import numpy as np
341+
>>> from sklearn.datasets import load_sample_images
342342
>>> from sklearn.feature_extraction import image
343-
>>> one_image = np.arange(16).reshape((4, 4))
344-
>>> one_image
345-
array([[ 0, 1, 2, 3],
346-
[ 4, 5, 6, 7],
347-
[ 8, 9, 10, 11],
348-
[12, 13, 14, 15]])
343+
>>> # Use the array data from the first image in this dataset:
344+
>>> one_image = load_sample_images().images[0]
345+
>>> print('Image shape: {}'.format(one_image.shape))
346+
Image shape: (427, 640, 3)
349347
>>> patches = image.extract_patches_2d(one_image, (2, 2))
350-
>>> patches.shape
351-
(9, 2, 2)
352-
>>> patches[0]
353-
array([[0, 1],
354-
[4, 5]])
355-
>>> patches[1]
356-
array([[1, 2],
357-
[5, 6]])
358-
>>> patches[8]
359-
array([[10, 11],
360-
[14, 15]])
348+
>>> print('Patches shape: {}'.format(patches.shape))
349+
Patches shape: (272214, 2, 2, 3)
350+
>>> # Here are just two of these patches:
351+
>>> print(patches[1]) # doctest: +NORMALIZE_WHITESPACE
352+
[[[174 201 231]
353+
[174 201 231]]
354+
[[173 200 230]
355+
[173 200 230]]]
356+
>>> print(patches[800])# doctest: +NORMALIZE_WHITESPACE
357+
[[[187 214 243]
358+
[188 215 244]]
359+
[[187 214 243]
360+
[188 215 244]]]
361361
"""
362362
i_h, i_w = image.shape[:2]
363363
p_h, p_w = patch_size
@@ -420,7 +420,6 @@ def reconstruct_from_patches_2d(patches, image_size):
420420
-------
421421
image : array, shape = image_size
422422
the reconstructed image
423-
424423
"""
425424
i_h, i_w = image_size[:2]
426425
p_h, p_w = patches.shape[1:3]
@@ -461,7 +460,21 @@ class PatchExtractor(BaseEstimator):
461460
If None, the random number generator is the RandomState instance used
462461
by `np.random`.
463462
463+
Examples
464+
--------
465+
>>> from sklearn.datasets import load_sample_images
466+
>>> from sklearn.feature_extraction import image
467+
>>> # Use the array data from the second image in this dataset:
468+
>>> X = load_sample_images().images[1]
469+
>>> print('Image shape: {}'.format(X.shape))
470+
Image shape: (427, 640, 3)
471+
>>> pe = image.PatchExtractor(patch_size=(2, 2))
472+
>>> pe_fit = pe.fit(X)
473+
>>> pe_trans = pe.transform(X)
474+
>>> print('Patches shape: {}'.format(pe_trans.shape))
475+
Patches shape: (545706, 2, 2)
464476
"""
477+
465478
def __init__(self, patch_size=None, max_patches=None, random_state=None):
466479
self.patch_size = patch_size
467480
self.max_patches = max_patches
@@ -498,7 +511,6 @@ def transform(self, X):
498511
The collection of patches extracted from the images, where
499512
`n_patches` is either `n_samples * max_patches` or the total
500513
number of patches that can be extracted.
501-
502514
"""
503515
self.random_state = check_random_state(self.random_state)
504516
n_images, i_h, i_w = X.shape[:3]

0 commit comments

Comments
 (0)
0