# Find Max and Min Value of Numpy Array with its index ( 1D & 2D)

Find Max and Min Value of Numpy Array

Beginners always face difficulty in finding the max and min Value of Numpy. In fact, the max and min are very useful in finding statistics.  Therefore in this entire tutorial, you will know how to find the max and min value of Numpy and its index for both the one-dimensional and multi-dimensional array.

## 1. Max and Min Value for the One Dimensional (1-D) array

Let’s create a 1-D NumPy array using the numpy.array() method. The array()  method accepts the list of all values you want to create NumPy array as an argument.

``````#1-D array
import numpy as np
array = np.array([19,5,10,1,9,17,50,19,25,50])
print(array)``````

### Finding the Maximum Value

To find the max value you have to use the max() method. Just pass the input array as an argument inside the max() method.

``````max = np.max(array)
print("The maximum value in the array is :",max)``````

### Index for the Maximum Value

To find the index for the maximum value you have to pass a specific condition as the argument inside the numpy.where() method.

``````#Index of the Maximum element
conditon = (array == max)
result = np.where(conditon)
print("Arrays for the max element:",result)
print("List for the maximum value indexes:",result[0])``````

Please note that. If there are duplicate values in the array then the output will be a list of the same maximum values.

### Finding the Minimum Value

In the same way, You can find the minimum value using the numpy.min() method.
``````# Minimum Value
min = np.min(array)
print("The minimum value in the array is :",min)``````

### Index of the Minimum Value

The concept to find the index of the min value is the same as finding the maximum values. You have to just change the condition inside the numpy.where() method.
``````# Index of the Minimum element
conditon = (array == min)
result = np.where(conditon)
print("Arrays for the min element:",result)
print("List for the min value indexes:",result[0])``````

## 2. Max and Min Value for the Two Dimensional (2-D) array

There are three ways you can find the max and min value of the NumPy array.

1. Maximum and Minumum in the entire array
2. Max and Min values in each column
3. Maximum and Minimum values in each row.

Let’s create a two-dimensional before finding max and min values. I am randomly generating a 2-D array of size 4×3.

``````#2-D Array
array_2d = np.arange(12).reshape(4,3)
print(array_2d)``````

### Finding the Max Value in the entire array

You can find the maximum value in the entire array using the same numpy.max() method just like you have used in finding the max in 1D. It will find the lowest element and gives the output.

``````max_2d = np.max(array_2d)
print("The maximum value for the 2D-array:",max_2d)``````

### Maximum Value in Each Column and Row

Here you have to use an extra argument and it is axis = 0 or axis =1.

#### Max Value in Column

``````# maximum value in each column
max_in_column = np.max(array_2d,axis=0)
print(max_in_column)``````

#### Max Value in Row

``````# maximum value in each row
max_in_row = np.max(array_2d,axis=1)
print(max_in_row)``````

Here I am using the same method max() but now I am passing axis =0 to tell the interpreter to traverse along with the columns and axis =1 to traverse along the columns.

### Finding the Index for the Max Value in 2D

You can easily find the index of the max value in a 1-dimensional NumPy array. But for the 2D array, you have to use Numpy module unravel_index. It will easily find the Index of the Max and Min value.

``````from numpy import unravel_index
result = unravel_index(np.max(array_2d),array_2d.shape)
print("Index for the Maximum Value in the 2D Array is:",result)``````

Here I am passing the two arguments inside the unravel_index() method one is the maximum value of the array and the shape of the array. Here In our case, the shape of the array is 4 rows and 3 columns.

### Finding the Min Value in the entire array

To find the minimum value inside the array you have to use the numpy.min() method and pass the array.

``````#Minimum Element in the 2D- Array
min_2d = np.min(array_2d)
print("The minimum value for the 2D-array:",min_2d)``````

### Minimum Value in Each Column and Row

#### Min Value in Column

``````# minimum value in each column
min_in_column = np.min(array_2d,axis=0)
print(min_in_column)``````

#### Min Value in Row

``````# minimum value in each row
min_in_row = np.min(array_2d,axis=1)
print(min_in_row)``````

To find the min value in each column and row you have to just change the value of the axis, axis = 0 for the column, and axis =1 for the row in the min() method.

### Index for the Min Value in 2D

Just like you have learned how to find the index of max value in 2D using the unravel_index() method. Here you will also use that. You have to pass the minimum value and the shape of the array as the arguments.

```index_of_min = unravel_index(np.min(array_2d),array_2d.shape)
print("Index for the Minimum Value in the 2D Array is:",index_of_min)```

## Other Question

Question: Difference between amax() and max()

Many data science learner readers have asked us what is the difference between the two methods amax() and max(). The answer is that np.max() is an alias of the method amax(). They both are able to compute the maximum elements of any input array. You can also find the maximum value along the axis.

If you run the method then you can see both are calling the same function.

``````import numpy as np
print(np.max)``````

Output

`<function numpy.core.fromnumeric.amax>`
``````import numpy as np
print(np.amax)``````

Output

`<function numpy.core.fromnumeric.amax>`

Source:

numpy.ndarray.min

numpy.ndarray.max