繁体   English   中英

从具有索引的数组填充1D numpy数组

[英]Fill 1D numpy array from arrays with indices

背景

我有一个用零初始化的1D NumPy数组。

import numpy as np
section = np.zeros(1000)

然后我有一个Pandas DataFrame,其中我有两列索引:

d= {'start': {0: 7200, 1: 7500, 2: 7560, 3: 8100, 4: 11400},
    'end': {0: 10800, 1: 8100, 2: 8100, 3: 8150, 4: 12000}}

df = pd.DataFrame(data=d, columns=['start', 'end'])

对于每对索引,我想将numpy数组中相应索引的值设置为True。

我目前的解决方案

我可以通过将函数应用于DataFrame来实现:

def fill_array(row):
    section[row.start:row.end] = True

df.apply(fill_array, axis=1)

我想矢量化这个操作

这正如我所料,但为了它的乐趣,我想矢量化操作。 我对此并不十分熟悉,而且我在线搜索并没有让我走上正轨。

如果可能的话,我真的很感激有关如何将其转换为矢量操作的任何建议。

实现的技巧是我们在每个起始点放置1s ,在零初始化int数组的每个端点放置-1s 接下来是实际技巧,因为我们会累计求和,给出bin(起止 - 对)边界所覆盖的位置的非零数字。 因此,最后一步是为最终输出寻找非零作为布尔数组。 因此,我们将有两个矢量化解决方案,其实现如下所示 -

def filled_array(start, end, length):
    out = np.zeros((length), dtype=int)
    np.add.at(out,start,1)
    np.add.at(out,end,-1)
    return out.cumsum()>0

def filled_array_v2(start, end, length): #Using @Daniel's suggestion
    out =np.bincount(start, minlength=length) - np.bincount(end, minlength=length)
    return out.cumsum().astype(bool)

样品运行 -

In [2]: start
Out[2]: array([ 4,  7,  5, 15])

In [3]: end
Out[3]: array([12, 12,  7, 17])

In [4]: out = filled_array(start, end, length=20)

In [7]: pd.DataFrame(out) # print as dataframe for easy verification
Out[7]: 
        0
0   False
1   False
2   False
3   False
4    True
5    True
6    True
7    True
8    True
9    True
10   True
11   True
12  False
13  False
14  False
15   True
16   True
17  False
18  False
19  False

矢量

您已经使用切片赋值完成了最重要的矢量化,但由于python不支持“多切片”,因此无法使用切片完全向量化。

如果您真的非常想要使用矢量化,您可以使用“True”索引创建一个数组,就像这样

indices = np.r_[tuple(slice(row.start, row.end) for row in df.itertuples())]
section[indices] = True

但这很可能会变慢,因为它会创建一个带索引的新临时数组。

删除重复的工作

有了这个说,你可以通过减少重复工作获得一些加速。 具体来说,您可以使用范围的并集,为您提供一组不相交的集合。

在您的情况下,第一个区间与最后一个区间重叠,因此您的数据帧相当于

d= {'start': {0: 7200, 1: 11400},
    'end': {0: 10800, 1: 12000}}

这可以减少高达60%的工作量! 但首先我们需要找到这些间隔。 根据上面的答案,我们可以通过以下方式实现:

slices = [(row.start, row.end) for row in df.itertuples()]
slices_union = []
for start, end in sorted(slices):
    if slices_union and slices_union[-1][1] >= start - 1:
        slices_union[-1][1] = max(slices_union[-1][1], end)
    else:
        slices_union.append([start, end])

然后你就可以使用这些(希望更小的切片)

for start, end in slices_union:
    section[start:end] = True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM