user11869
user11869

Reputation: 1131

What is the meaning of '*' in numpy?

>>> shape=(2,2)
>>> np.random.randn(*shape)
array([[-1.64633649, -0.03132273],
   [-0.92331459,  1.05325462]])

I can't find it in numpy's documentation. Any help is appreciated.

Upvotes: 5

Views: 1580

Answers (2)

Gregg Lind
Gregg Lind

Reputation: 21312

People other places sometimes call it the 'splat'. (for completeness, ** does the same thing, but with named/keyword arguments).

Upvotes: 2

David Alber
David Alber

Reputation: 18121

This is not NumPy-specific syntax; it is Python syntax. The so-called *-operator is Python syntax that does sequence unpacking in argument lists (see Unpacking Argument Lists).

The use in your example is to unpack the shape tuple into separate arguments. This is needed because numpy.random.randn takes an arbitrary number of integers as parameters, not a tuple of integers.

The code from the question is equivalent to doing:

>>> np.random.randn(2, 2)

Upvotes: 17

Related Questions