Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Other Functions

00:00 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.

00:16 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.

00:28 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 step of 1.

00:47 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.

01:04 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.

01:23 Let’s do something interesting, like 33,

01:27 and what this will do is give you 33 evenly spaced values between 1 and 100. You’ll notice that linspace() by default includes 100, which is different than arange(), which does not include the stop value.

01:40 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 step.

01:56 So, that’s 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?

02:15 And so you might want to standardize the number of values on an axis.

02:20 Another thing very similar to that is the logspace(), except the logspace()—I’m just going to duplicate the parameters above—

02:31 does all of the same things, except it just takes the log of the linspace(). So this is useful when you want to plot data on a logarithmic scale.

02:39 And there is a very similar function, which I’m going to slowly make my way back to, called geomspace(), which uses a geometric function and does essentially the same thing there.

02:51 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, np.array().

03:08 Say you want all of the—

03:14 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 x and 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, 1; 2, 2; 2, 3; 2, 4; and so on.

03:45 So what you can do for that is you can say np.meshgrid() of x and 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.

04:03 So if you match this up—x 1, y 1; x 2, y 1; x 3, y 1; x 4; y 1.

04:12 Or—yeah, exactly. So you see what I’m doing here. x 1, y is 2; x 2, y 2; and so on.

04:17 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 np.meshgrid() directly.

04:32 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 ogrid() and 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.

04:58 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.