Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of '*' in numpy?

Tags:

python

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.

like image 358
user11869 Avatar asked Dec 09 '11 18:12

user11869


People also ask

What does [:] mean in Python array?

The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])

What is Axis in NP mean?

NumPy axes are the directions along the rows and columns. Just like coordinate systems, NumPy arrays also have axes. In a 2-dimensional NumPy array, the axes are the directions along the rows and columns.

How is NP mean () different from NP average () in NumPy?

np. mean always computes an arithmetic mean, and has some additional options for input and output (e.g. what datatypes to use, where to place the result). np. average can compute a weighted average if the weights parameter is supplied.


1 Answers

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)
like image 51
David Alber Avatar answered Oct 05 '22 05:10

David Alber