[Numpy-discussion] another broadcast/fancy indexing question

Timothy Hochberg tim.hochberg at ieee.org
Tue Jul 10 17:02:54 EDT 2007


On 7/10/07, Mark.Miller <mpmusu at cc.usu.edu> wrote:
>
> Just ran across something that doesn't quite make sense to me at the
> moment.
>
> Here's some code:
>
> >>> numpy.__version__
> '1.0.2'
> >>>
> >>> def f1(b,c):
>         b=b.astype(int)
>         c=c.astype(int)
>         return b,c
>
> >>> b,c = numpy.fromfunction(f1,(5,5))
> >>> a=numpy.zeros((2,12,5,5),int)
> >>> a1=a[0]
> >>> a1[:,b,c].shape
> (12, 5, 5)
> >>> a[0,:,b,c].shape
> (5, 5, 12)   ###why does this not return (12,5,5)?
> >>>
>
> So in a nutshell, it's not completely clear to me why these are
> returning arrays of different shapes.  Can someone shed some light?


It's because you are using arrays as indices (aka Fancy-Indexing). When you
do this everything works differently. In this case, everything is being
broadcast to the same shape. As I understand it (and I try to use only the
simplest forms of fancy indexing), what you are doing is equivalent to:

-- 
.  __
.   |-\
.
.  tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070710/74fa1e89/attachment.html>


More information about the NumPy-Discussion mailing list