Skip to content

mean and nanmean give different results on masked arrays #9071

@clintrowe

Description

@clintrowe

I'm working on code to compute some statistics and created a masked array. Even though the current dataset contains no NaNs, future data may. I found that, after masking the array, I obtained differing results with mean than with nanmean. Here's a snippet using a 4x1096 array:

print np.count_nonzero(np.isnan(stats),axis=1)
print np.mean(stats,1)
print np.nanmean(stats,1)
stats = np.ma.masked_invalid(stats)
print stats.size - np.count_nonzero(stats,axis=1)
print np.mean(stats,1)
print np.nanmean(stats,1)

with output:

0
[ 27.42351151 26.58027077 26.62998009 26.5291214 ]
[ 27.42351151 26.58027077 26.62998009 26.5291214 ]
0
[27.423510920392335 26.58027058622263 26.62998082515967 26.52912223426095]
[27.423511505126953 26.580270767211914 26.629980087280273 26.52912139892578]

Python 2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15)
numpy.version.version
'1.11.3'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions