Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ... mean in numpy code?

Tags:

python

numpy

And what is it called? I don't know how to search for it; I tried calling it ellipsis with the Google. I don't mean in interactive output when dots are used to indicate that the full array is not being shown, but as in the code I'm looking at,

xTensor0[...] = xVTensor[..., 0]

From my experimentation, it appears to function the similarly to : in indexing, but stands in for multiple :'s, making x[:,:,1] equivalent to x[...,1].

like image 769
Thomas Avatar asked Oct 22 '10 00:10

Thomas


People also ask

What does [: :] mean on NumPy arrays?

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.]])

Is there a mean function in NumPy?

mean. Compute the arithmetic mean along the specified axis. Returns the average of the array elements.

How do you find the mean value in NumPy?

You get the mean by calculating the sum of all values in a Numpy array divided by the total number of values. By default, the average is taken from the flattened array (from all array elements), otherwise along with the specified axis.

What does 3 mean in NumPy?

The 3 is part of that shape tuple. You will reference the dimensions by number in subsequent numpy code. arr. shape[2] will return 3 , and arr[:,:,0] will be all the R values of the image (if that is the correct interpreation).


2 Answers

Yes, you're right. It fills in as many : as required. The only difference occurs when you use multiple ellipses. In that case, the first ellipsis acts in the same way, but each remaining one is converted to a single :.

like image 60
ars Avatar answered Oct 22 '22 17:10

ars


Although this feature exists mainly to support numpy and other, similar modules, it's a core feature of the language and can be used anywhere, like so:

>>> class foo:
...   def __getitem__(self, key):
...     return key
... 
>>> aFoo = foo()
>>> aFoo[..., 1]
(Ellipsis, 1)
>>> 

or even:

>>> derp = {}
>>> derp[..., 1] = "herp"
>>> derp
{(Ellipsis, 1): 'herp'}
like image 43
SingleNegationElimination Avatar answered Oct 22 '22 17:10

SingleNegationElimination