transcribe

Complete Python NumPy for Data Science in 30 minutes | NumPy Python Full Guide

Satyajit Pattnaik · 30m · transcribed 15d ago
More from Satyajit Pattnaik Business
𝕏 Share ▶ YouTube 📥 PDF 🤖 .md

Section Insights

# 0:00

Introduction to Python for Data Science

Why is Python important for data analysis and data science?

Python is crucial for data analysis and data science due to its extensive libraries like Pandas and NumPy that simplify tasks in machine learning and deep learning.

  • Python programming is essential for data science.
  • Libraries like Pandas and NumPy enhance productivity.
  • The video will focus on NumPy as a foundational tool.
# 5:59

Creating Arrays in NumPy

How do you create arrays in NumPy?

You can create arrays using the np.array() function, and specify data types such as integers, floats, or strings.

  • NumPy arrays can be one-dimensional or multi-dimensional.
  • Data types can be specified when creating arrays.
  • The length of an array can be determined using the len() function.
# 11:59

Understanding Array Shapes and Data Types

What are the shapes and data types of NumPy arrays?

The shape of an array indicates its dimensions, and you can check the data type of an array using the .dtype attribute.

  • Shapes define the structure of arrays (e.g., rows and columns).
  • Data types can be checked easily with .dtype.
  • Understanding shapes is crucial for data manipulation.
# 17:59

Using Zeros and Ones Methods in NumPy

What are the zeros and ones methods in NumPy?

The np.zeros() and np.ones() methods create arrays filled with zeros or ones, which can be useful in various scenarios.

  • Zeros and ones methods are fundamental for initializing arrays.
  • You can specify the data type when creating these arrays.
  • The full() method allows creating arrays with a specific value.
# 23:59

Slicing and Indexing in NumPy

How do you slice and index arrays in NumPy?

You can slice arrays using a combination of indices and ranges to access specific rows and columns.

  • Slicing allows for flexible data access in arrays.
  • Negative indexing can be used to access elements from the end.
  • Understanding slicing is key for effective data manipulation.

Transcript

0:00 hey guys welcome back to my channel my name is satijit patnaik in this video we shall be talking about how python can be used for data analysis and data science basically we already know that python programming is very important and we also know that there are multiple libraries which helps and which eases up our work when we start getting into machine learning deep learning nlp all these topics in one of my previous videos i also talked about pandas where i explained

0:31 each and everything about pandas in 30 minutes in this video i'll be focused on the second library which is numpy well numpy is pretty basics many of you might already be knowing it but there are so many things which you might not know which you definitely get to know in this particular video so this is an end-to-end numpy guide probably could take around 20 25 or 30 minutes to finish but i'm sure you will be benefited out of it and after this the

1:02 immediate next video will be published within a week or two weeks which will be on matplotlib and c bond and that's where our journey starts into machine learning we'll also upload more and more machine learning videos in this channel please like share and subscribe the channel press the bell icon to get notified and now let's get started with the video hi welcome to this course on python and

1:34 in this video we shall be talking about one of the most important library this is nothing but numpy well what exactly is numpy num stands for numerical and pi stands for python and this library is used for all the numerical operations and this library is used mostly for the data analytics and data science activities which we do for an example you are extracting some data you are reading a data now we

2:05 already know that we read data using pandas library and after reading data your data could have categorical features it could have numerical features as well now how can we deal with the numerical features sometimes we might have to do some transformations on numerical features so there could be a lot of activities which can be done on numerical features or numerical variables in that case numpy is used well numpy can be used to store data of

2:36 different forms it could be one-dimensional two-dimensional or n-dimensional but the question is why do we use numpy because traditionally speaking we already have gone through various for loop conditions we know what is list comprehension we know what is map functions now these kind of things can also deal with numerical variables why do we use numpy it all depends upon the type of data and if you are dealing with big data

3:08 your traditional approaches might take a lot of time numpy is way faster as compared to the traditional approaches you can see in this graph the time taken for various loops plane for loop takes the maximum amount of time because it's a very traditional approach it goes through each and every item iteratively then comes list comprehension then comes map function and numpy is seen or has been experienced to be the most

