使用numpy对数组求平均时如何忽略nan值
时间:2023-02-17 15:51:38|栏目:Python代码|点击: 次
numpy对数组求平均时忽略nan值
在对numpy数组求平均np.mean()或者求数组中最大最小值np.max()/np.min()时,如果数组中有nan,此时求得的结果为:nan,那么该如何忽略其中的nan呢?
此时应该用另一个方法
np.nanmean()
,np.nanmax()
,np.nanmin()
使用np.mean()的效果
使用np.nanmean()的效果
numpy含nan值进行归一化操作
方法一
import numpy as np A = np.array([[ 7, 4, 5, 7000], [ 1, 900, 9, nan], [ 5, -1000, nan, 100], [nan, nan, 3, 1000]]) #Compute NaN-norms L1_norm = np.nansum(np.abs(A), axis=1) L2_norm = np.sqrt(np.nansum(A**2, axis=1)) max_norm = np.nanmax(np.abs(A), axis=1) #Normalize rows A_L1 = A / L1_norm[:,np.newaxis] # A.values if Dataframe A_L2 = A / L2_norm[:,np.newaxis] A_max = A / max_norm[:,np.newaxis] #Check that it worked L1_norm_after = np.nansum(np.abs(A_L1), axis=1) L2_norm_after = np.sqrt(np.nansum(A_L2**2, axis=1)) max_norm_after = np.nanmax(np.abs(A_max), axis=1) In[182]: L1_norm_after Out[182]: array([1., 1., 1., 1.]) In[183]: L2_norm_after Out[183]: array([1., 1., 1., 1.]) In[184]: max_norm_after Out[184]: array([1., 1., 1., 1.])
方法二
rom numpy import nan, nanmean from sklearn.preprocessing import StandardScaler scaler = StandardScaler() A = [[ 7, 4, 5, 7000], [ 1, 900, 9, nan], [ 5, -1000, nan, 100], [nan, nan, 3, 1000]] scaler.fit(A) In [45]: scaler.mean_ Out[45]: array([4.33333333, -32., 5.66666667, 2700.]) In [46]: scaler.transform(A) Out[46]: array([[ 1.06904497, 0.04638641, -0.26726124, 1.40399977], [-1.33630621, 1.20089267, 1.33630621, nan], [ 0.26726124, -1.24727908, nan, -0.84893009], [ nan, nan, -1.06904497, -0.55506968]]) In [54]: nanmean(scaler.transform(A), axis=0) Out[54]: array([ 1.48029737e-16, 0.00000000e+00, -1.48029737e-16,0.00000000e+00])