Python Program - Maximum Value of Numpy Array - numpy.max()
Numpy - Maximum Value in Array
Given a numpy array, you can find the maximum value of all elements in the array.
To get the maximum value of a NumPy Array, you can use numpy.max()
function.
Syntax
The syntax of max() function is as follows:
max_value = numpy.max(arr)
Pass the numpy array as an argument to numpy.max()
, and this function will return the maximum value.
Examples
1. Maximum value of numpy array
In this example, we will take a numpy array with random integers and then find the maximum of the array using numpy.max()
function.
Python Program
import numpy as np
arr = np.random.randint(10, size=(4,5))
print('array\n', arr)
#find maximum value
max_value = np.max(arr)
print('Maximum value in array\n', max_value)
Explanation:
- The
np.random.randint()
generates a 4x5 matrix with random integers between 0 and 9. - The
numpy.max()
function is used to find the largest value in the array. - The
print()
function displays the original array and the maximum value.
Output
[[8 8 5 3 0]
[2 0 4 5 1]
[4 7 0 1 5]
[5 2 1 5 8]]
Maximum value in array
8
2. Maximum value of numpy array with float values
In this example, we will take a numpy array with random float values and then find the maximum of the array using numpy.max()
function.
Python Program
import numpy as np
arr = np.random.rand(6).reshape(2,3)
print('array\n', arr)
#find maximum value
max_value = np.max(arr)
print('Maximum value in array\n', max_value)
Explanation:
- The
np.random.rand()
generates a 1D array of 6 random float values between 0 and 1, which is reshaped into a 2x3 array usingreshape()
. - The
numpy.max()
function identifies the maximum value in this array of float numbers. - The result is printed to show the maximum value in the array.
Output
array
[[0.45733784 0.70319461 0.65038256]
[0.77489769 0.71777846 0.89612105]]
Maximum value in array
0.8961210501172747
3. Maximum Value Along Specific Axis
You can also find the maximum value along a specific axis (row-wise or column-wise) in a 2D array.
Python Program
import numpy as np
arr = np.random.randint(10, size=(3,4))
print('array\n', arr)
# find maximum value along each column
max_value_col = np.max(arr, axis=0)
# find maximum value along each row
max_value_row = np.max(arr, axis=1)
print('Maximum value in each column\n', max_value_col)
print('Maximum value in each row\n', max_value_row)
Explanation:
- The array is created using
np.random.randint()
to generate random integers between 0 and 9 in a 3x4 matrix. np.max(arr, axis=0)
calculates the maximum value along each column (vertically).np.max(arr, axis=1)
calculates the maximum value along each row (horizontally).- The results are printed for both row and column-wise maximum values.
Output
array
[[8 3 1 7]
[0 6 4 8]
[5 2 6 9]]
Maximum value in each column
[8 6 6 9]
Maximum value in each row
[8 8 9]
Summary
In this Numpy Tutorial, we learned how to find the maximum value in a Numpy Array using the numpy.max()
function, with detailed examples covering integer and float arrays, as well as finding maximum values along specific axes.