[英]Removing Corresponding Entries from Two Numpy Arrays
I have what I'm quite sure is a simple question, but I'm not having much luck finding an explanation online. 我很确定这是一个简单的问题,但是在网上找到解释并不太幸运。
I have an array of flux values and a corresponding array of time values. 我有一个磁通值数组和一个对应的时间值数组。 Obviously those two arrays are one-to-one (one flux value for each time value).
显然,这两个数组是一对一的(每个时间值一个通量值)。 However, some of my flux values are NaNs.
但是,我的一些通量值是NaNs。
My question is this: How do I remove the corresponding values from the time array when I remove the NaNs from the flux array? 我的问题是:从通量数组中删除NaN时,如何从时间数组中删除相应的值?
These arrays are large enough (several thousand entries) that it would be exceedingly cumbersome to do it by hand. 这些数组足够大(数千个条目),以手工完成将非常麻烦。
You could try boolean indexing: 您可以尝试布尔索引:
In [13]: time
Out[13]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
In [15]: flux
Out[15]: array([ 1., 1., 1., 1., 1., nan, nan, nan, 1., 1., 1.])
In [16]: time2 = time[~np.isnan(flux)]
In [17]: flux2 = flux[~np.isnan(flux)]
In [18]: time2
Out[18]: array([ 0., 1., 2., 3., 4., 8., 9., 10.])
In [19]: flux2
Out[19]: array([ 1., 1., 1., 1., 1., 1., 1., 1.])
Just write time = time[~np.isnan(flux)]
etc. if you don't need the original arrays any more. 如果您不再需要原始数组,只需编写
time = time[~np.isnan(flux)]
等。
A more complicated way is to use masked arrays : 一种更复杂的方法是使用掩码数组 :
In [20]: m = np.ma.masked_invalid(flux)
In [21]: time2 = time[~m.mask]
In [22]: time2
Out[22]: array([ 0., 1., 2., 3., 4., 8., 9., 10.])
In [23]: flux2
Out[23]: array([ 1., 1., 1., 1., 1., 1., 1., 1.])
In [22]: flux2 = flux[~m.mask]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.