背景
我有一个用零初始化的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)
我想对此操作进行矢量化
这正如我所期望的那样,但为了它的乐趣,我想对操作进行矢量化。我对此并不十分熟悉,而且我在线搜索并没有让我走上正轨。
如果可能的话,我真的很感激有关如何将其转换为矢量操作的任何建议。
答案 0 :(得分:5)
实现的技巧是我们将-1s
置于每个起始点,并在零初始化int数组的每个端点放置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)
。接下来是实际技巧,因为我们会累计求和,给出bin(起止 - 对)边界所覆盖的位置的非零数字。因此,最后一步是为最终输出寻找非零作为布尔数组。因此,我们将有两个矢量化解决方案,其实现如下所示 -
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
示例运行 -
{{1}}
答案 1 :(得分:1)
您已经使用切片分配完成了最重要的矢量化,但由于python不支持“多切片”,因此无法使用切片完全向量化。
如果您真的非常想使用矢量化,您可以创建一个数组 “真实”指数,像这样
indices = np.r_[tuple(slice(row.start, row.end) for row in df.itertuples())]
section[indices] = True
但这很可能会变慢,因为它会创建一个带索引的新临时数组。
据说,你可以通过减少重复工作来获得一些加速。具体来说,您可以使用union of the ranges,为您提供一组不相交的集合。
在您的情况下,第一个区间与最后一个区间重叠,因此您的数据帧等同于
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