[SciPy-User] Reshaping Question

David Warde-Farley dwf@cs.toronto....
Wed Nov 4 21:12:18 CST 2009


On 4-Nov-09, at 9:05 PM, Anne Archibald wrote:

> Reshape sometimes creates copies. It tries hard not to, and if you
> assign the shape attribute rather than calling reshape it won't ever
> make a copy, but if necessary reshape will copy the input array:
>
> In [42]: np.transpose(c.reshape(2,2,2,2), 
> (0,2,1,3)).reshape(4,4)Out[42]:
> array([[ 0,  1,  4,  5],
>       [ 2,  3,  6,  7],
>       [ 8,  9, 12, 13],
>       [10, 11, 14, 15]])
>
> The trick is to use transpose to do an arbitrary permutation of the
> input axes, and also to rearrange the first axis with an additional
> reshape.

D'oh. When he said reshape I was thinking purely in terms of what  
could be done with .reshape(). I didn't even think about .transpose().

Is it then the .transpose() call that triggers the copy in this  
situation?

David


More information about the SciPy-User mailing list