(DAY 2)Complete Numpy guide

(DAY 2)Complete Numpy guide

NumPy:

Hey guys, welcome back. This is the second day of the "My 7-day journey to Data Science" blog series. You can check out my profile for the previous blogs.

Let's start today's tutorial. In this tutorial, we are going to learn about the Numpy library in Python and its detailed use cases.

INDEX:

  • Numpy array

  • 2D and 3D array

  • Vectorized operations

  • Boolean array

As we all know, Numpy is a numeric computing library in Python. It is one of the most popular and useful libraries for data science. Even though in most scenarios we didn't use Numpy directly, the other libraries that we are going to use such as Pandas and MatPlotLib rely on Numpy for its numeric processing. So it is very mandatory to know about Numpy in detail, before getting into those libraries.

We can import the Numpy library to our editor by using the following command:

import numpy

For declaring and storing values in a numpy array , we can do it like:

a1=numpy.array([100,200,300,400,500])

The above code will create a numpy array named "a1" using the given values.If we need to fetch all the values in that array we can use,

a1

This will fetch all the values from the NumPy array "a1".The output of the same array in normal python will be,

[100,200,300,400,500]

But in numpy, the output will be like this,

array([100,200,300,400,500])

If we need the values in the numpy array based on the index position we can get it by

a1[0]           ///This will return the first value in the array
a1[0,3]      ///Like this we can retrive more than one value at a time
a1[0:-1]        ///The first parameter here is the starting position of the array and the second is the ending position of array this will return all the elements in between the positions but as default it skips the final position we give

There are various datatypes available in python.Even though it's not mandatory to declare the data type, the compiler itself selects the proper datatype for the given values in python. To know the datatype of the given numpy array we can use,

a1.dtype

If we need to declare it explicitly we can do that as follows while creating the numpy array,

a1=numpy.array([100,200,300,400,500],dtype=int8)

Here we declared the datatype of the numpy array "a1" as int with the size of 8 bits. We can use the size of the bit based on our needs such as 8,16, etc.

2D and 3D arrays:

For creating a 2D array in numpy we can declare values like

b1=numpy.array([1,2,3],[4,5,6])

Same as this we can create a 3D array like,

c1=numpy.array([ [ [11,12,13],[21,22,23] ],[ [1,2,3],[1,2,3] ] ])

This will create a 3d numpy array using the given values.

There are various built-in functions for NumPy arrays. Some of them are

b1.shape()  /Gives the shape of the numpy array such as (2,2)for an 2D numpy array

c1.shape()  //Gives the shape of the numpy array such as (2,2,3)for an 3D numpy array
//    .ndim gives the dimensions of the array
b1.ndim      //2
c1.ndim      //3
//    .size gives the total number of elements present in the numpy array

b1.size()    //6
c1.size()    //12
//    .sum gives the sum of the elements
b1.sum()     //21
b1.sum(axis=0)  /Gives the sum of values based on columns
b1.sum(axis=1)  /Gives the sum of values based on rows

There are various functions available such as,

  • sum()-->Gives the Sum of the values

  • mean()-->Gives the Mean of values

  • std()-->Gives the Standard Deviation between the values

  • var(),etc

For selecting values based on index position,

b1[0,0]      /Returns the first element  
c1[0,0,0]    /Returns the first element

Can also use multiple values and position limits like

b1[0,0:-1]
c1[0,1,0:3]

Vectorized operations:

These are used to do mathematical operations on the array elements such as Division, Multiplication, Addition, and Subtraction in a very optimal manner.

a1+10    /Will add 10 to all the elements present in the array a1
a1-10    /Will subtract 10 from all the elements present in the array a1/10
a1*10

Boolean arrays:

Boolean arrays in numpy are used for conditional selections in arrays. Such as for selecting array values based on indexes we can use like

a[[True,False,True,False]]

The above-given command returns the first and third values in the array "a1".

Also if we give

a>=10

This will return an array like

array([False,False,False,True])

This means that the given condition is only satisfied True for the Fourth element in the array. But if we need the array values instead of True or Falses we can use like

a[a[>12])        //This will return the values instaed of Boolean     expressions,The output will be like,

array([14,18])

THANKS FOR READING. Don't forget to follow our blog for more updates.

Other socials:

LinkedIn

GITHUB

Twitter

MEDIUM