Python Numpy

Python Numpy

Python Numpy

Numpy stands for Numerical Python and is the core library of numeric and scientific computing.
It consists of multidimensional array objects and a collection of routines for processing those arrays.
Numpy is python's package for doing math that is more advanced than +-*/.
This includes special functions like cosine, exponential, sqrt on top of this we can use NumPy to generate samples from types of random variables.
Numpy also has a powerful data type to define vectors, matrices, and tensors.
with these data types, NumPy also allows us to do linear algebra matrix multiplication and matrix-vector solutions.

Single dimensional array
import numpy as np
n1=np.array([1,2,3,4])
n1
array([1, 2, 3, 4]) 

Multidimensional array
import numpy as np
n2=np.array([[10,20,30,40],[40,30,20,10]])
n2
array([[10, 20, 30, 40],                        
     [40, 30, 20, 10]])  

Initializing Numpy Array 
import numpy as np
n1=np.zeros((2,3))
n1
array([[0., 0., 0.],
     [0., 0., 0.]])
import numpy as np
n1=np.zeros((5,5))
n1
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

Initializing Numpy array with same number 
import numpy as np
n1=np.full((2,4),5)
n1
array([[5, 5, 5, 5],
       [5, 5, 5, 5]])

Initializing Numpy array within a range 
import numpy as np
n1=np.arange(1,5)
n1
array([1, 2, 3, 4])
import numpy as np
n1=np.arange(10,100,5)
n1
array([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90,
       95])

Initializing Numpy array with random numbers 
import numpy as np
n1=np.random.randint(1,100,5)
n1
array([83, 57, 83, 58, 66])

Checking the shape of Numpy arrays 
import numpy as np
n1=np.array([[1,2,3],[4,5,6]])
n1.shape
(2L, 3L)
n1.shape=(3,2)
n1.shape
(3L, 2L)

Joining Numpy arrays 
vstack() 
import numpy as np
n1=np.array([1,2,3])
n2=np.array([4,5,6])
np.vstack((n1,n2))
array([[1, 2, 3],
       [4, 5, 6]])
hstack() 
import numpy as np
n1=np.array([10,20,30])
n2=np.array([40,50,60])
np.hstack((n1,n2))
array([10, 20, 30, 40, 50, 60])
Column_stack()
import numpy as np
n1=np.array([10,20,30])
n2=np.array([40,50,60])
np.column_stack((n1,n2))
array([[10, 40],
       [20, 50],
       [30, 60]])

Numpy Intersection and Difference 
import numpy as np
n1=np.array([1,2,3,4,5,6])
n2=np.array([5,6,7,8,9])
np.intersect1d(n1,n2)
array([5, 6])
import numpy as np
n1=np.array([10,20,30,40,50,60])
n2=np.array([50,60,70,80,90])
np.setdiff1d(n1,n2)
array([10, 20, 30, 40])
import numpy as np
n1=np.array([10,20,30,40,50,60])
n2=np.array([50,60,70,80,90])
np.setdiff1d(n2,n1)
array([70, 80, 90])

Addition of Numpy arrays 
import numpy as np
n1=np.array([10,20])
n2=np.array([30,40])
np.sum([n1,n2])
100
np.sum([n1,n2],axis=0)
array([40, 60])
np.sum([n1,n2],axis=1)
array([30, 70])

Numpy array Mathematics 
Basic Addition
import numpy as np
n1=np.array([1,2,3])
n1=n1+1
n1
array([2, 3, 4])
Basic Multiplication 
n1=n1*2
n1
array([4, 6, 8])
Basic Substraction
n1=n1-2
n1
array([2, 4, 6])
Basic Division
n1=n1/2
n1
array([1, 2, 3])

Numpy Math Functions 
Mean
import numpy as np
n1=np.array([1,2,3,4,5,6])
np.mean(n1)
3.5
Standard Deviation 
np.std(n1)
1.707825127659933
Median
np.median(n1)
3.5

Saving and Loading Numpy array
Numpy Save and Load
import numpy as np
n1=np.array([1,2,3,4,5,6])
np.save('numpy',n1)
n2=np.load('numpy.npy') 
n2
array([1, 2, 3, 4, 5, 6])