numpy_将nan替换为均值


# 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的数组,替换均值后的数组:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM