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

Chris Colbert sccolbert@gmail....
Tue Jul 14 14:42:54 CDT 2009


for your particular case:

>>> a = np.array([1, 5, 4, 99], 'f')
>>> b = np.array([3, 7, 2, 8], 'f')
>>> c = b.copy()
>>> d = a!=99
>>> c[d] = (a[d] + b[d])/2.
>>> c
array([ 2.,  6.,  3.,  8.], dtype=float32)
>>>


On Tue, Jul 14, 2009 at 3:36 PM, Chris Colbert<sccolbert@gmail.com> wrote:
> index with a boolean array?
>
>>>> import numpy as np
>>>> a = np.array([3, 3, 3, 4, 4, 4])
>>>> a
> array([3, 3, 3, 4, 4, 4])
>>>> np.average(a)
> 3.5
>>>> b = a != 3
>>>> b
> array([False, False, False,  True,  True,  True], dtype=bool)
>>>> np.average(a[b])
> 4.0
>>>>
>
>
> On Tue, Jul 14, 2009 at 3:33 PM, Greg Fiske<gfiske@whrc.org> 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