Python package Numpy for numerical computation

12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.39
List Price:  €93.41
You save:  €28.02
£55.92
List Price:  £79.90
You save:  £23.97
CA$96.01
List Price:  CA$137.16
You save:  CA$41.15
A$107.15
List Price:  A$153.08
You save:  A$45.93
S$95.13
List Price:  S$135.90
You save:  S$40.77
HK$547.14
List Price:  HK$781.66
You save:  HK$234.52
CHF 63.86
List Price:  CHF 91.23
You save:  CHF 27.37
NOK kr775.40
List Price:  NOK kr1,107.76
You save:  NOK kr332.36
DKK kr487.78
List Price:  DKK kr696.86
You save:  DKK kr209.07
NZ$118.01
List Price:  NZ$168.60
You save:  NZ$50.58
د.إ257.06
List Price:  د.إ367.24
You save:  د.إ110.18
৳7,680.49
List Price:  ৳10,972.60
You save:  ৳3,292.11
₹5,842.03
List Price:  ₹8,346.11
You save:  ₹2,504.08
RM332.86
List Price:  RM475.54
You save:  RM142.67
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,491.96
List Price:  ₨27,846.85
You save:  ₨8,354.89
฿2,586.09
List Price:  ฿3,694.58
You save:  ฿1,108.48
₺2,265.39
List Price:  ₺3,236.41
You save:  ₺971.02
B$363.53
List Price:  B$519.35
You save:  B$155.82
R1,302.64
List Price:  R1,861
You save:  R558.35
Лв127.90
List Price:  Лв182.73
You save:  Лв54.82
₩96,270.48
List Price:  ₩137,535.16
You save:  ₩41,264.67
₪262.29
List Price:  ₪374.71
You save:  ₪112.42
₱4,033.94
List Price:  ₱5,763.02
You save:  ₱1,729.07
¥10,867.12
List Price:  ¥15,525.12
You save:  ¥4,658
MX$1,187.12
List Price:  MX$1,695.96
You save:  MX$508.84
QR254.93
List Price:  QR364.20
You save:  QR109.27
P994.08
List Price:  P1,420.18
You save:  P426.09
KSh9,360.69
List Price:  KSh13,372.99
You save:  KSh4,012.30
E£3,358.63
List Price:  E£4,798.26
You save:  E£1,439.62
ብር4,003.77
List Price:  ብር5,719.92
You save:  ብር1,716.15
Kz58,546.63
List Price:  Kz83,641.63
You save:  Kz25,095
CLP$67,216.99
List Price:  CLP$96,028.39
You save:  CLP$28,811.40
CN¥506.70
List Price:  CN¥723.89
You save:  CN¥217.19
RD$4,073.53
List Price:  RD$5,819.58
You save:  RD$1,746.04
DA9,418.34
List Price:  DA13,455.35
You save:  DA4,037.01
FJ$158.31
List Price:  FJ$226.17
You save:  FJ$67.86
Q543.96
List Price:  Q777.12
You save:  Q233.16
GY$14,650.29
List Price:  GY$20,929.88
You save:  GY$6,279.59
ISK kr9,815.39
List Price:  ISK kr14,022.59
You save:  ISK kr4,207.20
DH707.71
List Price:  DH1,011.06
You save:  DH303.35
L1,237.78
List Price:  L1,768.33
You save:  L530.55
ден4,025.24
List Price:  ден5,750.59
You save:  ден1,725.35
MOP$563.96
List Price:  MOP$805.69
You save:  MOP$241.73
N$1,304.33
List Price:  N$1,863.42
You save:  N$559.08
C$2,570.38
List Price:  C$3,672.13
You save:  C$1,101.75
रु9,397.27
List Price:  रु13,425.24
You save:  रु4,027.97
S/263.43
List Price:  S/376.35
You save:  S/112.91
K270.11
List Price:  K385.89
You save:  K115.77
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,873.89
List Price:  ZK2,677.10
You save:  ZK803.21
L325.37
List Price:  L464.84
You save:  L139.46
Kč1,643.47
List Price:  Kč2,347.91
You save:  Kč704.44
Ft25,458.03
List Price:  Ft36,370.18
You save:  Ft10,912.14
SEK kr764.90
List Price:  SEK kr1,092.76
You save:  SEK kr327.86
ARS$61,327.27
List Price:  ARS$87,614.14
You save:  ARS$26,286.87
Bs483.57
List Price:  Bs690.85
You save:  Bs207.27
COP$273,218.78
List Price:  COP$390,329.27
You save:  COP$117,110.49
₡35,710.66
List Price:  ₡51,017.42
You save:  ₡15,306.75
L1,733.65
List Price:  L2,476.75
You save:  L743.09
₲524,442.73
List Price:  ₲749,236.02
You save:  ₲224,793.28
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł283.24
List Price:  zł404.64
You save:  zł121.40
Already have an account? Log In

