首页 > 其他 > 详细

numpy_将nan替换为均值

时间:2020-03-20 15:07:33      阅读:96      评论:0      收藏:0      [点我收藏+]
# coding = utf-8
import numpy as np
def nan_fill(a):
for i in range(a.shape[1]):
temp_col = a[:, i]
count_col_nan = np.count_nonzero(temp_col == temp_col)
if count_col_nan != 0:
not_nan_num = temp_col[temp_col == temp_col]
temp_col[np.isnan(temp_col)] = not_nan_num.mean()
return a

if __name__ == ‘__main__‘:
a = np.arange(12).reshape(3, 4).astype(float)
print(a)
a[1, 2:] = np.nan
print(a)
a = nan_fill(a)
print(a)


效果图,依次为原数组,含有nan的数组,替换均值后的数组:

技术分享图片

 

numpy_将nan替换为均值

原文:https://www.cnblogs.com/cxxBoo/p/12531606.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!