Numpy 的 clip(~)
方法用於確保輸入數組的值位於特定範圍之間。
參數
1. a
| array_like
輸入數組。
2. a_min
| scalar
或 array_like
或 None
要剪輯的下限。小於 a_min
的值將被 a_min
替換。您可以通過設置 None
忽略下限。
3. a_max
| scalar
或 array_like
或 None
要剪輯的上限。大於 a_max
的值將被 a_max
替換。您可以通過設置 None
來忽略上限。
返回值
一個 Numpy 數組,其中輸入數組的值根據您的參數進行裁剪。
例子
指定下限和上限
x = np.array([1,2,3,4,5])
np.clip(x, 2, 4)
array([2, 2, 3, 4, 4])
請注意值 1
被剪切為 2
,而值 5
被剪切為 4
。
未指定下限
設置第二個參數None
:
x = np.array([1,2,3,4,5])
np.clip(x, None, 4)
array([1, 2, 3, 4, 4])
未指定上限
將第三個參數設置為None
:
x = np.array([1,2,3,4,5])
np.clip(x, 2, None)
array([2, 2, 3, 4, 5])
裁剪二維數組
裁剪多維數組有點棘手。考慮以下示例:
x = np.array([[1,2],[3,4],[5,6]])
np.clip(x, [2,3], [4,5])
array([[2, 3],
[3, 4],
[4, 5]])
在這裏,第一行 [1,2]
被下限 [2,3]
剪裁 - 剪裁是按元素完成的,因此從 1<2
和 2<3
開始,兩個值都被剪裁為 [2,3]
。
相關用法
- Python clx.ip.is_ip用法及代碼示例
- Python clx.analytics.anomaly_detection.dbscan用法及代碼示例
- Python clx.ip.hostmask用法及代碼示例
- Python classmethod用法及代碼示例
- Python clx.osi.virus_total.VirusTotalClient.file_rescan用法及代碼示例
- Python clx.osi.virus_total.VirusTotalClient.url_report用法及代碼示例
- Python clx.ip.is_global用法及代碼示例
- Python clx.osi.virus_total.VirusTotalClient.ipaddress_report用法及代碼示例
- Python clx.ip.ip_to_int用法及代碼示例
- Python clx.osi.virus_total.VirusTotalClient.file_scan用法及代碼示例
- Python clx.osi.virus_total.VirusTotalClient.scan_big_file用法及代碼示例
- Python clx.analytics.dga_detector.DGADetector.predict用法及代碼示例
- Python clx.ip.is_private用法及代碼示例
- Python clx.osi.slashnext.SlashNextClient.host_reputation用法及代碼示例
- Python clx.analytics.asset_classification.AssetClassification.predict用法及代碼示例
- Python clx.eda.EDA用法及代碼示例
- Python clx.analytics.loda.Loda.score用法及代碼示例
- Python classmethod()用法及代碼示例
- Python clx.osi.slashnext.SlashNextClient.api_quota用法及代碼示例
- Python clx.ip.is_reserved用法及代碼示例
- Python clx.dns.dns_extractor.generate_tld_cols用法及代碼示例
- Python clx.ip.is_unspecified用法及代碼示例
- Python clx.ip.is_loopback用法及代碼示例
- Python clx.analytics.dga_detector.DGADetector.evaluate_model用法及代碼示例
- Python clx.osi.virus_total.VirusTotalClient.domain_report用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | clip method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。