Overiew:
- The min() and max() functions of numpy.ndarray returns the minimum and maximum values of an ndarray object.
- The return value of min() and max() functions is based on the axis specified.
- If no axis is specified the value returned is based on all the elements of the array.
- Axis of an ndarray is explained in the section cummulative sum and cummulative product functions of ndarray.
Example-ndarray.min(), ndarray.max():
- The example provided calls min() and max() functions on ndarray objects four times each.
- Once with no axis specified
- Thrice with axis values specified - the axis values are 0, 1 and 2.
- Since the ndarray object is a 3-dimensional array object it has 3 indexes.
- The dimension of the ndarray object is given by the tuple (3,3,4).
- That is the ndarray object has three 2-dimensional arrays of shape (3,4).
# Example Python program for finding the min value along the given axis of an ndarray
# Import numpy module import numpy as np
# Import standard library module random import random
# Create a 3-Dimensional ndarray object array_3d = np.array([[[1,1,1,1], [2,2,2,2], [3,3,3,3]],
[[4,4,4,4], [5,5,5,5], [6,6,6,6]],
[[7,7,7,7], [8,8,8,8], [9,9,9,9]]] )
# Print the 3-Dimensional Array print("Input ndarray:") print(array_3d)
print("Shape of the ndarray:") print(array_3d.shape)
print("Number of dimensions/axis of the ndarray:") print(array_3d.ndim)
# Print the minimum value of the whole array - without considering the axis parameter print("Minimum value in the whole array:%d"%(array_3d.min()))
# Print the minimum value for axis = 0 print("Minimum value along the axis 0:") print(array_3d.min(axis=0))
# Print the minimum value for axis = 1 print("Minimum value along the axis 1:") print(array_3d.min(axis=1))
# Print the minimum value for axis = 2 print("Minimum value along the axis 2:") print(array_3d.min(axis=2))
# Print the maximum value of the whole array - without considering the axis parameter print("Maximum value in the whole array:%d"%(array_3d.max()))
# Print the maximum value for axis = 0 print("Maximum value along the axis 0:") print(array_3d.max(axis=0))
# Print the maximum value for axis = 1 print("Maximum value along the axis 1:") print(array_3d.max(axis=1))
# Print the maximum value for axis = 2 print("Maximum value along the axis 2:") print(array_3d.max(axis=2))
|
Output:
Input ndarray: [[[1 1 1 1] [2 2 2 2] [3 3 3 3]]
[[4 4 4 4] [5 5 5 5] [6 6 6 6]]
[[7 7 7 7] [8 8 8 8] [9 9 9 9]]] Shape of the ndarray: (3, 3, 4) Number of dimensions/axis of the ndarray: 3 Minimum value in the whole array:1 Minimum value along the axis 0: [[1 1 1 1] [2 2 2 2] [3 3 3 3]] Minimum value along the axis 1: [[1 1 1 1] [4 4 4 4] [7 7 7 7]] Minimum value along the axis 2: [[1 2 3] [4 5 6] [7 8 9]] Maximum value in the whole array:9 Maximum value along the axis 0: [[7 7 7 7] [8 8 8 8] [9 9 9 9]] Maximum value along the axis 1: [[3 3 3 3] [6 6 6 6] [9 9 9 9]] Maximum value along the axis 2: [[1 2 3] [4 5 6] [7 8 9]] |