# [SciPy-User] masking an array ends up flattening it

Johann Cohen-Tanugi johann.cohentanugi@gmail....
Wed Feb 29 01:38:45 CST 2012

```Hi Zach, thanks a lot. I should know by now that naive expectations that
are not met in numpy are generally so for lack of generalization! Your
example makes perfect sense.
My use case is a covariance matrix that has the dimension of all the
parameters available, but some of them are fix in a fit, and I have a
bool array that tells me which parameters are fixed. I then would like
to "extract" the covariance matrix of the free parameters.

I would rather go for masking and then reshaping than fancy indexing,
which if too fancy start scaring me
Of course if there is a clean solution, I am all ears.

thanks again,
johann

On 02/28/2012 11:35 PM, Zachary Pincus wrote:
> Hi Johann,
>
>> Out[146]:
>> array([[ True,  True,  True, False],
>>         [ True,  True,  True, False],
>>         [ True,  True,  True, False],
>>         [False, False, False, False]], dtype=bool)
>>
>> Naively, I thought I would end up with a (3,3) shaped array when
>> applying the mask to m
>
> So that would make some sense for the above mask, but obviously doesn't generalize... what shape output would you expect if 'mask' looked like the following?
>
> array([[ True,  True,  True, False],
>         [ True,  True,  True, False],
>         [ True,  True,  True, False],
>         [False, False, False,  True]], dtype=bool)
>
> Flattening turns out to be the most-sensible general-case thing to do. Fortunately, this is generally not a problem, because often one winds up doing things like:
> where a and b can both be n-dimensional, and the fact that you go through a flattened intermediate is no problem.
>
> If, on the other hand, your task requires slicing square regions out of arrays, you could do that directly by other sorts of fancy-indexing or using programatically-generated slice objects, or some such. Can you describe the overall task? Perhaps then someone could suggest the "idiomatic numpy" solution?
>
> Zach
>
>
>
>> , but instead I get :
>>
>> Out[147]:
>> array([  1.82243247e-23,  -5.53103453e-14,   4.32071039e-13,
>>          -5.52425949e-14,   6.26697129e-02,  -5.12076585e-02,
>>           4.31598429e-13,  -5.12102340e-02,   6.27539118e-02])
>>
>> Out[148]: (9,)
>>
>> Is there another way to proceed and get directly the (3,3) shaped masked
>> array, or do I need to reshape it by hand?
>>
>> thanks a lot in advance,
>> Johann
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```