NumPy 的 argwhere(~)
方法返回非零值的索引。
參數
1. a
| array_like
輸入數組。
返回值
非零值索引的 NumPy 數組。如果輸入數組是一維或二維,則返回的數組將為二維。
例子
基本用法
x = np.array([1,2,3])
np.argwhere(x)
array([[0],
[1],
[2]])
此處,由於 x 中的所有值均非零,因此返回所有索引。請注意,返回的數組是二維的。
使用麵罩
在 NumPy 中,False
值相當於 0,而 True
值相當於 1。這意味著 argwhere(~)
可用於掩碼。
假設我們想要確定大於 2 的值的索引。我們可以這樣做:
x = np.array([1,2,3,4])
mask = x > 2
array([False, False, True, True])
使用麵膜可以得到:
np.argwhere(mask)
array([[2],
[3]])
二維數組
假設我們想要確定 2D Numpy 數組中大於 2 的值的索引
x = np.array([[1,2],[3,4],[5,6]])
mask = x > 2
array([[False, False],
[ True, True],
[ True, True]])
使用麵膜可以得到:
np.argwhere(mask)
array([[1, 0],
[1, 1],
[2, 0],
[2, 1]])
對此的解釋方法如下:
[1,0] -> row 1, column 0 is non-zero (i.e. greater than 2)
[1,1] -> row 1, column 1 is non-zero
[2,0] -> row 2, column 0 is non-zero
[2,1] -> row 2, column 1 is non-zero
相關用法
- Python argparse.ArgumentParser.convert_arg_line_to_args用法及代碼示例
- Python argparse.ArgumentParser.add_argument_group用法及代碼示例
- Python NumPy argpartition方法用法及代碼示例
- Python NumPy argmin方法用法及代碼示例
- Python argparse.FileType用法及代碼示例
- Python argparse.ArgumentParser.add_mutually_exclusive_group用法及代碼示例
- Python NumPy argsort方法用法及代碼示例
- Python argparse.ArgumentParser.get_default用法及代碼示例
- Python argparse.ArgumentParser.set_defaults用法及代碼示例
- Python argparse.ArgumentParser.exit用法及代碼示例
- Python NumPy argmax方法用法及代碼示例
- Python argparse.ArgumentParser.add_subparsers用法及代碼示例
- Python arcgis.gis._impl._profile.ProfileManager.save_as用法及代碼示例
- Python arcgis.raster.functions.ccdc_analysis用法及代碼示例
- Python arcgis.geometry.functions.trim_extend用法及代碼示例
- Python arcgis.raster.analytics.sample用法及代碼示例
- Python arcgis.features.analysis.derive_new_locations用法及代碼示例
- Python arcgis.features.analyze_patterns.calculate_density用法及代碼示例
- Python arcgis.geometry.Geometry.label_point用法及代碼示例
- Python arcgis.plan_routes用法及代碼示例
- Python arcgis.mapping.forms.FormInfo用法及代碼示例
- Python arcgis.gis.UserManager.get用法及代碼示例
- Python arcgis.raster.ImageryLayerCacheManager.update_tiles用法及代碼示例
- Python arcgis.geometry.Geometry.true_centroid用法及代碼示例
- Python arcgis.gis.User.generate_direct_access_url用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | argwhere method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。