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

猜你喜欢

转载自www.cnblogs.com/cxxBoo/p/12531606.html