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

Sebastian Berg sebastian@sipsolutions....
Sun Jan 6 12:56:22 CST 2013


On Sun, 2013-01-06 at 13:28 -0500, josef.pktd@gmail.com wrote:
> 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
> 

Exactly *why* should it have an implied squeeze? Note I agree, the
int(np.array([3])) is OK, since also int('10') works, however for index
I think it is not OK, you simply cannot do list['10'].

> 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'

Exactly, so why should numpy be much more forgiving?

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

Of course... But if you compare to lists, thats actually a point why
index should fail:

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

is very different from:

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

and in my opinion there is no reason why the latter should not simply
fail.

> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 




More information about the NumPy-Discussion mailing list