In this tutorial, I’ll show you how to use the np.random.rand function (AKA, Numpy random rand) to create Numpy arrays filled with random uniform numbers. Show
I’ll explain exactly what this function does, how the syntax works, and I’ll show you step-by-step examples of how to use it. If you need something specific, you can click on any of the following links to navigate to the correct section of the tutorial. Table of Contents:
Ok. Let’s get to it. A quick introduction to Numpy Random RandAt a high level, the Numpy random rand function is pretty straight forward.
To help you understand that, let’s quickly review the relevant details about Numpy arrays, and about the uniform distribution. Numpy Random Uniform Creates Numpy ArraysFirst of all, let’s review Numpy and Numpy arrays. The A Numpy array is a Python data structure that we use for storing and manipulating numeric data. Numpy arrays have a row-and-column structure, and they can come in a variety of shapes and sizes. They can be 1-dimensional, 2-dimensional, or multi dimensional. Here’s a quick example of a 2D Numpy array: Additionally, the Numbers inside of a Numpy array can have a variety of different properties. We Use Numpy Functions to Create and Manipulate Numpy ArraysThe Numpy package has a variety of functions for working with Numpy arrays. For example, there are tools for summing the numbers in a Numpy array, calculating the mean of the numbers, calculating the standard deviation, and so on. But there are also Numpy functions for creating arrays with specific properties. For example, there are functions for creating arrays that contain only 1s, creating arrays that contain all zeros, and creating arrays that contain numbers from specific probability distributions. For example, the Numpy random normal function creates arrays with normally distributed numbers. The np.random.rand creates arrays with numbers drawn from the standard uniform distributionThe Numpy random rand function creates Numpy arrays that are filled with Numbers from the standard uniform distribution. If you’ve taken a class on probability, you might be familiar with the uniform distribution, but let’s quickly review. The Uniform Distribution has a Constant Probability Within a Specific RangeThe uniform distribution has a constant probability density function between a specific range. Generally, the probability density function is between a specific range,low and high , and 0 everywhere else. Essentially, when we use a uniform probability distribution, there’s a constant probability of selecting a number within a specific range, and 0 probability outside that range. Keep in mind that the Numpy random uniform function generates numbers from the general uniform distribution. The standard uniform distribution is a special case of the uniform distributionThe standard uniform distribution is a special case of the uniform distribution. In the standard uniform distribution, the boundaries of the range are set to To restate and simplify: the standard uniform distribution selects numbers between 0 and 1. The numbers between 0 and 1 have a uniform probability of being selected. Outside of 0 and 1, the probability of selecting a number is 0. So that’s essentially what But, there are a few details about how the function works. To understand those details, we need to look at the syntax. The syntax of np.random.randHere, I’ll walk you through the syntax of A quick noteOne quick note about the syntax. Everything that I’m about to explain about the syntax assumes that you’ve imported Numpy. In particular, I’m going to assume that you’ve imported Numpy with the alias ‘ You can do that with the code This is the common convention among Python data scientists. And it’s important, because how we import Numpy will slightly affect how we write the syntax. np.random.rand syntaxOk, assuming that we’ve imported Numpy with the alias Inside of the parenthesis, there are a set of parameters that enable you to modify the size of the output. Let’s take a closer look at those. The parameters of np.random.randThe Keep in mind, that these parameters are optional. In fact, it’s possible to call the function without any parameters at all. In this case, But if you use the parameters, you can specify the number of rows, columns, and the number of elements along additional dimensions. Each of the following parameters controls an axis.
Let’s take a look at these so you understand exactly what they do. d0 (optional)If you chose to use the So if you’re creating a 1 dimensional Numpy array, If you’re creating a 2 dimensional or n-dimensional array, then Remember: for 1D arrays, axis-0 is the only axis. But for 2D arrays, axis-0 points downwards against the rows. If you’re confused about this, you should review how Numpy axes work. d1 (optional)The second parameter, Remember: in a 2-dimensional array, axis-1 points horizontally along the columns. So if you create a 2D or multi-dimensional array, Bear in mind that you’ll only use dn (optional)The previous two parameters ( If you want to create an n-dimensional Numpy array, The Output of np.random.randThe Having said that, the exact shape of the output depends on the parameters you use. If you call If you call the function as And if you use the higher order parameters So essentially, the parameters Examples: How to Use np.random.rand to Generate Random Uniform NumbersNow that we’ve looked at the syntax, let’s look at some examples. It will probably be easier to understand how this works if you see some concrete examples. Examples:
Run this code firstOne quick note. As I mentioned above, before we use Numpy, we need to import the Numpy package. You can do that with the following code: import numpy as np Once you do that, you’ll be ready to run the example code. EXAMPLE 1: Generate a single number with np.random.randOk. First, we’ll generate a single number with Numpy random rand. Let’s take a look. np.random.seed(0) np.random.rand() OUT: 0.5488135039273248Explanation This is really simple. When we call Here, we also used Numpy random seed to make our code reproducible. That being the case, if you run this code, you should get the same output. EXAMPLE 2: Create a 1D Numpy array with Numpy Random RandNext, we’re going to create a 1-dimensional Numpy array, filled with random uniform numbers. np.random.seed(0) np.random.rand(3) OUT: array([0.5488135 , 0.71518937, 0.60276338])Explanation Here, we’ve called Numpy random rand and we provided a single number, 3, as an argument. This argument value is being passed to the Effectively, when we call So the input value is 3, and the output array has 3 elements. (Also note that we’ve used Numpy random seed to make the output reproducible.) EXAMPLE 3: Create a 2D Numpy array with Numpy Random RandFinally, let’s create a 2-dimensional array with Numpy random rand. To do this, we’re going to call the function with two integer arguments: np.random.seed(0) np.random.rand(2, 3) OUT: array([[0.5488135 , 0.71518937, 0.60276338], [0.54488318, 0.4236548 , 0.64589411]])Explanation Here, we’ve called The value 2 is being passed to the Remember, when we create a 2D array, So here, when we call the function as Note also that once again, we’ve used Numpy random seed to make our code reproducible. Frequently Asked Questions about Numpy Random RandNow that you’ve learned about the syntax of Numpy Random Rand, and seen some examples, let’s look at a common question about the function. Frequently asked questions:
What’s the difference between np.random.rand and np.random.uniform?
Both functions generate random numbers drawn from the uniform distribution. Both functions output Numpy arrays. The difference is that Let me explain.
So the To see this, try running the following code: np.random.seed(0) np.random.rand(3) np.random.seed(0) np.random.uniform(size = 3, low = 0, high = 1) You’ll notice that these produce the same output. Beyond that, Numpy random uniform has a slightly different syntax. To learn more about this function, check out our tutorial about Numpy random uniform. Leave your other questions in the comments belowDo you have other questions about the Numpy random rand function? If so, just leave your question in the comments section at the bottom of the page. Join our course to learn more about NumpyHere in this tutorial, I’ve explained how to use the This should help you understand this particular function, but if you really want to learn Numpy, there’s a lot more to learn. If you’re serious about mastering Numpy, and serious about data science in Python, you should consider joining our premium course called Numpy Mastery. Numpy Mastery will teach you everything you need to know about Numpy, including:
Moreover, it will help you completely master the syntax within a few weeks. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for. Find out more here: Learn More About Numpy Mastery What is randn in Python?randn() function in Python is used to return random values from the normal distribution in a specified shape. This function creates an array of the given shape and it fills with random samples from the normal standard distribution.
What is the difference between NP random rand () and NP random randn ()?randn generates samples from the normal distribution, while numpy. random. rand from uniform (in range [0,1)).
What is the differences you see from the numbers generated from Rand and randn?The difference between rand and randn is (besides the letter n ) that rand returns random numbers sampled from a uniform distribution over the interval [0,1), while randn instead samples from a normal (a.k.a. Gaussian) distribution with a mean of 0 and a variance of 1.
What is the range for randn in Numpy?The numpy. random. rand() function outputs a Numpy array that contains numbers drawn from the standard uniform distribution. That is, the array will contain numbers from the range [0, 1) .
|