Creating Arrays in Numpy
Zenva
ACCESS the FULL COURSE here: https://academy.zenva.com/product/bite-sized-coding-academy/
TRANSCRIPT
Okay, so for starters, you might have seen lists in Python before, for example we could create list_1, we go to run the cell, we just get the simple array. Well NumPy arrays are set up not so differently, so we'll simply add the import of NumPy. We can also rename this to np as you'll often see. So we'll go ahead and run that cell. Nothing exciting happened because we're not printing or calculating anything, we're just gaining access to that library. So let's say I want to create a NumPy based on my list_1. So I'm gonna call this numpy_1. This is just going to actually be equal to np.array and then we can pass in whatever value we want. So we could pass in a list of values here, or we could actually pass in a pre-made list. In our case maybe we want to pass in list_1 and then perhaps we'll want to print out numpy_1, so let's go ahead and save that and if we were to run the cell we would again get one, two, three printed out just like before. Now as I mentioned earlier we can also create arrays of zeros or ones. This is going to be np not array this time, but np.zeroes. Now zeroes actually takes in an argument and it takes in how many elements we want in this array. So say we want five zeroes here, and then we want to perhaps print out zeroes array, and if we were to run this cell an array of five zeroes here. Again, we could do the same thing for one, and we'll print this out and then of course the array of ones here. But what if we want to specify a range of values so we want maybe a start point and an endpoint, np dot and we're gonna use this arange operator, operation rather, and this can take in several different parameters. So if we want to just specify a range from zero to some value, maybe let's say zero to five, we can enter that. Okay, and as you can see we get five elements printed out. So if we want to specify a start point as well, let's say we want the elements five to 10 and we're going to set np.arange from five up until, and if we want to include 10, we would have to put up until 11. So now we go to run the cell, we get five, six, seven, eight, nine and 10 printed out. Now let's say we want to step by twos, so we want to maybe start at zero, go until 20 and we want to skip every other element. Well we can do that simply by specifying a third parameter, being the step zero to 20 and then we'll want to include the step of two. So let's go ahead and run that cell. And we get exactly what we would want here. So linspace is a conjunction for linear space, and with a linspace we basically take a lower bound, an upper bound and the number of elements we want it to populate between that upper bound and lower bound. This is going to be np.lin ... https://www.youtube.com/watch?v=xgWJMbZYhRM
4918366 Bytes