Updated June 15, 2023
Introduction to NumPy max
Whenever we wish to find the maximum value of all the elements present in the array created using the NumPy function, we will use another NumPy function called the max function. This max function returns the maximum value of all the elements present in the array. The parameter passed to the max() function is the collection (array) in which we want to find the maximum value. The max function in NumPy is a built-in function in Python to quickly find the maximum value of the elements present in the given array.
Syntax:
maximum_value = numpy.max(arrayname)
Parameter –
- arrayname: It is the name of the array from which the maximum value of all the elements must be found.
- maximum_value: Maximum value of all the elements in the array found by using the max function in NumPy and returned by the max function.
Another syntax is:
NumPy.max( array, axis, out, keepdims )
Parameters –
- array – This is not an optional parameter specifying the array whose maximum value is to find and return.
- axis – This is an optional parameter specifying the axis along which to calculate the max value.
- out – This is an optional parameter that specifies the output array where we can store the output of the NumPy.max() function.
- Keep-dims – This is an optional parameter that specifies keeping the dimensions of an output array the same as an input array.
- Return value – This function’s return value is the maximum value from the NumPy array.
Examples of NumPy max
Following are the examples given below:
Example #1
Python program to demonstrate the NumPymax function to display the maximum value of all the elements stored in the array created using the array function.
Code:
#importing the package numpy
import numpy as num1
#Creating an array by making use of array function in NumPy and storing it in a variable called namarray
namarray = num1.array([[2,3],[4,5]])
#Displaying the elements of namarray followed by one line space by making use of \n
print 'The elements of the given array are:'
print namarray
print '\n'
#using max function of NumPy and passing the created array as the parameter to that function to find the maximum value and store it in a variable called maxval
maxval = num1.max(namarray)
#Displaying the maximum value stored in maxval variable
print 'The maximum value of all the elements of the array is:'
print maxval
Output:
In the above program, we import the NumPy package to utilize the array and max functions. We create an array using the array function and assign it to the variable namarray. Subsequently, we display the elements of namarray on the screen. To find the maximum value, we utilize the max function from NumPy and pass the created array as the parameter. The maximum value is then stored in the variable maxval. Then displaying the maximum value stored in the maxval variable as the output on the screen.
Example #2
Python program to demonstrate NumPy max function to display the maximum value of all the elements stored in the array created using array function in Numpy:
Code:
#importing the package numpy
import numpy as num1
#Creating an array by making use of array function in NumPy and storing it in a variable called namarray
namarray = num1.array([[0.3,0.7, 0.2],[0.4,0.5, 0.8]])
#Displaying the elements of namarray followed by one line space by making use of \n
print 'The elements of the given array are:'
print namarray
print '\n'
#using max function of NumPy and passing the created array as the parameter to that function to find the maximum value and store it in a variable called maxval
maxval = num1.max(namarray)
#Displaying the maximum value stored in maxval variable
print 'The maximum value of all the elements of the array is:'
print maxval
Output:
In the above program, we import the NumPy package to gain access to the array and max functions. We create an array using the array function from NumPy and assign it to the variable namarray. We display the elements of namarray on the screen. Next, we use the max function, passing namarray as the parameter, to find the maximum value. The maximum value is stored in the variable maxval. Then displaying the maximum value stored in the maxval variable as the output on the screen.
Example #3
Python program to demonstrate NumPy max function to display the maximum value of all the elements stored in the array created using array function.
Code:
#importing the package numpy
import numpy as num1
#Creating an array by making use of array function in NumPy and storing it in a variable called namarray
namarray = num1.array([[0.3556, 0.3500],[0.3554,0.3551]])
#Displaying the elements of namarray followed by one line space by making use of \n
print 'The elements of the given array are:'
print namarray
print '\n'
#using max function of NumPy and passing the created array as the parameter to that function to find the maximum value and store it in a variable called maxval
maxval = num1.max(namarray)
#Displaying the maximum value stored in maxval variable
print 'The maximum value of all the elements of the array is:'
print maxval
Output:
To utilize the array and max functions, we import the NumPy library in the above program. We utilize the array function from NumPy to create an array, and we store it in the variable namarray. The elements of namarray are subsequently displayed on the screen. We pass the created array as an argument to the max function, which computes the maximum value. This maximum value is then stored in the variable maxval. Then displaying the maximum value stored in the maxval variable as the output on the screen.
Example #4
Example of max() function to find max value in the 1-D array –
Code:
# import numpy package as np
import numpy as np
# creating 1d number array
array = np.array([ 10, 20, 30, 35, 40, 50, 60, 37 ])
print("The number array is : ", array )
max_value = np.max( array )
print( "The max value in an array is : ", max_value )
Output:
As in the above program, the max() function is used to find the max value from the array. So the max() function returns 60, which is the maximum number in an array, as we can see in the above output.
Example #5
Example of max () function to find max value in the 2-D array with axis and keepdims –
Code:
# import numpy package as np
import numpy as np
# creating 2d number array
array = np.array([ [11, 89, 76], [40, 55, 23], [76, 56, 60] ])
print("The number array is : ")
print(array)
max_value = np.max( array )
print( "The max value in an array is : ", max_value )
# calculate max along column
max_value = np.max( array, axis=0, keepdims = True )
print( "The max value with aix = 0 and keepdims = True in an array is : ", max_value )
# calculate max along row
max_value = np.max( array, axis=1, keepdims = False )
print( "The max value with aix = 1 and keepdims = False in an array is : ", max_value )
Output:
Similarly to the above program, we use the max() function to extract the largest value from a 2D array. The first approach does not involve passing the axis and keepdims arguments. The second approach sets the axis parameter to 0 and the keepdims parameter to True, resulting in a maximum value of 89. This approach keeps the output array dimension the same as the input array dimension. The max values return are [[76, 89, 76]], the third way where the axis and keepdims parameters are passed as axis=1 and keepdims=False(not keeps the output array dimension same as input dimension array) and the max values return are [89, 55, 76], as we can see in the above output.
Recommend ed Articles
We hope that this EDUCBA information on “NumPy max” was beneficial to you. You can view EDUCBA’s recommended articles for more information.