Numpy 的 nanmax(~)
方法會忽略所有缺失值(即 NaN
)並返回 Numpy 數組中的最大值。最大值也可以按行和列計算。
參數
1. a
| array_like
輸入數組。
2. axis
| None
或 int
| optional
允許的值如下:
參數值 |
意義 |
---|---|
軸=0 |
按列計算的最大值 |
軸=1 |
逐行計算的最大值 |
None |
從整個數組計算出的最大值 |
默認情況下,axis=None
。
3. initial
| int
| optional
如果計算出的最大值小於 initial
,則將返回 initial
。
4. where
| booleans
的array-like
| optional
我們可以通過提供此參數來選擇要考慮的值,而不是考慮所有值。僅考慮掩碼中對應於 True
的值。
返回值
如果未提供軸參數,則返回標量。否則,返回一個 Numpy 數組。
例子
整個數組的最大值
np.nanmax([[2,5],[np.NaN,3]])
5.0
每列的最大值
np.nanmax([[2,5],[np.NaN,3]], axis=0)
array([2., 5.])
每行的最大值
np.nanmax([[2,5],[np.NaN,3]], axis=1)
array([5, 3])
傳入初始參數
np.nanmax([[2,5],[1,3]], initial=8)
8
這裏,計算出的最大值是 5,但它小於提供的初始值(即 8),因此返回 8。
傳遞布爾掩碼
我們可以通過提供掩碼來選擇要計算最大值的值,而不是考慮所有值:
np.nanmax([2,5,3,4], where=[True,False,False,True], initial=-1)
4
這裏,雖然 5 從技術上來說是最大值,但它被忽略,因為它在掩碼中的對應值是 False
。請注意,我們需要在此處提供參數 initial
,如果無法計算最大值(例如,當掩碼全部為 False
時),該參數將作為返回值。
相關用法
- Python NumPy nanmin方法用法及代碼示例
- Python NumPy nanargmax方法用法及代碼示例
- Python NumPy nanargmin方法用法及代碼示例
- Python OpenCV namedWindow()用法及代碼示例
- Python numpy.less()用法及代碼示例
- Python networkx.algorithms.shortest_paths.weighted.all_pairs_dijkstra_path用法及代碼示例
- Python numpy.polynomial.hermite.hermmul用法及代碼示例
- Python numpy.seterrobj用法及代碼示例
- Python networkx.classes.function.edge_subgraph用法及代碼示例
- Python numpy.tril()用法及代碼示例
- Python numpy.around用法及代碼示例
- Python networkx.algorithms.tree.mst.maximum_spanning_edges用法及代碼示例
- Python numpy.random.standard_normal()用法及代碼示例
- Python networkx.algorithms.bipartite.basic.color用法及代碼示例
- Python numpy.select用法及代碼示例
- Python networkx.algorithms.bipartite.cluster.latapy_clustering用法及代碼示例
- Python networkx.readwrite.json_graph.adjacency_data用法及代碼示例
- Python numpy.fft.irfft2用法及代碼示例
- Python numpy.polynomial.hermite_e.hermemul用法及代碼示例
- Python numpy.fft.irfftn用法及代碼示例
- Python numpy.nonzero()用法及代碼示例
- Python numpy.maximum_sctype()用法及代碼示例
- Python numpy.ma.dstack用法及代碼示例
- Python numpy.ma.make_mask_none()用法及代碼示例
- Python numpy.mod用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | nanmax method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。