Transcript

Hello everyone, welcome to the course of machine learning with Python. In this particular video course, we will see how to use NumPy package in Python for numerical computations. So few properties of NumPy library NumPy is a numerical computation package as I already mentioned, it is why to use package in academia as well as an industry and it is the backbone of the computation in Python for machine learning and artificial intelligence tasks. It is very suitable for fast operations on vectors, matrices and multi dimensional arrays. As you will soon know that this data collectors matrices multi dimensional arrays are very common in machine learning and artificial intelligence. It is very much matches stable and under continuous development, you can find a lot of code written in NumPy all over the internet in the stack overflow and other websites know how to use NumPy.

In order to use NumPy. One has to install it first. So the comment is pip install NumPy so you have to go to your Anaconda prompt and type the command install NumPy it will install the NumPy library into your hard disk. Now as we are using Anaconda distribution which comes with NumPy library pre installed, we did start to install it again so we can skip the first step if we are using Anaconda distribution, the full complete documentation of the NumPy you can visit the website which is mentioned over here. Now we will see how to use NumPy library in Jupiter notebook. Let's open our Jupiter notebook.

So this is the Jupiter notebook I have created to demonstrate how the NumPy library works. This is the reference of NumPy documentation you can go to this reference if you click on this reference, you will be redirected to the website where you will find a lot of manual regarding NumPy Okay, so in this manual, the data type of the NumPy different functions that is built inside NumPy are properly insured and properly written Okay, you can refer to this website. Now how to import NumPy library in order to import like NumPy library command is import NumPy as NP okay, but here this NP is just an alias of NumPy. Okay, that means it is just a reference to the NumPy library we can use any name, let's say your name or my name as the reference, but usually we prefer shorter name as compared to the original librarian.

Okay, so let's go ahead and press Shift Enter. So this padlock means the particular sale is still running that means it took some time to import this NumPy library. Okay, now what are the NumPy arrays? So numpy.md is the default data type of NumPy. Okay, now let's say I want to create a NumPy array which contains only zero elements. Okay, so how to do that.

In order to do that, let's say a equals to NP dot zero, so this NP dot zeros will actually tell us to create an array Okay, now here are the sizes we mentioned three so it will create an array of size three with all zero elements and that will be assigned to a okay If we go ahead and press Shift Enter you can see there is nothing but an array of size three with all zero element now, if we go ahead and type T by P within first bracket a that means I want to know what is the type of this any then it will return NumPy doc in the editor now let's go ahead and type equals to NP dot zeros within bracket three and try to find out what is the type of individual elements. So we will be seeing this shortly how to access individual elements.

So this a within bracket zero means I am accessing the first element Okay, so if I go ahead and press Shift Enter, it will be saying NumPy dot float 64. Now NumPy here is a somewhat like native Python list okay. Now except the only difference between the native Python list and the NumPy array is the data in the NumPy array must be homogeneous that means all elements could be simple and this type must be one of the data is provided by NumPy. Okay, now, what is The data is provided by number it could be 464 in 32, Boolean etc fine now let's say I want to create an array of all zero elements the size of n is three and the data type of the array elements will be integer then we have to just pass on argument detail the cost to it in order to convert it to an integer edit file.

Okay, now let's go ahead and find out how to find out the shape and dimension of a particular okay. So zero equals to NP dot zeros with the bracket 10. So Zed will be an array of all zero elements of size 10 Okay, so here there is a fat at it which no dimension Okay, it has no row or column. Now, the dimension is recorded in the shape attribute which actually returns a tupple. So if I go ahead and run this current status ship, it will return 10 comma Okay, so nothing is mentioned in the column set Okay. Now here the shape tuple has only one element, which is the length of the ad, okay, so to give you the proper dimensions, we can change the shape attribute how just typed into shape equals two within a bracket 10 comma one, okay.

So see, so now it will become a column vector, we can also try some different combinations if say five comma two and go ahead and shifted, so it will become a five cross two matrix. Similarly, we can also do this in two comma two plus two rows and five columns. So, similarly, we can also reshape it to the different types of dimensions as we want. But notice that the number of elements in the array should be comfortable with the row number and column number that means row into columns would be equals to the number of elements in the ad, otherwise it will return some error. So, let's say if I go ahead and try two comma six over here, so, it will say that cannot be shipped the error of size 10 into the shape two comma six because two into six is 12. And the size original size is 10.

