Knu*_*sen 3 python arrays numpy vectorization pandas
背景
我有一个用零初始化的1D NumPy数组.
import numpy as np
section = np.zeros(1000)
Run Code Online (Sandbox Code Playgroud)
然后我有一个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'])
Run Code Online (Sandbox Code Playgroud)
对于每对索引,我想将numpy数组中相应索引的值设置为True.
我目前的解决方案
我可以通过将函数应用于DataFrame来实现:
def fill_array(row):
section[row.start:row.end] = True
df.apply(fill_array, axis=1)
Run Code Online (Sandbox Code Playgroud)
我想矢量化这个操作
这正如我所料,但为了它的乐趣,我想矢量化操作.我对此并不十分熟悉,而且我在线搜索并没有让我走上正轨.
如果可能的话,我真的很感激有关如何将其转换为矢量操作的任何建议.
实现遵循的技巧是我们将1s在-1s零初始化int数组的每个起始点和每个端点放置.接下来是实际技巧,因为我们会累计求和,给出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)
Run Code Online (Sandbox Code Playgroud)
样品运行 -
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
Run Code Online (Sandbox Code Playgroud)