In this video, I’m going to cover some other functions in the NumPy module which are a lot like
arange() in that they return arrays or grids of values—numeric values—but are different in the criteria that they use to build these arrays or grids.
Let’s move over into the terminal, and as always,
import numpy as np. And the reason I’m making sure to include this every time is just that I often forget it and so I want to make sure that you don’t.
I’m going to show one example of
arange() in all of its glory with all of the arguments included just to make sure that everyone’s on the same page. So, starting at
1, going up to but not including
100, with a
And then I’m going to say that the
dtype (data type) is just going to be an
int32, just this time for fun. So there it is! It generates a
numpy.ndarray going from
1 up to
99 because we’re not including
100, with a step of
1 and using
int32 as the kind of data.
The first alternate routine that I’m going to use is
linspace(), which is really, really useful. It’s very similar to
arange() except that what you can do instead of doing a
step is that you can specify a
start and a
stop, but you can specify the number of values that you want.
and what this will do is give you 33 evenly spaced values between
100. You’ll notice that
linspace() by default includes
100, which is different than
arange(), which does not include the
But you can specify, you can say
endpoint=False if you don’t want to include that value. And you’ll notice that including or not including the endpoint really changes the nature of the array when you’re using this
num parameter instead of specifying a
linspace(), and it’s pretty cool. It works really well, and it’s a really useful thing when you’re doing data analysis because sometimes what’s more important is that you have ten or a hundred or a thousand values on an axis—that’s more important than the exact distance between them, right?
I won’t get too much into the details of exactly what it uses there—you’re going to have to get into the documentation of NumPy a little bit on your own there. And then if you want grids of data—so you want, for example,
I’m sorry, I didn’t give it a full list. You need to actually give
numpy.array() a list instead of several arguments. So, say you want to get a grid of all of the values on a graph that have any combination of these
y values. So you might want
1, 1 then you might want
2, 3—or, sorry, I’ll just go in order—
1, 2, then
1, 3, and so on, right? Then you want
2, 4; and so on.
So what you can do for that is you can say
y. And what that does is that it gives you two parameters back, and these parameters are the
x and the
y values for all of those combinations.
So then, if you were to plug this into a plotting thing, so say you had some kind of
plot() function, you can use PyPlot or Matplotlib, or something like that—you can plot
And things are getting a little more hand-wavy here because this is not a tutorial course on how to use Matplotlib or PyPlot, so I don’t want to get too much into it, but if you need grids of evenly spaced values, then you can use
meshgrid() and there are also functions called
mgrid(), which have slight variations on that. But again, it’s very similar to the
arange() function, but they have different purposes and are useful for different things.
I hope you found this video useful, and I’d encourage you to read up a little bit on things like
np.meshgrid(), and then plotting and data analysis, because that’s where a lot of these functions are actually used.
Become a Member to join the conversation.