NumPy

                    Numpy in Python

NumPy means Numeric Python. It is a package in Python which deals with numeric computation and processing of arrays. NumPy makes easy for us to do the numeric computations. Numpy plays a important role in Machine Learning. There are many inbuilt functions in Numpy, you just need to know when and how to use them. Numpy deals with different ranges of int and float data types. Now lets see how to use NumPy and do the mathematical computations.
Before using NumPy we have to import it. Generally we use alias while importing the library but it is only optional.

Creating Arrays:
import numpy as np
a1 = np.array([2, 1, 3, 4])   # single dimension array
print(a1)

Output: [2  1  3  4]

a2 = np.array([[1, 2, 1], [2, 1, 2], [1, 2, 3]])  # multi-dimension array
print(a2)

Output: [[1  2  1]
                  [2  1  2]
                  [1  2  3]]

By using the same procedure we can make the array of required dimensions.

arange() :
In arange() function we have to give the range between numbers and the interval and it will create the array of that range. Ending range value is exclusive. If we don't specify the interval between values then be default it will be 1. In function final range value is compulsory. By default initial range value is 0.

a3 = np.arange(10, 50, 10)
print(a3)

Output: [10  20  30  40]

linspace():
In linspace() function it needs three values, starting range value, ending range value which is inclusive here and the number of values you need within the range.

a4 = np.linspace(2, 6, 3)   # it means divide the range from 2 to 6 in 3 equal parts.
print(a4) 

Output: [2  4  6]

ones() and zeros():
ones() function will create the array consisting of value 1 only and similarly zeros() will create the array of zeros. These functions will need the dimensions of array we need.

a5 = np.ones((2, 2, 2))
print(a5)

Output: [ [ [ 1  1 ]
                      [ 1  1 ] ]
                   [ [ 1   1 ]
                      [ 1   1 ] ] ]

There are many more functions like these for example empty() will create the array of random values of given dimensions and eye() will create the identity matrix (I hope you know about identity matrix). 

Dimensions and Shape of matrix:
ndim gives the dimensions of array and shape will give the shape of array and size will give the size of array. Now what is the difference among three? Dimensions means 2D or 3D. Shape means the number of rows and columns. Size means total number of elements in the array.

print(a5.ndim)    # 3
print(a5.shape)   # (2, 2, 2)
print(a5.size)      # 8

If we want we can reshape our array. But we need to be careful with the dimensions. For example we have our 1D array above that we want to reshape into shape (2, 2).

print(a1.reshape(2, 2))

Output: [[2  1]
                  [3  4]]

We can use reshape function with arange and linspace to get the array in specified dimensions.

There are so many functions that we can use in Numpy like max() to get the maximum from array, min() to get the minimum from array, sum() to get the total sum of elements from array,  cumsum() whic prints the value equal to the sum of all previous elements in array. There are so many other functions also like sin(), tan(), log() etc. Use the numpy and explore these functions.

Dot product:
To perform the multiplication between two matrix we can use dot() function.

a = np.array([[1, 1], [2, 2], [3, 3]])
b = np.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
print(a.dot(b))

Output: [[2  4  6  8  10]
                  [4  8  12  16  20]
                  [6  12  18  24  30]]

We can use dot product in this way too-
np.dot(a, b)

Numpy is a cool library. Go and explore!


Comments

Popular posts from this blog

Python Dictionary

Loops in Python

Python OOPs