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

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 not find it in the numpy documentation. Any help is appreciated.

+4
source share
2 answers

This is not NumPy specific syntax; This is Python syntax. The so-called * -operator is Python syntax that decompresses a sequence of argument lists (see Unpacking argument lists ).

The use in your example is to unpack the shape tuple into separate arguments. This is necessary because numpy.random.randn accepts an arbitrary number of integers as parameters, rather than a set of integers.

The code from the question is equivalent to doing:

 >>> np.random.randn(2, 2) 
+15
source

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

+1
source

Source: https://habr.com/ru/post/1385584/


All Articles