Ale*_*vid 6 python arrays optimization numpy vectorization
我有两个感兴趣的矩阵,第一个是“词袋”矩阵,有两列:文档 ID 和术语 ID。例如:
bow[0:10]
Out[1]:
array([[ 0, 10],
[ 0, 12],
[ 0, 19],
[ 0, 20],
[ 1, 9],
[ 1, 24],
[ 2, 33],
[ 2, 34],
[ 2, 35],
[ 3, 2]])
Run Code Online (Sandbox Code Playgroud)
此外,我有一个“索引”矩阵,其中矩阵中的每一行都包含词袋矩阵中给定文档 ID 的第一行和最后一行的索引。例如:第 0 行是 doc id 0 的第一个和最后一个索引。例如:
index[0:4]
Out[2]:
array([[ 0, 4],
[ 4, 6],
[ 6, 9],
[ 9, 10]])
Run Code Online (Sandbox Code Playgroud)
我想要做的是随机抽取文档 ID 的样本并获取这些文档 ID 的所有单词行包。词袋矩阵大约有 150M 行(~1.5Gb),所以使用 numpy.in1d() 太慢了。我们需要快速返回这些以供下游任务使用。
我想出的天真解决方案如下:
def get_rows(ids):
indices = np.concatenate([np.arange(x1, x2) for x1,x2 in index[ids]])
return bow[indices]
get_rows([4,10,3,5])
Run Code Online (Sandbox Code Playgroud)
通用样本
提出问题的通用示例是这样的 -
indices = np.array([[ 4, 7],
[10,16],
[11,18]]
Run Code Online (Sandbox Code Playgroud)
预期的输出将是 -
array([ 4, 5, 6, 10, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 16, 17])
Run Code Online (Sandbox Code Playgroud)
想我终于用cumsum矢量化解决方案的技巧破解了它-
def create_ranges(a):
l = a[:,1] - a[:,0]
clens = l.cumsum()
ids = np.ones(clens[-1],dtype=int)
ids[0] = a[0,0]
ids[clens[:-1]] = a[1:,0] - a[:-1,1]+1
out = ids.cumsum()
return out
Run Code Online (Sandbox Code Playgroud)
样品运行 -
In [416]: a = np.array([[4,7],[10,16],[11,18]])
In [417]: create_ranges(a)
Out[417]: array([ 4, 5, 6, 10, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 16, 17])
In [425]: a = np.array([[-2,4],[-5,2],[11,12]])
In [426]: create_ranges(a)
Out[426]: array([-2, -1, 0, 1, 2, 3, -5, -4, -3, -2, -1, 0, 1, 11])
Run Code Online (Sandbox Code Playgroud)
如果我们将开始和停止作为两个1D数组,我们只需要使用它们代替第一列和第二列。为了完整起见,这里是完整的代码 -
def create_ranges(starts, ends):
l = ends - starts
clens = l.cumsum()
ids = np.ones(clens[-1],dtype=int)
ids[0] = starts[0]
ids[clens[:-1]] = starts[1:] - ends[:-1]+1
out = ids.cumsum()
return out
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |