Home > Blockchain >  Why do np.random.randn and np.zeros take different kinds of argument?
Why do np.random.randn and np.zeros take different kinds of argument?

Time:11-26

Initializing an array with random numbers can be achieved like this:

arr_1 = np.random.randn(3,5)

Initializing with all entries equal to 0 is done like so:

arr_2 = np.zeros((3,5))

To specify the desired shape, np.random.randn takes integers as arguments, whereas np.zeros expects a tuple of integers (which is then equal to the shape of the returned array). When working with these functions, I often confuse one with the other.
Why do these functions take different kinds of arguments for specifying the shape?

First, I thought that functions from np.random (like randn) do this way and functions directly from np (like zeros or ones) do that way. This seems however not to be the case: np.random.randint takes, among other parameters, the shape as a tuple, unlike np.random.randn.

CodePudding user response:

This is explained in the documentation:

This is a convenience function for users porting code from Matlab, and wraps standard_normal. That function takes a tuple to specify the size of the output, which is consistent with other NumPy functions like numpy.zeros and numpy.ones.

  • Related