[SciPy-User] Reshaping Question

Skipper Seabold jsseabold@gmail....
Wed Nov 4 22:42:57 CST 2009


On Wed, Nov 4, 2009 at 10:56 PM, Skipper Seabold <jsseabold@gmail.com> 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.
>>
>> Anne
>>
>
> This makes sense as well.  This is kind of what I was looking for I
> just couldn't figure out the permutation.  I was trying to roll the
> axes, though I guess this could still work if you add the extra axis.
>
> I don't know if I'd use this in the end though, as it might sacrifice
> too much readability in the code, but maybe that's just me...
>

The more I think about it, this is actually pretty elegant.  I'm
always going to have a c (it's really a Hessian from a multinomial
logit) that's J**2*K x K, so I can just replace (2,2,2,2) with
(J,J,K,K) and (4,4) with (J * K, J * K), and I think it's still pretty
clear.

Thanks!

Skipper


More information about the SciPy-User mailing list