## Introduction to NumPy max

Whenever we wish to find the maximum value of all the elements present in the array created using NumPy function, we are going to make use of another NumPy function called max function and this max function returns the maximum value of all the elements present in the array and the name of the array consisting of all the elements stored in it and whose maximum value must be found is passed as a parameter to the max function and max function in NumPy is a built in function in Python to easily find the maximum value of the elements present in the given array.

**Syntax:**

`maximum_value = numpy.max(arrayname)`

**Parameter –**

- arrayname: It is 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, which specifies the array whose maximum value is to find and return.**axis –**This is an optional parameter, which specifies the axis on which along which to calculate the max value.**out –**This is an optional parameter, which specifies the output array where we can store the output of the NumPy.max() function.**keepdims –**This is an optional parameter, which specifies to keeps the dimensions of an output array the same as an input array.**Return value –**The return value of this function is the max value from the NumPy array.

### Working of NumPy max

Working of NumPy max is as follows:

- Whenever we wish to find the maximum value of all the elements present in the array created using function, we are going to make use of another NumPy function called max function.
- The max function in NumPy returns the maximum value of all the elements present in the array.
- The name of the array consisting of all the elements stored in it and whose maximum value must be found is passed as a parameter to the max function.
- The max function is a built in function in Python to easily find the maximum value of the elements present in the given array.

### Examples of NumPy max

Following are the examples given below:

#### Example #1

Python program to demonstrate NumPymax functionto 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([[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, apackage called NumPy is imported to enable us to make use of array and max function. Then an array is created by making use of array function and storing it in a variable called namarray. Then the elements of the namarray are displayed on the screen. Then by making use of max function of NumPy and passing the created array as the parameter to that function to find the maximum value and and the value is stored in a variable called maxval. Then displaying the maximum value stored in 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

4.5 (6,869 ratings)

View Course

**Output:**

In the above program, apackage called NumPy is imported to enable us to make use of array and max function. Then an array is created by making use of array function in NumPyand storing it in a variable called namarray. Then the elements of the namarray are displayed on the screen. Then by making use of max function and passing the created array as the parameter to that function to find the maximum value and and the value is stored in a variable called maxval. Then displaying the maximum value stored in 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:**

In the above program, a package called NumPy is imported to enable us to make use of array and max function. Then an array is created by making use of the array function in NumPy and storing it in a variable called namarray. Then the elements of the namarray are displayed on the screen. Then by making use of the max function and passing the created array as the parameter to that function to find the maximum value and the value is stored in a variable called maxval. Then displaying the maximum value stored in 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:**

As in the above program, the max() function is used to find the max value from the 2D array. The max value is found in three ways, the first way where the axis and keepdims parameters are not passed and the max value return is 89, second way where the axis and keepdims parameters are passed as axis=0, keepdims=True(keeps the output array dimension same as input dimension array) and 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

This is a guide to NumPy max. Here we also discuss the introduction and working of numpy max along with different examples and its code implementation. You may also have a look at the following articles to learn more –