[Numpy-discussion] high dimensional array -> python scalar/index

josef.pktd@gmai... josef.pktd@gmai...
Sun Jan 6 12:28:40 CST 2013


On Sun, Jan 6, 2013 at 12:57 PM, Sebastian Berg
<sebastian@sipsolutions.net> wrote:
> Question for everyone, is this really reasonable:
>
>>>> import numpy as np
>>>> from operator import index
>>>> index(np.array([[5]]))
> 5
>>>> int(np.array([[5]]))
> 5
>>>> [0,1,2,3][np.array([[2]])]
> 2

Not sure I understand the point

looks reasonable to my

int has an implied squeeze, if it succeeds

not so python lists

>>> int([[1]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'list'


>>> [0,1,2,3][np.array([[2, 2], [0, 1]])]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: only integer arrays with one element can be converted to an index


but we can to more fun things with numpy

>>> np.array([0,1,2,3])[np.array([[2, 2], [0, 1]])]
array([[2, 2],
       [0, 1]])

Josef

>
> To me, this does not make sense, why should we allow to use a high
> dimensional object like a normal scalar (its ok for 0-d arrays I guess)?
> Personally I would be for deprecating these usages, even if that
> (probably) means you cannot reshape your array with a matrix (as it is
> 2D) ;-):
>>>> np.arange(10).reshape(np.matrix([5,-1]).T)
> array([[0, 1],
>        [2, 3],
>        [4, 5],
>        [6, 7],
>        [8, 9]])
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list