Hello everyone and welcome to the PI arrays lecture and this lecture will learn the various ways you
can create a higher rate using Python and then some PI library.
Let's go ahead and jump to the Jupiter note to get started.
All right.
Here we are at the Jupiter notebook arrays are the main way we're going to be using the name PI library
throughout the course and I'm high res essentially come in two flavors that come as one dimensional
vectors or two dimensional matrices but they can both be called arrays.
If there are one or two the let's go out and show how we can create a new umpiring from Python objects
such as a list I'm going to go and create an object to my list.
One two three.
And if I go ahead and just display that list.
Notice I get out one two three and it's just a python list.
I'm going to go ahead and say import pi as P and then I can call and pede array.
And now I can cast that list as an array.
And notice I get an array back as the container for that list object.
And then I can actually assign this and then just call array off of that.
So I can cast a normal Python list into an array.
Now will return back a one dimensional array if I want to get a two dimensional array or a matrix.
I can cast a list of lists.
So for example I can say my underscore maths is equal to a list of three lists so one two three four
five six get rid of that extra comma and then 7 8 9.
So notice I get back a list of lists and then if I cast that to an array I will get back a two dimensional
array and that's noted both by the way it's presented as output but also more importantly as the number
of brackets shown on the closing and opening ends of this array.
I have two sets of brackets which will indicate that Z to that mentionable array has three rows and
three columns and that's how you can cast normal Python lists or list of lists to umpire range.
Usually however you'll probably be using some pie's own builtin generation methods to actually create
arrays a lot faster and a lot simpler.
Let's go ahead and go through some of the most common ways to do that.
The most common way will probably be creating a higher rate is using arr. and this is really similar
to Python's own built in range function.
You'll go ahead and say N.P. arrange and then pasan a start and stop.
So notice you have a start a stop and then a step size is a third argument and the return evenly spaced
values within this given interval.
So if I just say from 0 to 10 remember just like in Python's range indexing starts at zero.
It will go up to but not including to 10.
I'll get an array back from 0 all the way to 9.
So I start at 0 and I go all the way up to but not including 10.
So I get 10 digits back 0 1 2 3 4 5 6.
Essentially works just as Python's own range function.
So I want to go from zero all the way to 10 after Z.
I have to say zero comma 11 and that's what I get back out.
And then finally I can add a third argument a step size.
So if I were all the even numbers from 0 to 10 I could say zero come 11.
And then at a step size of two so that I'd jump in steps of 2 0 2 4 6 8 10.
That means a range is going to be one of the most useful functions for quickly generating an array using
them pi.
If you want specific types of arrays there's other functions in non-pilot that can help us do that.
For instance if you want to generate arrays of all zeros we can say P-doc zeros and you can pass in
a single digit.
So you get a one dimensional vector out or you can say N.P. that zeros and pass a tuple of the dimensions
you want the first number is going to represent the number of rows and the second number in that tuple
is going to represent the number of columns.
So for instance I want to rows by three columns I pass in the tuple to come a three and I get back to
Rose by three columns and this is noted by the brackets.
All right if I want a raise of just pure ones I can say that ones.
And similarly I can passen either a single number or a one dimensional array or a tuple of them mentions
for a two dimensional matrix that's ones and zeros in other really useful built in function is lens
space and space returns evenly spaced numbers over a specified interval.
It's important to not confuse Lewan.
Arrange your we can see that a range basically returns integers out from a start and a stop and a given
step size.
What Lin space is going to do is it's actually going to take in a third argument of the number of points
you wants me going to show you what I mean by that I can say P Gotland space and I get a pass and a
start a stop and there's a third argument I'm going to pass in a number.
So let's say I have a start of zero and a stop of five and I want to get 10 evenly spaced points between
zero to five.
I can pass in a third argument 10 and this returns back a one dimensional vector noted by the once set
of brackets here of ten evenly spaced points from zero to five.
And I can change this third argument to whatever number I want.
So if I want 100 evenly spaced points from zero to five I can just pass that in and that returns a much
larger one dimensional array.
And keep in mind even though it looks like this is to them as far as the way it's being printed out
you can tell it's one dimensional by the fact that there's only one bracket here in front of the array
when you're dealing with two dimensions you'll see that you have two sets of brackets.
On opening or closing at the ends.
Three that I mentioned will have three brackets etc..
All right.
So that's when space.
Again don't confuse space with arrange lint space will take in the third argument as the number of points
you want arrange will take in that argument as the step size you want let's go ahead and show how you
can create an identity matrix using pi.
We can say n p e y e.
And then you just pasan a single digit in identity matrix.
If you're not familiar is a useful matrix when you're dealing with linear algebra problems.
And it's basically just a two dimensional square matrix meaning the number of rows is the same as a
number of columns and you have a diagonal of ones that everything else is zeros and that's the reason
it only takes a single digit as an argument because it said that the matrix must be square as the output
and the P i.
All right moving along now pi also has lots of ways to create arrays of random numbers.
And we're going to use that often throughout the course so let's go ahead and cover a few ways to do
that.
What you're going to go ahead and do is call N.P. random dots and then ran the method.
You could tab you'll see that there's actually quite a bit of methods.
So we're going to go in and show you the most common ones.
The first one is Ran's are a and b.
And this is going to create an array of the given shape you pass in and it's going to populate it with
random samples from a uniform distribution over 0 to 1.
And that means if I want just eight one dimensional array of random numbers uniformly distributed from
0 to 1 I can pass in a single digit and I'll get back that one dimensional array.
I want this to be to that mentionable.
Unlike the previous functions you worked with instead of passing in a tuple.
I'll actually just pass in the dimensions as separate arguments.
So that means if I want a five by five matrix of random numbers I just pass and 5 come a 5 instead of
the tuple and working with these random libraries.
All right if we want to return a sample or many samples from the standard normal distribution or a Gaussian
distribution instead of using Ranz we can use rand n.
And that means I can say P thought random thought Ran's n and this will return numbers not from a uniform
distribution from 0 to 1 but instead from a standard normal distribution center around zero.
And that means I can go ahead and pass in two and I get two random numbers from a standard normal distribution
.
And later on we'll see that plotting these out not in this lecture but also it gets the data visualization
.
We'll go ahead and show you a nice normal distribution curve or a Gaussian distribution curve.
And again if I want this to be two dimensional I can just pass in two dimensions.
So for a four by four I say four come a four.
And no I don't pass a tuple here I just pass in each time mention as a separate argument and I can see
by the set of two brackets is a two dimensional matrix as an output and the final random method of creating
an array we're going to learn about is Rand int and that returns random integers from a low to a high
number or go ahead and show you what we can do for that.
You'll go ahead and say that random assin Rand I.A. you'll pasan.
I go to that shift tab here a low a high and then the actual size you want.
So for instance if you just say 1 to 100 you'll get a random integer between 1 and 100 and the low is
inclusive.
And the high x exclusive meaning one has the chance of being selected but 100 is not.
And if you want a particular number of random integers you can pass them in as a third argument.
So from to random integers from 1 to 100.
But not including 100.
I can just pass and 10 there and there are 10 random integers inclusive on the low in an exclusive on
the high end.
OK.
Let's go and discuss just a few useful attributes and methods of an array in order to do this.
I'm going to go ahead and create two new array objects we go in and clear this and clear these cells
.
All right.
I'm going to say a r r variable and I will say this is equal to pure range 25.
So if I check what A-R looks like here's a one dimensional array of zero up to 24.
And then I'm going to say Rand are is indeed random and it's going to be Rand.
Got to put a D there.
Random integers from 0 to 50.
So I didn't get 10 of those.
So if I Cheka or ran Are looks like looks like this you may get different values because it's randomly
selected.
These should have just 10 integers there.
All right.
One of the most useful methods you can use on an array is the reshape method.
And that's going to return an array containing the same data if in new shape.
That means I can call Mirei dots and then the reshape method on it reshape and I passen the new dimensions
I want.
So for example if I want to reshape that array member A R R is twenty five digits zero to 24.
I can reshape it as a 5 by 5 array using reshape and then passen the number of rows you want and the
number of columns.
Keep in mind you'll get an error if you can't fill up that matrix completely.
So for example if I wanted 5 by 10 I would need 50 elements to do that.
And I'm going to get an error here because the total size of the new array must be unchanged.
Meaning you need to fill up using only the digits you have or the elements you have a nice simple way
to check this is that the number of rows times the number of columns is equal to the actual number of
elements so five times five is equal to 25.
And I know I have 25 elements in this array.
OK let's go in and show a couple of more useful methods for finding max or min values or to find their
location and we'll go over a lot more operations later on in future lectures.
Here are some useful ones for you to know.
So go ahead and check out our random array.
One more time.
Again your numbers may be different but for the sake of this exercise that doesn't really matter.
If you want to find the max value in your array.
You can call the max method off of it and this will return the maximum value of that array.
Similarly if you want to get the minimum value of your array.
You can call the min method off of it.
We take a look at the array.
One more time we can actually figure out the location of the max or min values by specifying our max
or min.
Meaning if I want to actually know what index value is this Max located at 49 I can say Ranh are Argive
Max and that will return the index location of the max value.
So I notice that 49 is located in index 3.
So 0 1 2 3.
I have forty nine there.
And again you can do the same thing with the minute value you can say are men and it will return the
index location of the minimum value.
So 6 is the index location the minimum value and the minimum value was three.
In this case.
OK.
And quickly a few more useful methods and attributes.
If you ever want to figure out the shape of the vector you can say are shaped in a low return back the
shape.
Notice here I get 25 Khama and that's indicating that a r r that array was just a one dimensional vector
.
If I go ahead and reshape it.
So I'm going to say reshaped to a five by five and I'm going to set that equal to R..
Or a r r.
Notice now I have a two dimensional matrix called dot shape off of that.
Notice I'm not putting parentheses.
I'm just calling the attribute off of it.
Now reports back that it's a five by five.
So it's to that Mitchell now and the last useful attribute I want to show for now is the data type in
this array.
If you're ever curious of what data type you have an array you can just say a r r and type as an attribute
D type in it will return the actual data type that's in your way.
In this case 32 bit integers.
OK.
Just a quick review of everything we've gone over.
We've shown you how to create an empire raise either by casting a list to a higher array or by using
one of the built in functions for an umpire arrays such as arr. zeros and ones Lyn's space and I and
also the random generation methods you call N.P. that ran them and then Rand ran and ran and or some
of the other random methods.
All right and one last hints I want to show you if you don't want to have to type in NPE random that
Rand is what you can do is you can say from PI that random imports and then whatever method or function
you want to import.
So if you wanted to import Rand I N C you could go ahead and do that.
And then instead of saying that thought random thought ran I inti you can just say Rand's I I.A. and
I have a reference for yourself.
There you go.
OK.
Again remember you have the notebook with all the notes in case you want to reference any of this code
or any of the notes that actually spoke about throughout this lecture.
Coming up next we're going to learn about indexing and selection for an umpire race as well as operations
of the umpires.
Thanks everyone and I'll see you at the next lecture