[Numpy-discussion] mean of two or more arrays while ignoring a specific value

Zachary Pincus zachary.pincus@yale....
Wed Jul 15 09:05:01 CDT 2009


Might want to look into masked arrays: numpy.ma.array.
a = numpy.array([1,5,4,99])
b = numpy.array([3,7,2,8])
arr = numpy.array([a, b])
masked = numpy.ma.array(arr, mask = arr==99)
masked.mean(axis=0)

masked_array(data = [2.0 6.0 3.0 8.0],
              mask = [False False False False],
        fill_value = 1e+20)

Zach


On Jul 15, 2009, at 9:40 AM, Greg Fiske wrote:

> Dear list,
>
> I’m learning to work with numpy arrays.  Can somebody explain how to  
> get the average of two separate arrays while ignoring a user defined  
> value in one array?
>
> For example:
> >>>a = numpy.array([1,5,4,99])
> >>>b = numpy.array([3,7,2,8])
>
> Ignoring the value 99, the result should be an array like c=  
> ([2,6,3,8])
>
> Thanks for any advice,
>
> Greg
>
>
> Greg Fiske
> Research Associate/GIS Manager
> The Woods Hole Research Center
> 149 Woods Hole Road
> Falmouth MA, 02540
> 508-540-9900x139
> gfiske@whrc.org
> http://whrc.org
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list