从 X 数组中删除 NaN 行以及 Y 中的相应行

Posted

技术标签:

【中文标题】从 X 数组中删除 NaN 行以及 Y 中的相应行【英文标题】:Remove NaN row from X array and also the corresponding row in Y 【发布时间】:2015-02-16 09:49:33 【问题描述】:

我有一个带有 NaN 的 X 数组,我可以像这样删除带有 NaN 的行:

import numpy as np
x = x[~np.isnan(x)]

但是我有一个对应的Y数组

assert len(x) == len(y) # True
x = x[~np.isnan(x)]
assert len(x) == len(y) # False and breaks

如何从 Y 数组中删除对应的行?

我的 X 数组如下所示:

>>> x
[[ 2.67510434  2.67521927  3.49296989  3.80100625  4.          2.83631844]
 [ 3.47538057  3.4752436   3.62245715  4.0720535   5.          3.7773169 ]
 [ 2.6157049   2.61583852  3.48335887  3.78088813  0.          2.78791096]
 ..., 
 [ 3.60408952  3.60391203  3.64328267  4.1156462   5.          3.77933333]
 [ 2.66773792  2.66785516  3.49177798  3.7985113   4.          2.83631844]
 [ 3.26622238  3.26615124  3.58861468  4.00121327  5.          3.49693169]]

但是发生了一些奇怪的事情:

indexes = ~np.isnan(x)
print indexes

[出]:

[[ True  True  True  True  True  True]
 [ True  True  True  True  True  True]
 [ True  True  True  True  True  True]
 ..., 
 [ True  True  True  True  True  True]
 [ True  True  True  True  True  True]
 [ True  True  True  True  True  True]]

【问题讨论】:

你的意思是上面的y = y[~np.isnan(x)]?别忘了在此声明之后致电x = x[~np.isnan(x)] @xnx,是的,没错,我很傻...... 试试np.mat(x)[~np.isnan(x)]np.array(x)[~np.isnan(x)] 将返回一个一维数组,而 np.mat 将保持其尺寸。 它还在给IndexError: too many indices for array 【参考方案1】:

您正在摆脱 NaN 的项目,而不是带有 NaN 的行。正确的做法是:

mask = ~np.any(np.isnan(x), axis=1)
x = x[mask]
y = y[mask]

要查看两种方法的不同行为:

>>> x = np.random.rand(4, 5)
>>> x[[0, 2], [1, 4]] = np.nan
>>> x
array([[ 0.37499461,         nan,  0.51254549,  0.5253203 ,  0.3955948 ],
       [ 0.73817831,  0.70381481,  0.45222295,  0.68540433,  0.76113544],
       [ 0.1651173 ,  0.41594257,  0.66327842,  0.86836192,         nan],
       [ 0.70538764,  0.31702821,  0.04876226,  0.53867849,  0.58784935]])
>>> x[~np.isnan(x)]  # 1D array with NaNs removed
array([ 0.37499461,  0.51254549,  0.5253203 ,  0.3955948 ,  0.73817831,
        0.70381481,  0.45222295,  0.68540433,  0.76113544,  0.1651173 ,
        0.41594257,  0.66327842,  0.86836192,  0.70538764,  0.31702821,
        0.04876226,  0.53867849,  0.58784935])
>>> x[~np.any(np.isnan(x), axis=1)]  # 2D array with rows with NaN removed
array([[ 0.73817831,  0.70381481,  0.45222295,  0.68540433,  0.76113544],
       [ 0.70538764,  0.31702821,  0.04876226,  0.53867849,  0.58784935]]

【讨论】:

对我来说,~np.any(np.isnan(x, axis=1)) 返回错误:TypeError: 'axis' is an invalid keyword to ufunc 'isnan' 括号的位置我搞砸了,应该是~np.any(np.isnan(x), axis=1)【参考方案2】:
indexes = ~np.isnan(x)
x = x[indexes]
y = y[indexes]

【讨论】:

我收到了IndexError: too many indices for array 的答案以及@xnx 方法。 您确定xy 的长度相同吗? 牛津词典,参见例如english.stackexchange.com/questions/61080/… @Bart 我很欣赏引用,因此接受索引;然而,引文使问题悬而未决,因为我是一名科学家,所以我坚持“指数”;) @Chris8447 ~invert 运算符,即~np.array([True, False]) == np.array([False, True])。见docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html

以上是关于从 X 数组中删除 NaN 行以及 Y 中的相应行的主要内容,如果未能解决你的问题,请参考以下文章

从数据框中删除不包括一组列的列中的nan行。

数据清洗--DataFrame中的空值处理

如何使用NodeJs中的对象数组选择行

js删除数组中的'NaN'

如果前五列中的 NaN 则删除行

删除行和 ValueError 数组的长度不同