PatchExtractor#
- class sklearn.feature_extraction.image.PatchExtractor(*, patch_size=None, max_patches=None, random_state=None)[源代码]#
从图像集合中提取补丁。
阅读更多的 User Guide .
Added in version 0.9.
- 参数:
- patch_sizeint(patch_height,patch_root)的多元组,默认=无
一个补丁的尺寸。如果设置为无,补丁大小将自动设置为
(img_height // 10, img_width // 10)
,在哪里img_height
和img_width
是输入图像的尺寸。- max_patchesint或float,默认=无
每张图像要提取的最大补丁数。如果
max_patches
是(0,1)中的浮点数,它表示面片总数的一部分。如果设置为“无”,则提取所有可能的面片。- random_stateint,RandomState实例,默认=无
确定用于随机采样的随机数生成器
max_patches is not None
.使用int使随机性具有确定性。看到 Glossary .
参见
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)[源代码]#
仅验证估计量的参数。
该方法允许:(i)验证估计器的参数,以及(ii)与scikit-learn Transformer API一致。
- 参数:
- X形状的nd数组(n_samples、Image_height、Image_宽度)或 (n_samples、图像_height、图像_宽度、n_channels)
从中提取补丁的图像阵列。对于彩色图像,最后一个维度指定通道:GB图像将具有
n_channels=3
.- y忽视
未使用,按照惯例,为了API一致性而存在。
- 返回:
- self对象
返回实例本身。
- fit_transform(X, y=None, **fit_params)[源代码]#
适应数据,然后对其进行转换。
适合变压器
X
和y
具有可选参数fit_params
并返回的转换版本X
.- 参数:
- X形状类似阵列(n_samples,n_features)
输入样本。
- y形状为(n_samples,)或(n_samples,n_outputs)的阵列状, 默认值=无
目标值(无监督转换)。
- **fit_paramsdict
其他适合参数。
- 返回:
- X_newndray形状数组(n_samples,n_features_new)
变形的数组。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- set_output(*, transform=None)[源代码]#
设置输出容器。
看到 介绍 set_output API 了解如何使用API的示例。
- 参数:
- transform{“默认”,“pandas”,“polars”},默认=无
配置输出
transform
和fit_transform
."default"
:Transformer的默认输出格式"pandas"
:DataFrame输出"polars"
:两极输出None
:转换配置不变
Added in version 1.4:
"polars"
添加了选项。
- 返回:
- self估计器实例
估计实例。
- set_params(**params)[源代码]#
设置此估计器的参数。
该方法适用于简单估计器以及嵌套对象(例如
Pipeline
).后者具有以下形式的参数<component>__<parameter>
以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计参数。
- 返回:
- self估计器实例
估计实例。
- transform(X)[源代码]#
将图像样本转换为
X
转换为补丁数据矩阵。- 参数:
- X形状的nd数组(n_samples、Image_height、Image_宽度)或 (n_samples、图像_height、图像_宽度、n_channels)
从中提取补丁的图像阵列。对于彩色图像,最后一个维度指定通道:GB图像将具有
n_channels=3
.
- 返回:
- patches形状数组(n_patches、patch_height、patch_宽度)或 (n_patches、patches_height、patch_宽度、n_channels)
从图像中提取的补丁集合,其中
n_patches
要么是n_samples * max_patches
或可以提取的补丁总数。