Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

any reasons for inconsistent numpy arguments of numpy.zeros and numpy.random.randn

Tags:

numpy

I'm implementing a computation using numpy zeros and numpy.random.randn

W1 = np.random.randn(n_h, n_x) * .01
b1 = np.zeros((n_h, 1))

I'm not sure why random.randn() can accept two integers while zeros() needs a tuple. Is there a good reason for that?

Cheers, JChen.

like image 628
Jeremy Chen Avatar asked Sep 10 '17 06:09

Jeremy Chen


People also ask

What is the difference between NP random rand () and NP random randn ()?

randn generates samples from the normal distribution, while numpy. random. rand from a uniform distribution (in the range [0,1)).

What is NumPy random randn?

NumPy random. 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 does NP random random return?

random. random() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random floats in the half-open interval [0.0, 1.0).

What is the use of NP random seed ()?

The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.


1 Answers

Most likely it's just a matter of history. numpy results from the merger of several prior packages, and has a long development. Some quirks get cleaned up, others left as is.

randn(d0, d1, ..., dn)
zeros(shape, dtype=float, order='C')

randn has this note:

This is a convenience function. If you want an interface that takes a tuple as the first argument, use numpy.random.standard_normal instead.

standard_normal(size=None)

With * it is easy to pass a tuple to randn:

np.random.randn(*(1,2,3))

np.zeros takes a couple of keyword arguments. randn does not. You can define a Python function with a (*args, **kwargs) signature. But accepting a tuple, especially one with a common usage as shape, fits better. But that's a matter of opinion.

np.random.rand and np.random.random_sample are another such pair. Most likely rand and randn are the older versions, and standard_normal and random_sample are newer ones designed to conform to the more common tuple style.

like image 104
hpaulj Avatar answered Oct 07 '22 13:10

hpaulj