Thursday, August 30, 2018

Numpy Argsort, Argmin, Argmax And Other Tips | Numpy Tutorial Part 4 | Codeing School

This is the part 4 of Numpy Tutorial and Jupyter Notebook Tutorial. Numpy is a python array function, it helps for Data Science and Data Analysis, and it is used for scientific computing with Python. This is an introduction for beginners with examples. Jupyter Notebook is best for Data Science and Data Analysis, that's why we used Jupyter Notebook

Numpy Tutorial Part 4 & Jupyter Notebook Part 4


Today’s Topic: Array Range Function, Argsort, Argmin, Argmax.


Array Range Function:
In the Numpy array, we use range function that defines the range of the array. Array range starts from zero indexes.

arr = np.arange(6)
arr

Output:
array([0, 1, 2, 3, 4, 5])

Array Range


Logic: That’s means the array start from 0 and end at 6. That’s why we get the value 0 to 5.

Argsort:
Take an array,

a=np.array([253,4,2,56,22,5678,234,5678,34,56,31,54,32,23,23,2354,436,3,21,7])
np.argsort(a)

Output
array([ 2, 17,  1, 19, 18,  4, 14, 13, 10, 12,  8, 11,  9,  3,  6,  0, 16,15,  5,  7], dtype=int32)

Logic: First, we take an array “a”, which has 20 elements. The np.argsort(a), says that if we want to sort the array, which is the smallest element’s index number. Means, in this array “2” is the smallest number and its index number is “2”, in the output, we find the smallest number’s index is “2”. Then the second smallest number in the array is “3” and its index number is “17”. In the output, we find the second smallest number’s index is “17”.
Basically, it says, which element is smallest and its index number, in sorted order.

Argsort


Argmin:

np.argmin(a)

Output
2

Argmax:

np.argmax(a)

Output
5

Logic: In argmin() and argmax(), we find the minimum and maximum index number of the array elements.

Argmin & Argmax


Other Tips:

Take an array,

b=np.array([253,4,2,56,2,5678,234,5678,34,56,3,54,3,23,23,23       54,436,2,2,7])
b = b.reshape(5,4)
b

Output
array([[ 253,    4,    2,   56],
           [   2, 5678,  234, 5678],
           [  34,   56,    3,   54],
           [   3,   23,   23, 2354],
           [ 436,    2,    2,    7]])

Logic: reshape(), this function changes the 1D array to 2D array. The “1X20” matrix change into “5X4” matrix.

*You can do argsort in this array.

np.argsort(b)

Output
array([[2, 1, 3, 0],
          [0, 2, 1, 3],
          [2, 0, 3, 1],
          [0, 1, 2, 3],
          [1, 2, 3, 0]], dtype=int32)

Logic: This argsort do row-wise. In the first row [253, 4, 2, 56], “2” is the smallest element and its index number “2”. In the output, we find the smallest number’s index that is “2”. In the case of “253”, “4”, “56”, the proses is same.

*You can do argsort column-wise in this array.

np.argsort(b, axis=0)

Output
array([[1, 4, 0, 4],
           [3, 0, 4, 2],
           [2, 3, 2, 0],
           [0, 2, 3, 3],
           [4, 1, 1, 1]], dtype=int32)

Logic: This argsort do column-wise. In the first row [253, 2, 34, 3, 436], “2” is the smallest element and its index number “2”. In output, we find the smallest number’s index that is “1”. In the case of “253”, “34”, “3”, “436”, the proses is same.

Other Functions



If you have, any quarry please comment below. 

1 comment: