如何忽略numpy数组中的NaN数据点,在Python中生成规范化的数据?

2024-04-28 09:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个numpy数组,它有一些float('nan'),我现在不想输入这些数据,我想首先将它们规范化,并将nan数据保持在原始空间,有什么方法可以做到这一点吗?

以前我在sklearn.Preprocessing中使用normalize函数,但该函数似乎不能接受任何包含NaN的数组作为输入。


Tags: 数据方法函数numpy空间数组sklearnnan
2条回答

可以使用^{}计算范数并忽略nan:

In [54]: x
Out[54]: array([  1.,   2.,  nan,   3.])

下面是忽略nan的规范:

In [55]: np.sqrt(np.nansum(np.square(x)))
Out[55]: 3.7416573867739413

y是规范化数组:

In [56]: y = x / np.sqrt(np.nansum(np.square(x)))

In [57]: y
Out[57]: array([ 0.26726124,  0.53452248,         nan,  0.80178373])

In [58]: np.linalg.norm(y[~np.isnan(y)])
Out[58]: 1.0

可以使用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

相关问题 更多 >