Numpy Basics — 4

Devil’s Advocate
4 min readFeb 20, 2023

--

We saw how to use the axis parameter to handle specific dimensions of a Numpy array while doing some mathematical calculations.

In a 3D NumPy array, the axis parameter refers to the dimension or dimensions along which an operation is performed.

Since a 3D array has three dimensions, the axis parameter can take three possible values: 0, 1, or 2.

  • axis=0 refers to the 1st dimension of the array, which runs vertically along the rows of the 2D arrays. In other words, this axis corresponds to the “depth” of the 3D array.
  • axis=1 is the 2nd dimension, which runs horizontally along the columns of the 2D arrays. This axis corresponds to the “height” of the 3D array.
  • axis=2 is the 3rd dimension, which runs along the elements within each 2D array. This axis corresponds to the “width” of the 3D array.

array.max: returns the maximum value of an array.

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a.max())
# Output: 6

If the array is a 2D array, the max() function returns the maximum value in the flattened array. In other words, it treats the 2D array as a 1D array and finds the maximum value in that 1D array.

For example, consider the following 2D array:

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.max())
# Output: 6

If we call arr.max(), the output would be 6, as that is the maximum value in the flattened 1D array [1, 2, 3, 4, 5, 6].

However, to find the maximum value along the rows of a 2D array, we can call arr.max(axis=1), and to find the maximum value along the columns, we can call arr.max(axis=0).

array.min: returns the minimum value of an array.

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a.min())
# Output: 1

The same axis rule holds well for the array.min() function as it does for array.max() function.

array.argmax: returns the index of the maximum value of an array.

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a.argmax())
# Output: 5

By default, argmax returns the index of the flattened array. You can also use the argmax function on a 2D NumPy array to find the index of the maximum value along a specific axis when the axis argument is passed.

For example, consider the following array:

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 9, 8]])

To find the index of the maximum value along the rows (i.e., along the second axis), you can call np.argmax(arr, axis=1), which returns an array [2, 2, 1]. This means that the max value in the first row is at index 2, in the second row at index 2, and in the third row at index 1.

The catch to be noted is that the argmax function returns the first occurrence of the maximum value along the specified axis. If there are multiple occurrences of the maximum value, only the index of the first occurrence is returned.

array.argmin: returns the index of the minimum value of an array.

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a.argmin())
# Output: 0

The same rules as argmax function hold well for the argmin function with the axis parameter.

array.dot: returns the dot product of two arrays. When we take the dot product of two matrices, we are essentially multiplying the corresponding elements of each matrix and adding them up. This gives us a single number, which is the dot product of the two matrices.

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a.dot(b))
# Output: 32

These are some of the common methods and functions for handling arrays in NumPy.

I hope these examples help you understand their usage.

Numpy Basics Part 3 — https://medium.com/@bigfundu/numpy-basics-3-27ad58610efe

Numpy Basics Part 2 — https://medium.com/@bigfundu/numpy-basics-2-1b9980defe8b

Numpy Basics Part 1 — https://medium.com/@bigfundu/numpy-basics-1-1f367599de39

[Disclaimer: All the images I used in these Numpy were from Google searches from various sources. Am thankful to all the wonderful creators of those images.]

--

--

Devil’s Advocate
Devil’s Advocate

Written by Devil’s Advocate

Seeker for life. Looking to make technology simpler for everyone.