PatchExtractor#
- class sklearn.feature_extraction.image.PatchExtractor(*, patch_size=None, max_patches=None, random_state=None)[原始碼]#
從一系列影像中提取圖塊。
更多資訊請參閱使用者指南。
於 0.9 版本加入。
- 參數:
- patch_sizetuple of int (patch_height, patch_width),預設為 None
單個圖塊的尺寸。如果設定為 None,則圖塊大小會自動設定為
(img_height // 10, img_width // 10)
,其中img_height
和img_width
是輸入影像的尺寸。- max_patchesint 或 float,預設為 None
每個影像要提取的最大圖塊數。如果
max_patches
是 (0, 1) 中的 float,則表示佔總圖塊數的比例。如果設定為 None,則提取所有可能的圖塊。- random_stateint、RandomState 實例,預設為 None
決定當
max_patches 不是 None
時,用於隨機取樣的隨機數產生器。使用整數可使隨機性具有決定性。請參閱詞彙表。
另請參閱
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 轉換器 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)[原始碼]#
將資料擬合,然後轉換它。
使用選用的參數
fit_params
將轉換器擬合到X
和y
,並傳回X
的轉換版本。- 參數:
- X形狀為 (n_samples, n_features) 的類陣列
輸入樣本。
- y形狀為 (n_samples,) 或 (n_samples, n_outputs) 的類陣列,預設為 None
目標值(對於無監督轉換為 None)。
- **fit_paramsdict
其他擬合參數。
- 傳回:
- X_new形狀為 (n_samples, n_features_new) 的 ndarray 陣列
轉換後的陣列。
- get_metadata_routing()[原始碼]#
取得此物件的中繼資料路由。
請檢查使用者指南,瞭解路由機制如何運作。
- 傳回:
- routingMetadataRequest
封裝路由資訊的
MetadataRequest
。
- get_params(deep=True)[原始碼]#
取得此估算器的參數。
- 參數:
- deepbool,預設為 True
如果為 True,將傳回此估算器和所包含的子物件(即估算器)的參數。
- 傳回:
- paramsdict
參數名稱對應到其值。
- set_output(*, transform=None)[原始碼]#
設定輸出容器。
請參閱介紹 set_output API,瞭解如何使用 API 的範例。
- 參數:
- transform{"default", "pandas", "polars"},預設為 None
設定
transform
和fit_transform
的輸出。"default"
:轉換器的預設輸出格式"pandas"
:DataFrame 輸出"polars"
:Polars 輸出None
:轉換設定未變更
在 1.4 版本中新增: 新增了
"polars"
選項。
- 傳回:
- self估計器實例
估計器實例。
- set_params(**params)[原始碼]#
設定此估計器的參數。
此方法適用於簡單的估計器以及巢狀物件(例如
Pipeline
)。後者具有<元件>__<參數>
形式的參數,因此可以更新巢狀物件的每個元件。- 參數:
- **paramsdict
估計器參數。
- 傳回:
- self估計器實例
估計器實例。
- transform(X)[原始碼]#
將
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
或可提取的圖塊總數。