Numpy Array Axis amd argmax max mean sort reshape Methods

In this class, We discuss Numpy Array Axis and argmax max mean sort reshape Methods.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of numpy arrays. Click here.

This class discusses some of the methods required for an introductory python course and placement course.

We use many other methods in our data science course. These methods are discussed in our data science playlist.

Numpy Array Methods

axis and argmax Method

The concept of the axis is a bit confusing to understand. Would you please focus a little for a better understanding?

The argmax Method will identify the index of the maximum element.

The below example shows the working of argmax Method.

import numpy as np
x=np.array([[4,5,6],[3,4,9],[8,1,7]])
print(x)
print("index of maximum element on axis 0")
y=x.argmax(axis=0)# display the index of maximum element
print(y)

Output:
[[4 5 6]
 [3 4 9]
 [8 1 7]]
index of maximum element on axis 0
[2 0 1]

The example axis=0 tells us the argmax Method should be applied on columns.

Usually, we think axis=0 will be used on rows. No, numpy takes on columns.

In the above example, the first column maximum element is 8.

Element 8 is at position 2. The method argmax will return the index 2 for the first column.

The method argmax will identify the index of the maximum element in each column.

axis=1 will give the index of the maximum element in rows.

The below example shows the method argmax with parameter axis=1.

x=np.array([[4,5,6],[3,4,9],[8,1,7]])
print(x)
print("index of maximum element on axis 1")
y=x.argmax(axis=1)# display the index of maximum element
print(y)

Output:
[[4 5 6]
 [3 4 9]
 [8 1 7]]
index of maximum element on axis 1
[2 2 0]

axis not Specified

The below example shows the method argmax without axis parameter.

x=np.array([[4,5,6],[3,4,9],[8,1,7]])
print(x)
print("index of maximum element axis not specified")
y=x.argmax()# axis not specified will take elements in sequence
print(y)

Output:
[[4 5 6]
 [3 4 9]
 [8 1 7]]
index of maximum element axis not specified
5

The argmax Method will take the maximum element in the array. And the index is taken in the row order.

In our example, we have the maximum element 9. Element 9 is the last in the second row.

The index is given 5. because first row indexes are taken 0,1,2.

The second-row indexes are taken 3,4,5. So on.

axis in three-dimensional Arrays

In three-dimensional arrays, axis=0 will apply to the third dimension.

The below example shows the three-dimensional array.

# 3 dimmensional
x=np.array([[[4,5,6],[3,4,9],[8,1,7]],[[8,11,7],[9,1,2],[6,12,4]]])
print(x)
print("index of maximum element 3D axis 0")
y=x.argmax(axis=0)# axis 0 in 3D
print(y)

Output:
[[[ 4  5  6]
  [ 3  4  9]
  [ 8  1  7]]

 [[ 8 11  7]
  [ 9  1  2]
  [ 6 12  4]]]
index of maximum element 3D axis 0
[[1 1 1]
 [1 0 0]
 [0 1 0]]

In the example, we have two two-dimensional arrays of size 3X3.

The third dimension has indexes 0 and 1.

axis=0 will be applied on the third dimension. in our example, we have 4 and 8 in the first box of the two-dimensional arrays, respectively.

The element 8 is maximum. The third dimension index for element 8 is 1.

Similarly, the second element in the two-dimensional matrices is 5 and 11.

the maximum is 11. the third dimension index is 1.

If you are not able to understand the concept, please watch the video for better clarity.

Axis=1 will be applied on columns.

In our example, we have two two-dimensional arrays. A total of six columns are present.

The below example shows the program for axis =1.

# 3 dimmensional
x=np.array([[[4,5,6],[3,4,9],[8,1,7]],[[8,11,7],[9,1,2],[6,12,4]]])
print(x)
print("index of maximum element 3D axis 1")
y=x.argmax(axis=1)# axis 1 in 3D
print(y)

Output:
[[[ 4  5  6]
  [ 3  4  9]
  [ 8  1  7]]

 [[ 8 11  7]
  [ 9  1  2]
  [ 6 12  4]]]
index of maximum element 3D axis 1
[[2 0 1]
 [1 2 0]]

axis =2 applied on rows.

The below example shows the program for axis = 2.

# 3 dimmensional
x=np.array([[[4,5,6],[3,4,9],[8,1,7]],[[8,11,7],[9,1,2],[6,12,4]]])
print(x)
print("index of maximum element 3D axis 2")
y=x.argmax(axis=2)# axis 2 in 3D
print(y)

[[[ 4  5  6]
  [ 3  4  9]
  [ 8  1  7]]

 [[ 8 11  7]
  [ 9  1  2]
  [ 6 12  4]]]
index of maximum element 3D axis 2
[[2 2 0]
 [1 0 1]]

Total, we have six rows. The output gives six indexes.

axis not Mentioned in Three dimension

If the axis is not mentioned, python takes the maximum element of all the elements.

The index is given row-wise.

# 3 dimmensional
x=np.array([[[4,5,6],[3,4,9],[8,1,7]],[[8,11,7],[9,1,2],[6,12,4]]])
print(x)
print("index of maximum element axis not mentioned")
y=x.argmax()# axis not mentioned-- take in sequence
print(y)

[[[ 4  5  6]
  [ 3  4  9]
  [ 8  1  7]]

 [[ 8 11  7]
  [ 9  1  2]
  [ 6 12  4]]]