3:40 effective when it comes to time taken now what is numpy we already discussed the basics it's a library used for computation and processing of single and multi-dimensional arrays of elements smaller memory consumption and better runtime behavior installation of numpy is very simple these days with the latest python versions numpy pandas matplotlib they come predefined like they are pre-installed in those python modules

4:12 itself but in worst case if numpy is not installed when you are importing numpy library and you are getting an error you can simply install using the command pip install numpy you can run this command over your command line in your anaconda prompt jupyter notebook consoles or your spyder notebook consoles and after this your numpy will be installed and you are ready to start installing or start using the numpy liability so this is all about the theoretical

4:43 concepts we'll jump onto the practicals part now as we already talked about numpy numpy is nothing but a python library and it stands for numerical python we already discussed that it is used for working with arrays it is used for computation and processing of single and multi-dimensional array of elements and smaller smaller memory consumption and better runtime behavior to install numpy you have to do pip install numpy and then start using by importing the

5:15 library but usually whenever we import numpy we do it like this import numpy as np so that we don't have to call numpy numpy numpy again and again so np acts like an alias here as np so whenever you import numpy you can simply do np dot your method name numpy will be called so there is a funny meme i don't always import numpy whenever i do i do it as np image courtesy goes to

5:48 memegenerator.net from where i extracted this image now let's get started with the practical spot so here i'm creating an array so how to create an array it's very simple creating an array okay so none numpy which is np dot array one two three so these are the three elements and here i'm doing print of a if i run this okay numpy is not imported

6:20 so we'll import this and then we'll run this now you can see this is what my a looks like it's an array if you want to see what is the type of it type will be numpy array you can see numpy dot nd array what is this this is nothing but n dimensional array okay this is the type of numpy array so it could be a one dimensional array it could be a two dimensional array let's say you're defining like this

6:47 so this will also be an array but it's a two dimensional array the type will remain the same it's going to be nd array but the interpreter already knows that it's a two dimensional array now let's say i'm defining the same data like the same numbers one two three but i want to define it as float objects here it is defined as integers what we can do is we can simply pass d type equals to float which basically

7:17 means data type equals to float if i run this now my a looks like this now if i want to create data type as string will this work or str will this work yes it will work you can see the three elements are string objects one two and three now if you want to find the length of an array you can simply type len function length of your object and then it shows you three that means

7:48 it has three elements now let's say i am defining a two dimensional array you can see one dimension your first element is 1 2 3 second dimension 4 5 6 like the second element if i run this this is how my b will be now if you want to type the length of b length of b it will be two why because there are two objects each object is a list so you are you are

8:18 forming a list of list so that is the reason the length of b is two here first and second okay it will not be like one two three four five six if you don't define like this if you define it this way let's say like this sorry 6 and in this case if i want to length of b it will return you 6 because there are 6 elements but here there are 2 elements 1 and 2.

8:50 ok similarly how to type the type type of b here i am creating another array and this time i am going to create three objects three elements one two and three if i run this and now if i run length of c it will return me three now one thing is here let's say my a is one two three my type is nd array but you want to see

9:23 how much dimensions your type is like how much dimension does this array has what you can simply do is you can simply type in dim of a it will give you sorry my bat a dot n dim okay a dot end in if you run this it shows you that this is a two dimensional array similarly we can run it here c dot endim

9:53 you can see c is of two dimensions right there are three elements one two three four five six seven eight nine so the dimensions of c is nothing but two similarly dimensions of b is 1 what is b b is this one right so to check the number of dimensions you use the object dot endim similarly i am creating a four dimensional object one two three four and then if i do d dot ending it is

10:24 returning me four why use numpy well somehow we already discussed this the main reason why we use is it's having a faster computation time it is way too faster as compared to the previous approaches so in traditional python programming we have lists that serve the purpose of arrays but they are slow to process because so far when we created all these arrays you might be thinking that we

