All you need to know about Numpy :

imaginor-labs 15 June 19
Technology Comments

This is the continuation of series of blog post on getting started to data science using python . So today will explore the building blocks of python with special emphasis on numpy .

Most frequently used python library for data science community . Why it is being used so extensively , we will find it in this blog post .

Numpy stands for ‘Numeric Python’ , it is the core library in python to do the scientific computing . It is the container of tools and technique to compute mathematical model of science .

Multidimensional array object is one of the powerful data structure for computation of arrays and matrices .

Enough of the theory lets dig deeper .

If you are following us for a while you might have already installed ananaconda which comes pre-loaded with numpy !!

imag1

A numpy array comes with 2 important state variables. Just like Python, it automatically detects dtype (if not mentioned)

  • dtype
  • shape

Lets look into an example :

imag2

Initialization of NumPy via arrange()

imag3

Initialization with Zeros and 2d array

Numpy stores matrix in a row major format i.e the entire row will be stored first and then the second row will be stored .

imag4

Similarly you can have it for ones too :

imag5

More on Example :

imag6

In other words, you see that the result of x-y gives the shape of (3,4): y had a shape of (4,) and x had a shape of (3,4). The resulting array is formed from the maximum size of the array .

Initialization with Random Values

numpy.random.random(size=None,)
numpy.random.randint(low, high, size=None, dtype='I') : The value of matrix lies between low and high
numpy.random.randn(d0,d1,⋯,dnd0,d1,⋯,dn)

imag7

Slicing and index in Numpy :

This is a very important concept in numpy while we can used it for a variety of usage . This operation is very similar to list operation where you can index and slice using square [ ] .

Just like list, numpy also has 0 indexing. Let us see some of the commonly used slicing techniques .

  • Generic Slicing Operation : [start]:[end]:[jump]
  • Only jump ::2
  • Only end :5/li>
  • Start and jump 2::-1
  • End and Jump :5:2
  • Start, end and jump 2:7:3

imag8
imag9
imag10

Solving Mathematical problems using numpy :

Numpy has a wide range of function to do mathematical operations :

np.add(), np.subtract(), np.multiply(), np.divide() and np.remainder().

imag11

Broadcasting in numpy :

It is a way to broadcast data of lower or same dimension onto another ndarray. It is similar to map operation in python.

imag12
imag13

Matrix Operation :

One of the very important operation to look out for as its being extensively used for image processing , vector calculation and a lot more :

  1. Inner Product
    • Vector Vector
    • Matrix Vector
    • Matrix Matrix
  2. Outer product

Inner Product

In terms of Inner Product or Dot product, will be be nothing but sum of element wise product. So, the dot product between vector v=[1,2,3]v=[1,2,3] and w=[2,4,6]w=[2,4,6] will be
1∗2+2∗4+3∗6
2+8+18
28
The outer product work as inverted L :
Lets look at few example :

imag14
imag15
imag16

Statistical Function in Numpy :

  1. np.mean(data,axis=0)
  2. np.var(data,axis=0)
  3. np.sum(data,axis=0)
  4. np.max(data,axis=0)
  5. np.min(data,axis=0)
  6. np.percentile(data, percentage,axis=0)
  7. np.std(data,axis=0)

imag17
imag18

Reshape and resizing of matrix :

Resizing of array is done to make combability of two operations so that the array are of same dimensions or different dimensions the element here gets changed .

imag19

Reshaping in array :

Besides resizing, you can also reshape your array, you give a new shape to an array without changing its data. Only key of reshaping is keep the size of the new array unchanged .

imag20
imag21

Transpose Operation :

numpy.transpose(a, axes=None)
This function reverses the axes for 2D array.
For multidimensional array, it permutes the matrix according to the axis argument.

imag22

This are some of the main function of NumPy , you can even use it for doing small visualization task .

There are lot can be done with numpy , will explore more of it in explanatory data analysis .

Stay tuned and happy learning !!

Comments:

Add a Comment:



Recent Posts: