Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

np.random.rand vs np.random.random

Tags:

python

numpy

I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:

np.random.rand

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

np.random.random

Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval.

??? What exactly is the difference there?

like image 353
SpaceMonkey Avatar asked Nov 10 '17 22:11

SpaceMonkey


1 Answers

First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)

Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.

For example, to create an array of samples with shape (3, 5), you can write

sample = np.random.rand(3, 5) 

or

sample = np.random.random_sample((3, 5)) 

(Really, that's it.)


Update

As of version 1.17, NumPy has a new random API. The recommended method for generating samples from the uniform distribution on [0, 1) is:

>>> rng = np.random.default_rng()  # Create a default Generator. >>> rng.random(size=10)  # Generate 10 samples. array([0.00416913, 0.31533329, 0.19057857, 0.48732511, 0.40638395,        0.32165646, 0.02597142, 0.19788567, 0.08142055, 0.15755424]) 

The new Generator class does not have the rand() or random_sample() methods. There is a uniform() method that allows you to specify the lower and upper bounds of the distribution. E.g.

>>> rng.uniform(1, 2, size=10) array([1.75573298, 1.79862591, 1.53700962, 1.29183769, 1.16439681,        1.64413869, 1.7675135 , 1.02121057, 1.37345967, 1.73589452]) 

The old functions in the numpy.random namespace will continue to work, but they are considered "frozen", with no ongoing development. If you are writing new code, and you don't have to support pre-1.17 versions of numpy, it is recommended that you use the new random API.

like image 99
Warren Weckesser Avatar answered Sep 18 '22 12:09

Warren Weckesser