从X数组中删除NaN行,并从Y中删除相应的行 [英] Remove NaN row from X array and also the corresponding row in Y

查看:110
本文介绍了从X数组中删除NaN行,并从Y中删除相应的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an X array with NaN and I can remove the row with NaN as such:

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

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

But I have a corresponding Y array

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]]

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

But something weird is going on:

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]]

推荐答案

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

You are getting rid of items which are NaN, not of rows with NaN. The proper thing to do would be:

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

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

To see the different behavior of both approaches:

>>> 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]]

这篇关于从X数组中删除NaN行,并从Y中删除相应的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