0
如何通過零TE替代的NaN下面一組數據?:我的目標是讓下面的估計,假設matches
是下面給出的數據集:如何用numpy數組替換nan來計算平均值?
# average the data
avg = {}
for k, v in matches.items():
avg[k] = sum(v)/float(len(v))
print(avg)
的matches
內容:
{('AD', 'MM', 'TT'): [], ('AD', 'MM', 'VV'): [array([54.0, 97.0, 160.0, nan, 9.0, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object), array([176.0, 68.0, 166.0, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([nan, 55.0, 176.0, nan, 34.0, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object), array([38.0, 72.0, 466.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object), array([62.0, 20.0, 397.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object), array([40.0, 31.0, 94.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object), array([104.0, 70.0, 338.0, nan, 19.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([99.0, 62.0, 401.0, nan, 37.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([53.0, 109.0, 262.0, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([45.0, 55.0, 159.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object), array([80.0, 104.0, 438.0, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([81.0, 75.0, 141.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object), array([140.0, 43.0, 192.0, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([72.0, 26.0, 704.0, nan, 57.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([67.0, 49.0, 210.0, nan, 20.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([122.0, 67.0, 546.0, nan, 25.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([130.0, 61.0, 400.0, nan, 20.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([88.0, 133.0, 354.0, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([77.0, 42.0, 212.0, nan, 31.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object), array([138.0, 64.0, 132.0, nan, 49.0, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object)]}
看'np.nanmean'。但是當我嘗試一下你的''''''''時,我得到'nan'。如果我將數組更改爲'dtype = float',我可以得到一個合適的毫無意義。 – hpaulj
你是指這個? 'for k,v in matches.items(): avg [k] = np.nanmean(v)'。它爲所有'v'輸出'nan'。 – user7379562
http://stackoverflow.com/questions/42299093/how-to-calculate-mean-values-over-columns-in-a-given-data-structure詢問包含對象dtype數組列表的類似字典的含義。如果需要,更改'mean'函數的'axis'參數。 – hpaulj