index of maximum element axis not mentioned
16

In our example, the maximum element is 12.

Element 12 is in the second two-dimensional array third-row second element.

The index value is given 16.

axis in One Dimensional Array

In one dimensional array, we have one row. So axis=0 will take the row.

The below example is on the one-dimensional array.

#1 dimmension
x=np.array([1,2,3,4])
print(x)
print("index of maximum element")
y=x.argmax(axis=0)
print(y)

Output:
[1 2 3 4]
index of maximum element
3

Attributes and Methods

Transpose Attribute

The transpose attribute T is used to transpose the array.

Transpose means rows are converted to columns.

The below example shows the transpose of the array.

# array methods and attributes

import numpy as np
x=np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
print(x)# before transpose
y=x.T
print(y)# after transpose

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
[[ 1  6 11]
 [ 2  7 12]
 [ 3  8 13]
 [ 4  9 14]
 [ 5 10 15]]

data Attribute

The data attribute will give the starting address of the array.

print(x.data)#starting address of the array elements

size Attribute

The size attribute gives the number of elements in the array.

print(x.size)#no of elements in the array

Output:
15

itemsize Attribute

The itemsize Attribute gives the size of the element in bytes.

print(x.itemsize)#size of each element in bytes

Output:
4

ndim Attribute

The ndim Attribute gives the number of array dimensions.

print(x.ndim)# no of array dimmensions

Output:
2

shape Attribute

The shape attribute gives the shape of the array.

print(x.shape)# array dimmensions

Output:
(3, 5)

argmin Method

The argmin method will display the index of the minimum element in the array.

The below example shows the program for argmin.

# argmin method to give index of minimum element
x=np.array([[4,5,6],[3,4,9],[8,1,7]])
print(x)
print("index of minimum element on axis 0")
y=x.argmin(axis=0)# display the index of maximum element
print(y)

Output:
[[4 5 6]
 [3 4 9]
 [8 1 7]]
index of minimum element on axis 0
[1 2 0]

The above-discussed axis concept applied to argmin method.

max Method

The max Method will return the maximum element in the array.

The below example shows the program for the max Method.

# max method to give maximum element
x=np.array([[4,5,6],[3,4,9],[8,1,7]])
print(x)
print("maximum element on axis 0")
y=x.max(axis=0)# display the maximum element
print(y)

Output:
[[4 5 6]
 [3 4 9]
 [8 1 7]]
maximum element on axis 0
[8 5 9]

The above-discussed axis concept applied to the max Method.

min Method

The min method will return the minimum element in the array.

The below example shows the program for min method.

# min method to give minimum element
x=np.array([[4,5,6],[3,4,9],[8,1,7]])
print(x)
print("minimum element on axis 0")
y=x.min(axis=0)# display the minimum element
print(y)

Output:
[[4 5 6]
 [3 4 9]
 [8 1 7]]
minimum element on axis 0
[3 1 6]

The above-discussed axis concept applied to the min method.

mean Method

The mean method will return the mean value of elements in the array.

The below example shows the program for the mean Method.

# mean method to give mean value
x=np.array([[4,5,6],[3,4,9],[8,1,7]])
print(x)
print("mean value on axis 0")
y=x.mean(axis=0)# display the mean value
print(y)

Output:
[[4 5 6]
 [3 4 9]
 [8 1 7]]
mean value on axis 0
[5.         3.33333333 7.33333333]

The above-discussed axis concept applied to the mean Method.

reshape Method

We can reshape the array the way we needed.

The below example shows the program for the reshaping Method.

# reshape method
x=np.array([1,2,3,4,5,6,7,8,9])
print(x)
print("after reshape")
y=x.reshape((3,3))
print(y)

Output:
[1 2 3 4 5 6 7 8 9]
after reshape
[[1 2 3]
 [4 5 6]
 [7 8 9]]

In the example, we considered a one-dimensional array.

The array is reshaped using reshape(5,-1).

the value 5 takes five rows. The value -1 states the columns are not mentioned.

If columns are not mentioned, python takes them according to the data.

Five rows are compulsory to take.

The below example reshape a two-dimensional array into a three-dimensional array.

x=np.array([[4,5,6,1],[3,4,9,8],[8,1,7,7]])
print(x)
print("after reshape")
y=x.reshape((2,3,2))
print(y)

Output:
[[4 5 6 1]
 [3 4 9 8]
 [8 1 7 7]]
after reshape
[[[4 5]
  [6 1]
  [3 4]]

 [[9 8]
  [8 1]
  [7 7]]]

sort Method

The sort method is used to sort the elements.

By default, the sort method will sort elements in the rows.

axis=0 means the elements in the columns are sorted.

Given below shows row and column sort examples.

#sort method
x=np.array([[4,5,6,1],[3,4,9,8],[8,1,7,7]])
print(x)
print("after sort")
x.sort()#inplace
print(x)

Output:
[[4 5 6 1]
 [3 4 9 8]
 [8 1 7 7]]
after sort
[[1 4 5 6]
 [3 4 8 9]
 [1 7 7 8]]

#sort method
x=np.array([[4,5,6,1],[3,4,9,8],[8,1,7,7]])
print(x)
print("after sort")
x.sort(axis=0)#inplace
print(x)

Output:
[[4 5 6 1]
 [3 4 9 8]
 [8 1 7 7]]
after sort
[[3 1 6 1]
 [4 4 7 7]
 [8 5 9 8]]