class sklearn.feature_extraction.image.PatchExtractor(*, patch_size=None, max_patches=None, random_state=None)[source]

Extracts patches from a collection of images

Read more in the User Guide.

New in version 0.9.

patch_sizetuple of int (patch_height, patch_width)

The dimensions of one patch.

max_patchesint or float, default=None

The maximum number of patches per image to extract. If max_patches is a float in (0, 1), it is taken to mean a proportion of the total number of patches.

random_stateint, RandomState instance, default=None

Determines the random number generator used for random sampling when max_patches is not None. Use an int to make the randomness deterministic. See Glossary.


>>> from sklearn.datasets import load_sample_images
>>> from sklearn.feature_extraction import image
>>> # Use the array data from the second image in this dataset:
>>> X = load_sample_images().images[1]
>>> print('Image shape: {}'.format(X.shape))
Image shape: (427, 640, 3)
>>> pe = image.PatchExtractor(patch_size=(2, 2))
>>> pe_fit =
>>> pe_trans = pe.transform(X)
>>> print('Patches shape: {}'.format(pe_trans.shape))
Patches shape: (545706, 2, 2)


fit(self, X[, y])

Do nothing and return the estimator unchanged.

get_params(self[, deep])

Get parameters for this estimator.

set_params(self, \*\*params)

Set the parameters of this estimator.

transform(self, X)

Transforms the image samples in X into a matrix of patch data.

__init__(self, *, patch_size=None, max_patches=None, random_state=None)[source]

Initialize self. See help(type(self)) for accurate signature.

fit(self, X, y=None)[source]

Do nothing and return the estimator unchanged.

This method is just there to implement the usual API and hence work in pipelines.

Xarray-like of shape (n_samples, n_features)

Training data.

get_params(self, deep=True)[source]

Get parameters for this estimator.

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

paramsmapping of string to any

Parameter names mapped to their values.

set_params(self, **params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.


Estimator parameters.


Estimator instance.

transform(self, X)[source]

Transforms the image samples in X into a matrix of patch data.

Xndarray of shape (n_samples, image_height, image_width) or (n_samples, image_height, image_width, n_channels)

Array of images from which to extract patches. For color images, the last dimension specifies the channel: a RGB image would have n_channels=3.

patchesarray of shape (n_patches, patch_height, patch_width) or (n_patches, patch_height, patch_width, n_channels)

The collection of patches extracted from the images, where n_patches is either n_samples * max_patches or the total number of patches that can be extracted.