# [SciPy-user] how to filter outliers from an array

Conor Robinson conor.robinson at gmail.com
Mon Oct 30 15:38:19 CST 2006

```I have found scipy.where() useful, although James' solution is very
nice as well.

On 10/30/06, James Coughlan <coughlan at ski.org> wrote:
>
> ####
> dx=rand(5,5) #substitute whatever matrices you're actually
> dy=rand(5,5) #using here
>
> import scipy.stats
> #compute mean and std for dx and dy:
> mx=stats.mean(dx.flat)
> my=stats.mean(dy.flat)
> sx=stats.std(dx.flat)
> sy=stats.std(dy.flat)
>
> wx=abs(dx-mx)>sx
> wy=abs(dy-my)>sy
> print wx | wy # logical or
> ####
>
> Here wx and wy are arrays whose elements are set to True if they lie
> more than 1 std. from mean. So wx | xy will flag those elements for
> which either the x or y components are outliers.
>
> James
>
>
>
> Aaron Hoover wrote:
>
> >Hi All,
> >
> >I'm doing feature matching between two images and as a result I get
> >back an array of dx, dy shift values in pixel space for potential
> >features that match between two images. I'd like to be able to throw
> >out [dx, dy] vectors for which either value deviates significantly
> >(more than one std deviation, perhaps) from the mean (I'm dealing
> >with 2D rigid body translations only. I could do this by looping over
> >the values and comparing them individually to the mean, but I'm
> >wondering if there's a more compact (single line?) way of doing this
> >perhaps using the scipy.stats package. Something along the lines of
> >the filter built-in for lists, but that handles the higher
> >dimensionality, would be nice.
> >
> >Thanks,
> >Aaron
> >
> >
> >_______________________________________________
> >SciPy-user mailing list
> >SciPy-user at scipy.org
> >http://projects.scipy.org/mailman/listinfo/scipy-user
> >
> >
>
>
> --
> -------------------------------------------------------
> James Coughlan, Ph.D., Associate Scientist
>
> Smith-Kettlewell Eye Research Institute
>
> Email: coughlan at ski.org
> URL: http://www.ski.org/Rehab/Coughlan_lab/
> Phone: 415-345-2146
> Fax: 415-345-8455
> -------------------------------------------------------
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
```