Wednesday, August 29, 2018

Axis, Sorting & Useful Numpy Functions | Numpy Tutorial Part 3 | Codeing School

This is the part 3 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.   



Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
 Numpy Tutorial Part 3 Jupyter Notebook Tutorial Part 3 


Today's Topic Is Axis, Dot Product, Cross Product, Sorting.  

Before starting one important note:
To find all Numpy functions run this code:

import numpy as np
dir(np)

Output
Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
Numpy Functions

Axis:

First, take an array 

import numpy as np
a = np.array([ [1,2,3,4],

                          [5,6,7,8] ])  #this is a 2D array

a.sum(axis = 1)

Output
array([10, 26])

Logic: In this line I sum the values row wise. Means (1 + 2 + 3 + 4) = 10 and (5 + 6 + 7 + 8) = 26. With the help of this keyword (axis = 1) I denoted rows.

a.sum(axis = 0)

Output
array([ 6,  8, 10, 12])
Logic: In this line I sum the values column wise. Means (1 + 5) = 6, (2 + 6) = 8, (3 + 7) = 10, (4 + 8) = 12. With the help of this keyword (axis = 0) I denoted collums.

Note: axis = 1 means row and axis = 0 means column.

Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
Axis in Numpy

Dot Product:

First take two array:

a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]])

Find Dot product of this two array:

np.dot(a,b)

Output
array([[37, 40],
          [85, 92]])
Logic: numpy.dot() This function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication. I take two array a and b, and find dot product of this two arrays. 
For more informetion read this official Dot product documentation of Numpy.  Click Here
Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
Dot Function of Numpy

Cross Product:

Find Dot product of this two array:

np.cross(a,b)

Output
array( [-10, -10] )  

Logic: numpy.cross. Return the cross product of two (arrays of) vectors. The cross product of a and b in is a vector perpendicular to both a and b. If a and b are arrays of vectors, the vectors are defined by the last axis of a and b by default, and these axes can have dimensions 2 or 3. 
For more informetion read this official Cross product documentation of Numpy.   Click Here
Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
Cross Function of Numpy

Sorting:

a = np.array([[23,1,345,2,576,234,14], [11,34,253,256,21,67,72]])

np.sort(a) #that means row wise shoring, axis = 1

Output
array([[  1,   2,  14,  23, 234, 345, 576],
           [ 11,  21,  34,  67,  72, 253, 256]])

np.sort(a, axis = 0) # that means column wise sorting, axis = 0

Output
array([[ 11,   1, 253,   2,  21,  67,  14],
           [ 23,  34, 345, 256, 576, 234,  72]])
Logic: Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axiskind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional.
Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
Sorting in Numpy
In Numpy there has different types of sorting algorithms like marge sort, quick sort, etc. To implement this king of sorting algorithms, we used this keywork 'kind'. See the examples below.. 
Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
Marge Sort
Axis, Sorting & Useful Numpy Functions | Numpy & Jupyter Notebook Tutorial 3 | Codeing School
Quick Sort
For more informetion read this official Sorting documentation of Numpy.   Click Here

If you have any quarry, please comment below.

0 comments:

Post a Comment