Why does scipy.stats.nanmean give different result from numpy.nansum?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: >>> import numpy as np >>> from scipy import stats >>> a = np.r_[1., 2., np.nan, 4., 5.] >>> stats.nanmean(a) 2.9999999999999996 >>> np.nansum(a)/np.sum(~np.isnan(a)) 3.0 I'm aware of the limitation of floating point representation. Just curious why the more clumsy expression seems to give "better" result. 回答1: First of all, here is scipy.nanmean() so that we know what we're comparing to: def nanmean(x, axis=0): x, axis = _chk_asarray(x,axis) x = x.copy() Norig = x.shape[axis] factor = 1.0-np.sum(np.isnan(x),axis)*1.0/Norig x[np.isnan(x)] =