10:55 already have lists in traditional programming we already have tuples why don't we use that why do we have numpy so in traditional python programming we have lists that serve the purpose of ares but they are slow to process numpy aims to provide an array object that is up to 50 times faster than traditional python lists and this is what they this is what they claim numpy claims but

11:26 with personal experience i can say that it's way too faster as compared to the traditional lists i haven't calculated whether it is 50 times faster or not but it is way too faster but this is what numpy library claims they claim to be 50 times faster than the traditional python lists the array object in numpy is called nd array n dimensions array it provides a lot of supporting functions that make working with ndra

11:57 very easy there are so many methods inside your numpy library that you will be lost i will not be able to cover each and everything but i will try to cover as much as possible as much frequent methods that we use on day-to-day activities i will be covering all those basic methods arrays are very frequently used in data science where speed and resources are very important because when it comes to data analytics and data science we deal

12:28 with huge amount of data right big data so in that case your traditional programming will not be able to help you out so numpy is mostly used can be used to store 1d 2d 3d and so on time taken for the loop we have already gone through these things now we'll talk about some other methods there is one more method called as shape shape basically tells you how many rows and how many columns do you have now

13:00 let's say your a is like this so the shape is one comma three okay your b is like this the b dot shape is going to be six because you have six elements here you have one element inside one element you have three elements right so your shape is one comma three c looks like this so c will have two comma three sorry three comma three because there

13:31 are three elements my bad three elements and within these three elements there are three entities okay so if you want to find the data type of a particular numpy variable you can simply type in a dot d type or else b dot d type this will tell you the data type okay so moving on to the next one let's say i have an array 1.1 2.1 3.4

14:04 and here i'm printing it so these are the basic things which we have already covered now the next important thing in numpy will be accessing or changing specific elements rows and columns so these are some concepts which we are going to write now once we jump into some real time data data some real time projects then you will have a better understanding on where and how to use numpy operations okay in this video we are just focusing on

14:35 the various methods that we have inside numpy now accessing or changing specific elements rows and columns here i've defined a equals to numpy dot array something 1 2 3 4 5 6 7 8 9 something something if i print my a this is how my a looks like what is a a has two dimensions right what will be the shape of a

15:05 it's going to be two comma one two three four five six seven two comma seven okay now let's say you want to get a specific element we will be using 0 comma 3 what is 0 comma 3 you can see 0 is basically means the first index python indexing starts from 0 and then 3 that means 0 1 2 3. so 0 comma 3 is basically 0th

15:37 entity inside that 0th entity you are getting the third index so 0 1 2 3 now if you want to print 12 what it will be if you want to print 12 so it will be 1 1 0 1 2 3 4 it should be 1 comma 4 if you print then it is showing you 12 right what will be 1 comma zero it will be

16:08 eight first index so zeroth index first index and inside first index the zeroth index one comma four so it's super simple it's very simple to understand get a specific row let's say out of this you want to print the entire row that means 0 colon nothing that means all the entities from the first row 0 colon this one you want a specific column let's say you want this column 3 10

16:39 so colon 2 something like this colon 0 is basically printing you 1 8 if you want to do colon 2 it will print you 0 1 2 so 3 and 10 it will print you 3 and 10 where is column 2 3 and 10 ok so this is how you access your different operations now this is your array let's say you want to access some elements and you want to

17:10 change the values let's say you want to change this attribute what is this attribute you can simply access this attribute by 0 0 1 2 3 0 comma 3 this is 4 now if you do this equals to 40 like i am doing here in this line if i do this and then print a you can see the elements have changed so numpy arrays are mutable in nature you can change it

17:42 now change the fifth column in first row to 50 simply fifth column and first row so zeroth index zero fifth column so zero one two three four zero comma four equals to fifty after this we'll we'll be talking about some zeros and ones method in numpy there are some zeros and num ones method so in case you want to create a numpy array with all zero entities like all the

18:15 elements will have zeros why do we do that there are specific scenarios where we use these kind of logics so we do have some functions like zeros ones and something like full now if i call numpy dot zeros then i call the dimensions three comma three that will give me a three comma three matrix so a three cross three matrix with all 0 entities you can see if i am printing it i am getting the values as 0 0 0 0 0 but

18:48 these values are in float format how can we change it to integer you can change it like this oh zeros sorry my bad spelling mistake now you can see all have integer values is string possible well string is also possible but it's not showing you any value because 0 is basically null in

19:19 terms of string right similarly if you want to print a 3 cross 3 1 matrix you can do np dot once and if you want to have the data type as integer pass the d type equals to int 32 if you are not passing in 32 or passing end that also works okay so this or this both are same okay similarly float 64 or float both are

19:50 same now after zeros and ones the next method is going to be full what is full pull is let's say you want to create a matrix of a single number all the entities will have the same number in that case you can use np dot full you are passing i need a 3 cross 3 matrix and the value should be 50. or else let's say you are defining a variable where var equals to 20

20:22 and then you are passing this value see 20 if you are changing it to integer it will have integer something like that so if you pass it or you hard code it it's going to be same so np dot full creates a full array of a number which you pass if you want to create a numpy error of a random number you can pass dot random dot rand comma four comma four so this will give you a random matrix and all these

20:54 numbers inside your numpy array will have a number between zero to one but if you want a random integer you can do dot random dot rand end here it is float and here it is random here i'm passing minus 4 1 which means i want in the range of this if i don't pass this it will fail because rand int takes at least one positional argument so if you pass this which basically

21:25 means i want the random values to be in the range of minus four to one let's say one to five and your values will be within one to five one two hundred will be within one two hundred right let's say i want from my minus five to zero so minus 5 to 0 and then we have another function called as identity i hope everybody knows about identity matrix 0 1 0

21:55 1 this is identity matrix i hope everybody knows about this now to create an identity matrix you pass np dot identity 5 np dot identity 3 which will be 0 1 0 0 0 1 0 0 zero one identity five will be one four zeros zero one three zeros zero one zero zero zero zero zero one zero zero zero zero zero one okay this is the identity matrix now there are some practice questions to

22:26 talk about let's say extract all the first three rows of the last five columns in a given numpy array a so this is the array which has been given okay this is not the array this is the array okay 1 2 3 4 5 6 10 20 something something something extract all the first three rows that means all these three rows of the last five columns

22:56 okay first three rows of the last five columns so your output should be like this okay so that was the output that was mentioned output should be this then this then this this should be the output okay how can we do that so here we have defined my a my a looks like this now how many rows do we need we need the top three rows

23:28 so zero one two three four minus one minus two minus three minus four minus 5 i hope everybody knows about the indexing in python your indexes start from 0 from the beginning and from the end it starts from minus 1 right so similarly what i'm doing is 0 1 2 3 4. so 0 1 2 3 so here i'm passing 0 colon 3 that means colon 3 and i'm passing minus

24:00 5 colon something so here if i run this it is throwing me the output which is expected like this okay 0 1 2 3 4 5 minus 1 minus 2 minus 3 minus 4 minus 5. so you can see from minus 5 till end till minus from minus 5 till end and here till 3 that means 0 1 2 0 1 2 3 okay so this is for the rows and

24:31 this is for the columns we want 3 rows and we want this much and if you want to do this will this work like i want to do 0 comma 3 this will also work so blank is nothing but zero okay if you just want to print the first three rows this is how we do the first three rows right a zero colon three from the first three rows you want the last five columns

25:03 so mine zero one two three four five from end minus one minus two minus three minus four minus five so here i'm doing minus five colon minus 1 so this will also work oh minus 1 doesn't work okay minus 1 empty my bad my bad so there should be a comma here minus 5 colon minus 1 so this will also work and if you are not passing anything the end attribute

25:33 here and here this will also work second question is given a positive number n greater than 2 create a numpy area of size n cross n with all zeros and ones such that the ones make a shape of plus so the input is 3 your output should be like this that means you can see the 1 is formed as a plus 0 1 0 1 1 1 0 1 0. it's forming a plus right if you are

