NumPy 的 argmin(~)
方法返回與數組中最小元素對應的索引。
注意
如果您的數組缺少值(即 NaN
s),則 np.argmin(~)
方法會將它們視為最小值。如果您想忽略缺失值,請改用 np.nanargmin(~)
方法。
參數
1. a
| array_like
輸入數組。
2. axis
| int
| optional
計算方法所沿的軸。對於二維數組,如果 axis=0
,則該方法按列執行,如果 axis=1
則按行執行。如果未提供軸,則 NumPy 會將您的數組視為展平數組。
返回值
如果未提供axis
,則返回標量。否則,返回 NumPy 數組。
例子
一維數組
x = np.array([3,5,2,1])
np.argmin(x)
3
這裏,返回 3,因為最小值(即 1)位於索引 3。
二維數組
假設我們有以下 2D NumPy 數組:
x = np.array([[5,4],[1,3]])
x
array([[5, 4],
[1, 3]])
整個數組的最大索引
要獲取整個數組中最大值的索引,請省略 axis
參數:
np.argmin(x)
2
每列的最大索引
要按列獲取最小值的索引,請設置 axis=0
:
np.argmin(x, axis=0)
array([1, 1])
在這裏,我們將檢查矩陣的每一列並計算其最小值的索引。
每行的最大索引
要按行獲取最小值的索引,請設置 axis=1
:
np.argmin(x, axis=1)
array([1, 0])
在這裏,我們將檢查矩陣的每一行並計算其最小值的索引。
相關用法
- Python NumPy argmax方法用法及代碼示例
- Python argparse.ArgumentParser.convert_arg_line_to_args用法及代碼示例
- Python argparse.ArgumentParser.add_argument_group用法及代碼示例
- Python NumPy argpartition方法用法及代碼示例
- Python NumPy argwhere方法用法及代碼示例
- 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 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 | argmin method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。