[SciPy-User] masking an array ends up flattening it
Zachary Pincus
zachary.pincus at yale.edu
Tue Feb 28 17:35:11 EST 2012
Hi Johann,
> In [146]: mask
> 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:
a[mask] = b[mask]
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 :
>
> In [147]: m[mask]
> 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])
>
> In [148]: m[mask].shape
> 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 at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
More information about the SciPy-User
mailing list