The Random Module
- 03:09
How to use the NumPy random module for generating random integers and manipulating arrays, including reshaping arrays and specifying dimensions directly in the size parameter.
NumPy Functions: https://numpy.org/doc/stable/reference/
Downloads
No associated resources to download.
Glossary
NumPy Numpy arrays Python Random ModuleTranscript
A package like NumPy is a collection of modules, which are just collections of related custom functions. Inside the NumPy package, there's a module named Random, which you can access by calling NumPy dot random, or np dot random if you imported NumPy with the np alias. This module contains useful statistical tools for random sampling and generating randomized data. You can view the functions inside the random module by typing np dot random dot, and then hitting the tab key. And when you hit the tab key, it's gonna show you all of the functions that are inside this module. This is useful for any kind of package that you're importing into your code, and there will also be a link below this video to the NumPy documentation with descriptions of all of the random functions.
A useful function in the NumPy random module is the randint function. For generating random integer. It takes three arguments. First, the lower boundary, which in this case is 1, the upper boundary, which in this case is 11, and the size or the amount of numbers that you want to generate using the randint function. Notice that low is inclusive and high is exclusive. So this one sample is going to be drawn from the range 1 to 10, it will not be 11, and when I execute that code cell, it's gonna give me an array with a single number 9. In this next case, I'm telling the randint function to generate 10 numbers in that same range, and then I'm printing the array that randint generates. After that, I'm printing the shape of the array. So you can see here that I've generated 10 random integer between the numbers 1 and 10, and you can see the shape of that array is 10 objects in one dimension. If I needed that array in a different shape, I could use the reshape function. So let's say that instead of 10 objects in 1 dimension, I want this array to have five rows in two columns. Well, I've defined this array rand array, so I'm gonna call rand array, and I'm gonna set it equal to rand array dot reshape five two, and then I'm gonna print it.
And you can see that this array has been reshaped into a new array with five rows and two columns. Alternatively, instead of reshaping the array after you create it, you could actually pass the size of the array that you want, the dimensions of the array that you want into the size function like I've done right here. So instead of reshaping into five rows and two columns, I can just replace the 10 that you have up here telling random to generate 10 random integers. And I can tell it print me an array of random integers with the dimensions, five rows and two columns. So now when I generate that and print it, I have a new set of random numbers generated in the shape or in the dimensions that I already want the array to be in.