So that the row into column should be comfortable with the size of the original file. Now, similar to the zeros we also create empty matrices or empty arrays, which is nothing but zero equals to NP dot empty within a bracket three. So, see if we go ahead and run this the numbers you see here are actually garbage hence, okay, these are not that good that you will always get zero here fine. Now similar to the AP dot zeros there is something called NP dot once which will return an array of all ones fine. So this NP dot wants within a bracket I am passing a topple six comma three that means it will create an array of number of rows equals to see the number of columns equals to three with all ones. OK, let's go ahead and run this file.

Okay, now to set up a grid of evenly spaced numbers we can use NP dot linspace Okay. So here it goes to NP dot linspace. Within a bracket there are three arguments so two comma 10 comma five. So two is basically the starting position. There is basically the ending position and we will be creating even this Paste any of evenly spaced elements, all the elements are evenly spaced, it is like an APC these, the first elements equals to two and lastly we're supposed to take and the number of elements in the series is fine. So let's go ahead and run this again.

So what it will return it will return 246 810 that means, this is basically an AP series with starting value to ending value 10 and number of elements in the series is five. Okay, create an identity matrix. The comment is NP dot identity with parentheses three. So as you can see, it will return a third or identity matrix Okay. Now, we can always convert the least inside the item that is already defined in the Python by to native list into the NumPy array how we have to convert it using the common NP dot A. Okay, so let's say 10 comma 20 is the list Allah, I want to convert it to the Python array using this common NP dot A okay?

Not visit will become an error after that and if you go ahead and find out what is what type of object is determined not by.in file similarly we can create a multi dimensional array from list of list as shown over here okay now we will see how to use array indexes okay in order to demonstrate that I have created an ad which is starting position is one ending position is two and there are five elements Okay, so I have used in this comment, so go ahead and run this cell. Now if I type zero with no graphic zero, it will be returning the first element Now note that like C or c++ in Python also we have zero based indexing that means the first element will be denoted as zero, okay, now this is what is called a slicing if we type Zed within a bracket zero colon two, so it will basically return the first element and the second element.

Okay, let's go ahead and run this. Now. You can also omit this zero and if you run the Python will understand that either test to return the first two elements okay similarly, this will return the first four elements fine similarly, if we go ahead and also specify two colon then what does it mean the starting from starting from the position three, it will return all the elements of tourists Okay. Now, there is something called negative indexing. So Zed minus one will actually return the last element, so that is eight minus two will return the second last element. So, you can also use negative indexing wherever it is completely fine, okay, so similarly, we can also index in the 2d array, but here we have to specify both row as well as the parent.

So, first we specify the row and then we specify the columns zero comma zero will return the first row, first column, okay, so here it is one similarly, zero comma one will return the element which belongs to row one and column two. Okay, so fine. Now let's see, I want to select a particular rule then I can use the comment Zed within a bracket zero comma slides. So, these slides will actually indicate that I have to actually take all the columns that I have specified in the particular column over here. So, that means this column will indicate that I have to select all the columns fine. So, as you have seen that this will return the first row.

Similarly, we can also select a column fine, what we have to do is in the row part we have to mention the colon that is a slice, so, that means I have to select all the rows but only one particular column, okay, so, it will basically return the second column. Fine, good. Okay, now, let's discuss few areas. First, let's say a is some recruiting Edie. Okay, now if we go ahead and type the comment, a dot SOT So you saw the Edit note that need not to do any kind of fancy stuff is for loop in order to start the Edit. So the default we'll talk about that is a dot sock.

Now by default, it will start in ascending order. Okay, so let's say you want to find an eddy which is sorted in descending order, not in ascending order. Okay. So, first you have to sorted in the ascending order as it is mentioned over here and then you can always revert the edit in order to find a descending order sorted fine. Similarly, we thought a dot sum will return the sum of all the elements in the edit a dot mean is the method to return the mean or the average value of the elements in the edit a dot max will return the maximum eliminated the Edit. So, as you can see some difference the maximum element Similarly, a dot arg max will return the position of the maximum element of the Edit okay similarly, we have a.com sum which is nothing but the cumulative summation right.

So, let's say to start from the first element, so, practices similar to the next value will be 36 plus 38 then the next one will be 36 plus 30 plus 42 and so on okay. So, it is basically cumulative sumption. Similarly there is a method called cumulative product okay and we can always find variance and standard deviation as mentioned over here now in the next video we will see how to use macros Libraries flow property in Python. So see you there. Thank you. Meanwhile, you look at this notebook to know more about the NumPy

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.