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