本文介绍了numpy的数组:平均列替换NaN值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有实数大多是填补了numpy的阵列,但在这几个男
值,以及
I've got a numpy array filled mostly with real numbers, but there is a few nan
values in it as well.
我如何与列的平均值替换男
就是他们在哪里?
How can I replace the nan
s with averages of columns where they are?
推荐答案
没有需要循环:
import scipy.stats as stats
print a
[[ 0.93230948 nan 0.47773439 0.76998063]
[ 0.94460779 0.87882456 0.79615838 0.56282885]
[ 0.94272934 0.48615268 0.06196785 nan]
[ 0.64940216 0.74414127 nan nan]]
#Obtain mean of columns as you need, nanmean is just convenient.
col_mean = stats.nanmean(a,axis=0)
print col_mean
[ 0.86726219 0.7030395 0.44528687 0.66640474]
#Find indicies that you need to replace
inds = np.where(np.isnan(a))
#Place column means in the indices. Align the arrays using take
a[inds]=np.take(col_mean,inds[1])
print a
[[ 0.93230948 0.7030395 0.47773439 0.76998063]
[ 0.94460779 0.87882456 0.79615838 0.56282885]
[ 0.94272934 0.48615268 0.06196785 0.66640474]
[ 0.64940216 0.74414127 0.44528687 0.66640474]]
这篇关于numpy的数组:平均列替换NaN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!