Random Module Workout
- 03:45
Practice using the NumPy random module in Python.
Downloads
Glossary
NumPy Numpy arrays Python Random Seed Seed FunctionTranscript
Now you're gonna get some practice with the random module in NumPy. Go to your Jupyter Notebook and complete the following steps. First, set the random seed to 99. Then generate an array of random integers between 1 and 25 named rand array with five rows and five columns. Print rand array. Then choose and print a random sample of five numbers from the second row of rand array with replacement. Then generate an array of ordered integer from 1 to 25 named ordered array and reshape it to five rows and five columns. Print ordered array. And then choose and print a random sample of five numbers from the third column of ordered array without replacement.
We're gonna start by setting the random seed to 99 so that we can have reproducible results. Then we're gonna create the rand array NumPy array using the randint function. And we want these random integers to come between 1 and 25. And that second argument is exclusive, so we're going to exclude 26. This third argument tells randint that we want an array that's five columns in five rows.
Let's print that out. And here you can see that we have a new array that's five by five. And if you set your random seed to 99, you should get the exact same numbers here, although they appear to be random. Next, we're going to take a random sample from random array using the NumPy choice function. So the first argument is going to be the array that we want to choose from. And we're strictly looking at the second row of rand array right here. So in this first argument where we're telling choice where to choose from, we're gonna give it rand array, but specifically the second row index one. And we're gonna put this colon by itself to indicate that we want to choose from the entire row. The second argument, five tells choice to take five samples.
And here you can see that we have 2, 5, 21, 6, and 21, all of which come from the second row in rand array.
Next, we're creating ordered array. And to do this, we are creating an NumPy array using the range function, starting with 1 and excluding 26 and reshaping that to five rows in five columns.
When you print ordered array, it should look exactly like this one.
Finally, we're taking a random sample from the third column of ordered array, so we want all rows, which is why we use this colon by itself. And that tells Python we're starting at the beginning and then the colon, and we're ending at the at the final object. And then this two tells Python that we're looking at the third column. The second argument in choice tells choice to take five samples, and then by changing this replace argument to false choice will pull those five random samples without replacement, meaning that there will be no duplicates when we execute that cell. You'll see we get 13, 18, 8, 3, and 23 because choice is sampling without replacement. These are identical to all five of the objects in our third row of ordered array.