PatchExtractor#

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

从图像集合中提取图像块。

用户指南中阅读更多内容。

0.9 版本新增。

参数:
patch_sizeint 元组 (patch_height, patch_width),默认=None

单个图像块的尺寸。如果设置为 None,图像块尺寸将自动设置为 (img_height // 10, img_width // 10),其中 img_heightimg_width 是输入图像的尺寸。

max_patchesint 或 float,默认=None

每张图像要提取的最大图像块数量。如果 max_patches 是 (0, 1) 范围内的浮点数,则表示占总图像块数量的比例。如果设置为 None,则提取所有可能的图像块。

random_stateint,RandomState 实例,默认=None

max_patches 不为 None 时,决定随机采样所使用的随机数生成器。使用 int 类型可使随机性确定。请参阅 术语表

另请参见

reconstruct_from_patches_2d

从所有图像块重建图像。

注意事项

该估计器是无状态的,不需要拟合。然而,我们建议调用 fit_transform 而不是 transform,因为参数验证只在 fit 中执行。

示例

>>> 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]
>>> X = X[None, ...]
>>> print(f"Image shape: {X.shape}")
Image shape: (1, 427, 640, 3)
>>> pe = image.PatchExtractor(patch_size=(10, 10))
>>> pe_trans = pe.transform(X)
>>> print(f"Patches shape: {pe_trans.shape}")
Patches shape: (263758, 10, 10, 3)
>>> X_reconstructed = image.reconstruct_from_patches_2d(pe_trans, X.shape[1:])
>>> print(f"Reconstructed shape: {X_reconstructed.shape}")
Reconstructed shape: (427, 640, 3)
fit(X, y=None)[source]#

仅验证估计器的参数。

此方法允许:(i) 验证估计器的参数,以及 (ii) 与 scikit-learn 转换器 API 保持一致。

参数:
X形状为 (n_samples, image_height, image_width) 或 (n_samples, image_height, image_width, n_channels) 的 ndarray

从中提取图像块的图像数组。对于彩色图像,最后一个维度指定通道:一个 RGB 图像将具有 n_channels=3

y忽略

未使用,根据约定为保持 API 一致性而存在。

返回:
self对象

返回实例本身。

fit_transform(X, y=None, **fit_params)[source]#

拟合数据,然后进行转换。

使用可选参数 fit_params 将转换器拟合到 Xy,并返回 X 的转换版本。

参数:
X形状为 (n_samples, n_features) 的类数组

输入样本。

y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组,默认=None

目标值(无监督转换时为 None)。

**fit_params字典

附加的拟合参数。

返回:
X_new形状为 (n_samples, n_features_new) 的 ndarray 数组

转换后的数组。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查看用户指南,了解路由机制的工作原理。

返回:
routingMetadataRequest

一个封装了路由信息的 MetadataRequest

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deep布尔值,默认=True

如果为 True,将返回此估计器及其包含的作为估计器的子对象的参数。

返回:
params字典

参数名称及其对应的值。

set_output(*, transform=None)[source]#

设置输出容器。

请参阅引入 set_output API 以获取如何使用该 API 的示例。

参数:
transform{“default”, “pandas”, “polars”},默认=None

配置 transformfit_transform 的输出。

  • "default": 转换器的默认输出格式

  • "pandas": DataFrame 输出

  • "polars": Polars 输出

  • None: 转换配置未更改

1.4 版本新增: 添加了 "polars" 选项。

返回:
self估计器实例

估计器实例。

set_params(**params)[source]#

设置此估计器的参数。

此方法适用于简单估计器以及嵌套对象(例如 Pipeline)。后者具有 <component>__<parameter> 形式的参数,因此可以更新嵌套对象的每个组件。

参数:
**params字典

估计器参数。

返回:
self估计器实例

估计器实例。

transform(X)[source]#

X 中的图像样本转换为图像块数据矩阵。

参数:
X形状为 (n_samples, image_height, image_width) 或 (n_samples, image_height, image_width, n_channels) 的 ndarray

从中提取图像块的图像数组。对于彩色图像,最后一个维度指定通道:一个 RGB 图像将具有 n_channels=3

返回:
patches形状为 (n_patches, patch_height, patch_width) 或 (n_patches, patch_height, patch_width, n_channels) 的数组

从图像中提取的图像块集合,其中 n_patches 要么是 n_samples * max_patches,要么是可以提取的图像块总数。