30 Mar Axes in Numpy arrays
In Numpy arrays, the axis is the direction along the rows and columns. A two-dimensional numpy array has the following two corresponding axes:
vertical axis (axis 0) horizontal axis (axis 1)
Let us see an example to understand the concept of axis:
Getting minimum value with axes
Specifying axes will calculate values along that specific axes only. Let us see an example. We have used the min() method and the axis attribute:
import numpy as np
# Create a numpy 3x5 array
n = np.array([[2, 5, 7, 9, 6], [15, 25, 35, 45, 55], [100, 200, 300, 400, 500]])
print("Iterating 2D array...")
for a in n:
print(a)
print("\nMinimum value = ", n.min())
print("\nMinimum value with axis 0 (vertical axes) = ", n.min(axis=0))
print("\nMinimum value with axis 1 (horizontal axes) = ", n.min(axis=1))
Output
Iterating 2D array... [2 5 7 9 6] [15 25 35 45 55] [100 200 300 400 500] Minimum value = 2 Minimum value with axis 0 (vertical axes) = [2 5 7 9 6] Minimum value with axis 1 (horizontal axes) = [ 2 15 100]
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments