Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

NumPy in DataScience:-
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

It is a predefined library of python which is used to perform a mathematical operation using predefined methods.

NumPy uses array type data to perform the operation.

it contains array type data to accept input data.


Operation using NumPy:-

Operations using NumPy
Using NumPy, a developer can perform the following operations −
⦁    Mathematical and logical operations on arrays.
⦁    Fourier transforms and routines for shape manipulation.
⦁    Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.

How to install NumPy:-

before installation of NumPy python, pip and python environment paths should be set.

open Command Prompt and Type:-

pip install NumPy



Numpy array:-

1) IT is called NDArray, which means we can create one-dimension,two-dimension, and multidimensional array using Numpy.

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


Numpy Operation:-

Numpy provides a various predefined method to manage array operation

1) ndmin:-  it is used to convert array to multiple dimension

a = np.array([1,2,3,4],ndmin=2)


2) dtype:-  It is used to convert array elements to a different type of elements.

a = np.array([1,2,3,4],dtype=complex)

3) shape :-  it will return number of rows and column

print(a.shape) //


a complete example of NumPy:-

import numpy as np

#a = np.array([1,2,3,4],ndmin=2)
a = np.array([[1, 2], [3, 4]])
#a = np.array([1,2,3,4],dtype=complex)

print(a)
print(a.shape)

4) numpy.zeros():-  this method will create zero values array element

 import numpy as np
print(np.zeros((2,2)))


5) numpy.ones():-   this method will create one values array elements
 import numpy as np
print(np.ones((2,2)))


6) numpy.reshape():-  it is used to transpose the matrix or multi-dimension array.

   for example, if the matrix is 2*3 then it will convert into 3*2

   numpy.reshape(a, newShape, order='C')

import numpy as np
e  = np.array([(1,2,3), (4,5,6)])
print(e)
#e.reshape(3,2)
np.reshape(e,(3,2),order='C')

7) fllatern :-  It is used to display array in ColumStyle, Multi to single dimension array we will use flattern.

numpy.flatten(order='C')
 
import numpy as np
e  = np.array([(1,2,3), (4,5,6)])
e.flatten() 

8) hstack() and vstack():-

hstack():-  it is used to append array data horizontally

a = np.array([1,2,3])
b = np.array([4,5,6])
c= numpy.hstack((a,b))

vstack():-  it is used to append array data vertically

a = np.array([1,2,3])
b = np.array([4,5,6])
d = numpy.vstack((a,b))

Program to implement vstack and hstack:-
import numpy as np
a  = np.array([1,2,3])
b = np.array([4,7,8])
c = np.hstack((a,b))
d= np.hstack((a,b))

9) numpy.random.normal():-  It is used to display random number based on start,distance and last index.
numpy.random.normal(loc, scale, size)
Here
  • Loc: the mean. The center of distribution
  • scale: standard deviation.
  • Size: number of returns
## Generate random nmber from normal distribution
normal_array = np.random.normal(5, 0.5, 10)
print(normal_array)   
[5.56171852 4.84233558 4.65392767 4.946659   4.85165567 5.61211317 4.46704244 5.22675736 4.49888936 4.68731125]   


10) linspace() :-  it is used to provide sequence of data using starting point ,ending point and size.
     
      numpy.linspace(start, stop, size, endpoint)

It is used to subdivide the range data based on size


example of linespace():-

import numpy as np
a=np.linspace(0.0, 1.0, num=6)
print(a)
,........................................................................................................................

LogSpace

LogSpace returns even spaced numbers on a log scale. Logspace has the same parameters as np.linspace.

numpy.logspace(start, stop, num, endpoint)
 
import numpy as np
a=np.logspace(3, 4, num=3)
print(a)
 
it will provide 10**3 and 10**3.5 , 10**4   10 is the default base of log
 .................................................................
 
 
Indexing and slicing in Numpy:-
 
Indexing is used to display particular element of numpyarray.
 
slicing is the show data of particular range
 
 
WAP to display first row of numpy array?

import numpy as np
e  = np.array([(1,2,3), (4,5,6)])
print(e[0])
 
 
Another example of slicing :-
 
 
import numpy as np
e  = np.array([(1,2,3), (4,5,6)])
print(e[:2]) 
 
 

NumPy Statistical Functions with Example:-

 it is used to provide in-built() to implement statistical operation using min,max,deviation,variance,median,mean
 import numpy as np
normal_array = np.random.normal(5, 0.5, 10)
print(normal_array)  
print(np.min(normal_array))

### Max
print(np.max(normal_array))

### Mean
print(np.mean(normal_array))

### Median
print(np.median(normal_array))

### Sd
print(np.std(normal_array))
 


 ........................................................................
 
 
Numpy dot product in python:-
 
 
It is used to cross multiply matrix row element .
 
 
 
numpy.dot(x, y, out=None)
 
 
 
f = np.array([[1,2],[3,2]])
g = np.array([[1,2],[3,2]])
### 1*4+2*5
np.dot(f, g) 
 
 
NumPy Matmul() :-  it is used to multiply matrix element with row element to column elements.

h = [[1,2],[3,4]]
i = [[5,6],[7,8]]
### 1*5+2*7 = 19
np.matmul(h, i)
 

Determinant

Last but not least, if you need to compute the determinant, you can use np.linalg.det(). Note that numpy takes care of the dimension.
## Determinant 2*2 matrix ###

 i = [[5,6],[7,8]]

        5*8-7*6

np.linalg.det(i)
 
 
 np.linalg.matrix_rank(A)):-  It return number of columns in matrix 

 np.trace(A))  :-  It return sum of main diagonal of matrix

print(np.linalg.det(A)) :-  It return determinant of matrix   
print(np.linalg.inv(A)) :-   It provide inverse of matrix
print( np.linalg.matrix_power(A, 3))  :-  It provide multiplication of  matrix



Example of matrix operation:-

import numpy as np
 
A = np.array([[2, 1, 1,4],
              [4, -2, 5,5],
              [2, 8, 7,5],
              [2, 8, 7,3]])
 
print(np.linalg.matrix_rank(A))

print(np.trace(A))
print(np.linalg.det(A))
print(np.linalg.inv(A))
print( np.linalg.matrix_power(A, 3))



Numpy Eigen Function Example:-
This function is used to return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.

eigh() :-  Returns two objects, a 1-D array containing the eigenvalues of a matrix, and a 2-D square array or matrix (depending on the input type) of the corresponding eigenvectors (in columns). 

from numpy import linalg as scs

c, d = scs.eigh(a)

print("Eigen value is :", c)
print("Eigen vector  is :", d)

numpy.linalg.eig(a) : This function is used to compute the eigenvalues and right eigenvectors of a square array.

import numpy as np
from numpy import linalg as scs


a = np.diag((1, 2, 3))

print("Array is :",a)

# calculating an eigen value
# using eig() function
c, d = scs.eig(a)

print("Eigen value is :",c)

print("Eigen vector is :",d)


Numpy DateTime Function:-

var = np.datetime64('2017-02-12')






Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post