958
In this article we show 2 ways to find the largest value in a numpy array
Example 1
In this example we use the numpy max function that returns the largest or maximum value in an array
import numpy as np myarray = np.array([5, 32, 17, 56, 98, 23, 36, 33, 45, 92]) print("Array Items = ", myarray) print("The Largest Number in the Array = ", max(myarray))
Run this and you will see the following
>>> %Run numpyarraylargest1.py Array Items = [ 5 32 17 56 98 23 36 33 45 92] The Largest Number in the Array = 98
Example 2
In this example we use the numpy sort function to sort the array in ascending order and then display this
import numpy as np myarray = np.array([5, 32, 17, 56, 98, 23, 36, 33, 45, 92]) print("Array Items = ", myarray) myarray.sort() arraylength = len(myarray) - 1 print("The Largest Number is = ", myarray[arraylength])
If you run this you should see something like this
>>> %Run numpyarraylargest2.py Array Items = [ 5 32 17 56 98 23 36 33 45 92] The Largest Number is = 98
Link
The examples can be found in the following github repository
https://github.com/programmershelp/maxpython/upload/main/numpy
The examples are numpyarraylargest1 and numpyarraylargest2