假设我有一个带有一些float('nan')的numpy数组,我现在不想插补这些数据,我想先对其进行规范化并将NaN数据保留在原始空间,有什么办法可以做那?
以前我在中使用过normalize
函数sklearn.Preprocessing
,但是该函数似乎无法将任何包含NaN的数组作为输入。
您可以使用numpy.ma.array
函数屏蔽数组,然后执行任何numpy
操作:
import numpy as np
a = np.random.rand(10) # Generate random data.
a = np.where(a > 0.8, np.nan, a) # Set all data larger than 0.8 to NaN
a = np.ma.array(a, mask=np.isnan(a)) # Use a mask to mark the NaNs
a_norm = a / np.sum(a) # The sum function ignores the masked values.
a_norm2 = a / np.std(a) # The std function ignores the masked values.
您仍然可以访问原始数据:
print a.data
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句