https://thispointer.com/find-max-value-its-index-in-numpy-array-numpy-amax/
在本文中,我们将讨论如何使用numpy.amax()获取Numpy数组及其索引中的最大/最大值。
numpy.amax()
Python的numpy模块提供了一个从Numpy数组中获取最大值的函数,即
numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>)
参数:
a: numpy数组,需要从中找到最大值。
axis:它是可选的,如果没有提供,那么它将使传递的numpy数组变平并返回其中的最大值。
如果它被提供,那么它将返回沿轴的最大值数组,即
如果axis = 0,则返回包含每列最大值的数组。
如果axis = 1,则返回包含每行最大值的数组。
让我们看一下细节,
在1D Numpy数组中查找最大值及其索引:
让我们从列表中创建一维numpy数组
arr = numpy.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17])
找到最大值:
现在让我们使用numpy.amax()通过将数组作为参数传递来从这个numpy数组中找到最大值,即
Get the maximum element from a Numpy array
maxElement = numpy.amax(arr)
print('Max element from Numpy Array : ', maxElement)
输出:
Max element from Numpy Array : 17
它返回传递的numpy数组的最大值,即17
查找最大值的索引:
使用numpy.where()获取numpy数组中最大值的索引数组
Get the indices of maximum element in numpy array
result = numpy.where(arr == numpy.amax(arr))
print('Returned tuple of arrays :', result)
print('List of Indices of maximum element :', result[0])
输出:
Returned tuple of arrays : (array([ 6, 13], dtype=int32),)
List of Indices of maximum element : [ 6 13]
在numpy.where()中,当我们仅传递条件表达式时,它返回一个数组元组(每个轴一个),其中包含满足给定条件的元素索引。由于我们的numpy数组只有一个轴,因此返回的元组包含一个索引数组。
在2D Numpy数组中查找最大值及其索引
让我们创建一个2D numpy数组即
Create a 2D Numpy array from list of lists
arr2D = numpy.array([[11, 12, 13],
[14, 15, 16],
[17, 15, 11],
[12, 14, 15]])
2D numpy数组的内容是,
[[11 12 13]
[14 15 16]
[17 15 11]
[12 14 15]]
在完整的2D numpy数组中查找最大值
要从完整的2D numpy数组中找到最大值,我们将不会在numpy.amax()中传递轴
Get the maximum value from complete 2D numpy array
maxValue = numpy.amax(arr2D)
它将从完整的2D numpy数组返回最大值,即在所有行和列中。
17
在2D numpy数组中找到沿轴的最大值 行数或列数最大值:
如果我们在numpy.amax()中传递axis = 0,那么它返回一个包含每列的最大值的数组,即
Get the maximum values of each column i.e. along axis 0
maxInColumns = numpy.amax(arr2D, axis=0)
print('Max value of every column: ', maxInColumns)
输出:
Max value of every column: [17 15 16]
1
如果我们在numpy.amax()中传递axis = 1,那么它返回一个包含每行的最大值的数组,即
Get the maximum values of each row i.e. along axis 1
maxInRows = numpy.amax(arr2D, axis=1)
print('Max value of every Row: ', maxInRows)
print('Max value of every Row: ', maxInRows)
输出:
Max value of every Row: [13 16 17 15]
从2D numpy数组中查找最大值的索引:
2D numpy数组arr2D的内容是,
[[11 12 13]
[14 15 16]
[17 15 11]
[12 14 15]]
让我们得到2D numpy数组中最大值的索引数组
Find index of maximum value from 2D numpy array
result = numpy.where(arr2D == numpy.amax(arr2D))
print('Tuple of arrays returned : ', result)
print('List of coordinates of maximum value in Numpy array : ')
# zip the 2 arrays to get the exact coordinates
listOfCordinates = list(zip(result[0], result[1]))
# travese over the list of cordinates
for cord in listOfCordinates:
print(cord)
输出:
Tuple of arrays returned : (array([2], dtype=int32), array([0], dtype=int32))
List of coordinates of maximum value in Numpy array :
(2, 0)
numpy.amax()&NaN
numpy.amax()传播NaN值,即如果给定的numpy数组中有NaN,则numpy.amax()将返回NaN作为最大值。例如,
arr = numpy.array([11, 12, 13, 14, 15], dtype=float)
arr[3] = numpy.NaN
print('Max element from Numpy Array : ', numpy.amax(arr))
输出:
Max element from Numpy Array : nan
如果要在从numpy中找到最大值时忽略NaN,请改用numpy.nanmax()。
完整的例子如下,
import numpy
def main():
# Create a Numpy array from a list
arr = numpy.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17])
print('Contents of Numpy array : ', arr, sep='\n')
print("*** Get Maximum element from a 1D numpy array***")
# Get the maximum element from a Numpy array
maxElement = numpy.amax(arr)
print('Max element from Numpy Array : ', maxElement)
print("*** Get the indices of maximum element from a 1D numpy array***")
# Get the indices of maximum element in numpy array
result = numpy.where(arr == numpy.amax(arr))
print('Returned result :', result)
print('List of Indices of maximum element :', result[0])
print("*** Get Maximum element from a 2D numpy array***")
# Create a 2D Numpy array from list of lists
arr2D = numpy.array([[11, 12, 13],
[14, 15, 16],
[17, 15, 11],
[12, 14, 15]])
print('Contents of 2D Numpy Array', arr2D, sep='\n')
# Get the maximum value from complete 2D numpy array
maxValue = numpy.amax(arr2D)
print('Max value from complete 2D array : ', maxValue)
# Get the maximum values of each column i.e. along axis 0
maxInColumns = numpy.amax(arr2D, axis=0)
print('Max value of every column: ', maxInColumns)
# Get the maximum values of each row i.e. along axis 1
maxInRows = numpy.amax(arr2D, axis=1)
print('Max value of every Row: ', maxInRows)
print('*** Get the index of maximum value in 2D numpy array ***')
# Find index of maximum value from 2D numpy array
result = numpy.where(arr2D == numpy.amax(arr2D))
print('Tuple of arrays returned : ', result)
print('List of coordinates of maximum value in Numpy array : ')
# zip the 2 arrays to get the exact coordinates
listOfCordinates = list(zip(result[0], result[1]))
# travese over the list of cordinates
for cord in listOfCordinates:
print(cord)
print('*** numpy.amax() & NaN ***')
arr = numpy.array([11, 12, 13, 14, 15], dtype=float)
arr[3] = numpy.NaN
print('Max element from Numpy Array : ', numpy.amax(arr))
if __name__ == '__main__':
main()
Contents of Numpy array :
[11 12 13 14 15 16 17 15 11 12 14 15 16 17]
*** Get Maximum element from a 1D numpy array***
Max element from Numpy Array : 17
*** Get the indices of maximum element from a 1D numpy array***
Returned result : (array([ 6, 13], dtype=int32),)
List of Indices of maximum element : [ 6 13]
*** Get Maximum element from a 2D numpy array***
Contents of 2D Numpy Array
[[11 12 13]
[14 15 16]
[17 15 11]
[12 14 15]]
Max value from complete 2D array : 17
Max value of every column: [17 15 16]
Max value of every Row: [13 16 17 15]
*** Get the index of maximum value in 2D numpy array ***
Tuple of arrays returned : (array([2], dtype=int32), array([0], dtype=int32))
List of coordinates of maximum value in Numpy array :
(2, 0)
*** numpy.amax() & NaN ***
Max element from Numpy Array : nan
网友评论