26:03 passing 5 this is forming a plus right so the middle let's say you have five rows the middle row will have all the values as one and the middle column will have all the values as one right so let's say i'm passing n equals to five here i'm creating a numpy dot zeros so all my entities are zeros now here i am passing n slash slash two colon equals to one and colon n slash

26:35 two what is slash class i hope everybody knows about this let's say 5 slash 2 is nothing but 2 right 4 slash 2 will be 2. so 100 divided by 5 is 20 right 101 divided by 5 is 20 point something so your absolute value is basically this one this is basically your operation now here what i am doing is 5 slash 2 is

27:06 basically 2 so i am passing 2 comma colon and then colon comma 2 so for all the middle rows and the middle columns it should be 1 and now if i print this my middle rows and my middle columns are 1. so these are some basic operations apart from that there are some arithmetic operations as well this is another exercise we'll be leaving this document to you so that you can practice there are some various arithmetic

27:37 operations which we should talk about as well so let's say here we have defined and numpy array np dot array1234 let's say you want to add two numbers like you want to add two with each and every element how can we do that simply do a plus two so a plus two is nothing but all the entities are added with 2. similarly a to the power of 3 that means

28:07 1 to the power of 3 is 1 2 to the power of 3 is 8 so 3 to the power 3 is 27 if you print this so each and every entity in your array is getting that mathematical operation it is doing that mathematical operation so we also have mean max all these functions as well in array let's say a dot main will give you a dot main np dot mean a so both these operations are giving you the minimum values

28:39 we also have a dot max or numpy dot max a a dot mean will give you 2.5 so 1 plus 2 3 3 plus 3 6 6 plus 4 10 divided by 4 2.5 so the mean value is 2.5 you can also calculate mean using np dot mean of a okay similarly we have created another two dimensional array a dot n dim

29:10 it's two dimensions what will be max element max element is eight max of a is eight a of max is eight both are same so these are some numpy operations arithmetic operations that you can perform and it's all about practice the more you practice the more you understand about these concepts and when and where to use this numpy libraries and pandas libraries it all depends upon your practical scenarios when we start talking about an use case which we have

29:43 covered in this course we will be explaining you where and how to use numpy pandas and various other libraries that's all about numpy's in the next video we shall be talking about matplotlib and seabourn libraries thank you

Summary

This video by Satijit Patnaik focuses on the NumPy library, a crucial tool for data analysis and data science in Python. It provides an in-depth guide on NumPy's functionalities, including array creation, manipulation, and performance advantages over traditional Python lists.

- NumPy stands for Numerical Python and is essential for numerical operations in data analytics and data science.
- It supports single and multi-dimensional arrays, offering faster computation times compared to traditional Python lists.
- The library is easy to install using `pip install numpy` and is often imported as `np` for convenience.
- Key functionalities include creating arrays, accessing and modifying elements, and using methods like `zeros`, `ones`, and `full` to generate specific types of arrays.
- NumPy arrays are mutable, allowing for changes to specific elements and supporting various data types.
- The library provides functions for mathematical operations, such as calculating mean and maximum values, and facilitates efficient handling of large datasets.
- The video emphasizes the importance of practice in mastering NumPy and hints at future content on Matplotlib and Seaborn for data visualization.

Questions Answered

Why is Python important for data analysis and data science?

Python is crucial for data analysis and data science due to its extensive libraries like Pandas and NumPy that simplify tasks in machine learning and deep learning.

How do you create arrays in NumPy?

You can create arrays using the np.array() function, and specify data types such as integers, floats, or strings.

What are the shapes and data types of NumPy arrays?

The shape of an array indicates its dimensions, and you can check the data type of an array using the .dtype attribute.

What are the zeros and ones methods in NumPy?

The np.zeros() and np.ones() methods create arrays filled with zeros or ones, which can be useful in various scenarios.

How do you slice and index arrays in NumPy?

You can slice arrays using a combination of indices and ranges to access specific rows and columns.

© transcribe · For agents Built with care and craft by Gokul Rajaram