numpy.nanmax()
函數用於返回數組的最大值或沿數組的任何特定提到的軸,而忽略任何Nan值。
用法:numpy.nanmax(arr, axis=None, out=None, keepdims = no value)
參數:
arr :輸入數組。
axis :我們想要最大值的軸。否則,它將認為arr是平坦的(在所有軸上均有效)axis = 0表示沿列
並且axis = 1表示沿行加工。
out :我們要在其中放置結果的另一個數組。數組必須具有與預期輸出相同的尺寸。
keepdims :如果將其設置為True,則縮小的軸將保留為尺寸1的尺寸。使用此選項,結果將相對於原始a正確廣播。
返回:最大數組值(如果軸為空則為標量值)或沿指定軸具有最大值的數組。
代碼1:工作
# Python Program illustrating
# numpy.nanmax() method
import numpy as np
# 1D array
arr = [1, 2, 7, 0, np.nan]
print("arr : ", arr)
print("max of arr : ", np.amax(arr))
# nanmax ignores NaN values.
print("nanmax of arr : ", np.nanmax(arr))
輸出:
arr : [1, 2, 7, 0, nan] max of arr : nan nanmax of arr : 7.0
代碼2:
import numpy as np
# 2D array
arr = [[np.nan, 17, 12, 33, 44],
[15, 6, 27, 8, 19]]
print("\narr : \n", arr)
# maximum of the flattened array
print("\nmax of arr, axis = None : ", np.nanmax(arr))
# maximum along the first axis
# axis 0 means vertical
print("max of arr, axis = 0 : ", np.nanmax(arr, axis = 0))
# maximum along the second axis
# axis 1 means horizontal
print("max of arr, axis = 1 : ", np.nanmax(arr, axis = 1))
輸出:
arr : [[nan, 17, 12, 33, 44], [15, 6, 27, 8, 19]] max of arr, axis = None : 44.0 max of arr, axis = 0 : [15. 17. 27. 33. 44.] max of arr, axis = 1 : [44. 27.]
代碼3:
import numpy as np
arr1 = np.arange(5)
print("Initial arr1 : ", arr1)
# using out parameter
np.nanmax(arr, axis = 0, out = arr1)
print("Changed arr1(having results) : ", arr1)
輸出:
Initial arr1 : [0 1 2 3 4] Changed arr1(having results) : [15 17 27 33 44]
相關用法
注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 np.nanmax() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。