我正在寻找Python和/或Numpy矢量化的方法,以消除以下内容的for循环使用:
for i in list_range_values:
v[list_list_values[i]] += list_comp_values[i]
其中:
list_range_values是一个整数值的Python列表,例如。 [1,3,5],范围(0,R-1,1)
list_comp_values是数值的Python列表,例如。 [0.7,9.8,1.2,5,10,11.7,6,0.2]使len(list_comp_values)= R
v是长度为V的numpy向量,使得R可以是<,=,>比V
list_list_values是一个列表的Python列表(每个列表包含不同数量的整数值,例如[[3,6,7],[5,7,11,25,99],[8,45] ],[4,7,8],[0,1],[21,31,41],[9,11,22,33,44],[17,19]])从范围(0, V-1,1)和len(list_list_values)= R
例如
for i in list_range_values (= [1, 3, 5]):
i=1: v[[5, 7, 11, 25, 99]] += list_comp_values[1] (= 9.8)
i=3: v[[4, 7, 8]] += list_comp_values[3] (= 5)
i=5: v[[21, 31, 41]] += list_comp_values[5] (= 11.7)
是否有可用的方法可以消除for循环?
Cython,Scipy / Weave / Blitz和C模块是替代解决方案,但是想要确定是否首先存在Numpy矢量化答案。
答案 0 :(得分:6)
虽然它经常导致大规模的加速消除for循环并利用numpy内置插件/矢量化。我只想指出情况并非总是如此。定时简单的for循环与更多涉及的矢量化,不会给你一个大的加速,而且更冗长。需要考虑的事情:
from timeit import Timer
setstr="""import numpy as np
import itertools
import random
Nlists = 1000
total_lists = 5000
outsz = 100
maxsublistsz = 100
# create random list of lists
list_range_values = random.sample(xrange(total_lists),Nlists)
list_list_values = [random.sample(xrange(outsz),np.random.randint(1,maxsublistsz)) for k in xrange(total_lists)]
list_comp_values = 10*np.random.uniform(size=(total_lists,))
v = np.zeros((outsz,))
def indices(start, end):
lens = end - start
np.cumsum(lens, out=lens)
i = np.ones(lens[-1], dtype=int)
i[0] = start[0]
i[lens[:-1]] += start[1:]
i[lens[:-1]] -= end[:-1]
np.cumsum(i, out=i)
return i
def sum_by_group(values, groups):
order = np.argsort(groups)
groups = groups[order]
values = values[order]
values.cumsum(out=values)
index = np.ones(len(groups), 'bool')
index[:-1] = groups[1:] != groups[:-1]
values = values[index]
groups = groups[index]
values[1:] = np.diff(values)
return values, groups
"""
method1="""
list_list_lens = np.array(map(len, list_list_values))
comp_vals_expanded = np.repeat(list_comp_values, list_list_lens)
list_vals_flat = np.fromiter(itertools.chain.from_iterable(list_list_values),dtype=int)
list_list_starts = np.concatenate(([0], np.cumsum(list_list_lens)[:-1]))
toadd = indices(list_list_starts[list_range_values],(list_list_starts + list_list_lens)[list_range_values])
v[list_vals_flat[toadd]] += comp_vals_expanded[toadd]
"""
method2="""
for k in list_range_values:
v[list_list_values[k]] += list_comp_values[k]
"""
method3="""
llv = [list_list_values[i] for i in list_range_values]
lcv = [list_comp_values[i] for i in list_range_values]
counts = map(len, llv)
indices = np.concatenate(llv)
values = np.repeat(lcv, counts)
totals, indices_unique = sum_by_group(values, indices)
v[indices_unique] += totals
"""
t1 = Timer(method1,setup=setstr).timeit(100)
print t1
t2 = Timer(method2,setup=setstr).timeit(100)
print t2
t3 = Timer(method3,setup=setstr).timeit(100)
print t3
对于列表中的大量元素:
方法1 :(不用于循环-jterrace)1.43秒
方法2 :( for loop)4.62秒
方法3 :(不用于循环 - bago)2.99秒
对于少量列表(将Nlists
更改为10),for循环明显快于jterrace的解决方案:
方法1 :(不用于循环-jterrace)1.05秒
方法2 :( for循环)0.045秒
方法3 :(不用于循环 - bago)0.041秒
这不是要敲@jterrace或@ bago的解决方案,这是非常优雅的。相反,它要指出的是,通常简单的for循环不能很好地执行。
答案 1 :(得分:2)
使用您的示例输入:
>>> list_list_values = [[3, 6, 7], [5, 7, 11, 25, 99], [8, 45], [4, 7, 8],
[0, 1], [21, 31, 41], [9, 11, 22, 33, 44], [17, 19]]
>>> list_comp_values = [0.7, 9.8, 1.2, 5, 10, 11.7, 6, 0.2]
>>> list_range_values = [1, 3, 5]
首先,一些发电机恶作剧:
>>> indices_weights = ((list_list_values[i], list_comp_values[i])
for i in list_range_values)
>>> flat_indices_weights = ((i, weight) for indices, weight in indices_weights
for i in indices)
现在我们将数据传递给numpy
。我无法弄清楚如何从迭代器生成rec.array
,所以我不得不将上面的生成器转换为列表。也许有办法避免......
>>> i_w = numpy.rec.array(list(flat_indices_weights),
dtype=[('i', int), ('weight', float)])
>>> numpy.histogram(i_w['i'], bins=range(0, 100), weights=i_w['weight'])
(array([ 0. , 0. , 0. , 0. , 5. , 9.8, 0. , 14.8, 5. ,
0. , 0. , 9.8, 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 11.7, 0. , 0. , 0. , 9.8, 0. ,
0. , 0. , 0. , 0. , 11.7, 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 11.7, 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 9.8]),
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]))
我有一点时间用自己的几个来跟进JoshAdel的测试。到目前为止,最快的解决方案是使用Bago的设置,但用sum_by_group
函数取代histogram
函数。以下是我获得的数字(已更新):
Method1(jterrace):2.65
方法2(for循环):2.25
Method3(Bago):1.14
Method4(直方图):2.82
方法5(3/4组合):1.07
请注意,如此处所实现的,第一种方法根据我的测试给出了错误的结果。我没有时间弄清问题是什么。我的测试代码如下;它只是轻轻地调整了JoshAdel的原始代码,但为了方便我在这里发布了它。 (更新后包括Bago的评论,并且有点说不清楚。)
from timeit import Timer
setstr="""import numpy as np
import itertools
import random
Nlists = 1000
total_lists = 5000
outsz = 100
maxsublistsz = 100
# create random list of lists
list_range_values = random.sample(xrange(total_lists),Nlists)
list_list_values = [random.sample(xrange(outsz),np.random.randint(1,maxsublistsz)) for k in xrange(total_lists)]
list_comp_values = list(10*np.random.uniform(size=(total_lists,)))
v = np.zeros((outsz,))
def indices(start, end):
lens = end - start
np.cumsum(lens, out=lens)
i = np.ones(lens[-1], dtype=int)
i[0] = start[0]
i[lens[:-1]] += start[1:]
i[lens[:-1]] -= end[:-1]
np.cumsum(i, out=i)
return i
def sum_by_group(values, groups):
order = np.argsort(groups)
groups = groups[order]
values = values[order]
values.cumsum(out=values)
index = np.ones(len(groups), 'bool')
index[:-1] = groups[1:] != groups[:-1]
values = values[index]
groups = groups[index]
values[1:] = np.diff(values)
return values, groups
"""
setstr_test = setstr + "\nprint_v = True\n"
method1="""
list_list_lens = np.array(map(len, list_list_values))
comp_vals_expanded = np.repeat(list_comp_values, list_list_lens)
list_vals_flat = np.fromiter(itertools.chain.from_iterable(list_list_values),dtype=int)
list_list_starts = np.concatenate(([0], np.cumsum(list_list_lens)[:-1]))
toadd = indices(list_list_starts[list_range_values],(list_list_starts + list_list_lens)[list_range_values])
v[list_vals_flat[toadd]] += comp_vals_expanded[toadd]
"""
method2="""
for k in list_range_values:
v[list_list_values[k]] += list_comp_values[k]
"""
method3="""
llv = [np.fromiter(list_list_values[i], 'int') for i in list_range_values]
lcv = [list_comp_values[i] for i in list_range_values]
counts = map(len, llv)
indices = np.concatenate(llv)
values = np.repeat(lcv, counts)
totals, indices_unique = sum_by_group(values, indices)
v[indices_unique] += totals
"""
method4="""
indices_weights = ((list_list_values[i], list_comp_values[i]) for i in list_range_values)
flat_indices_weights = ((i, weight) for indices, weight in indices_weights for i in indices)
i_w = np.rec.array(list(flat_indices_weights), dtype=[('i', 'i'), ('weight', 'd')])
v += np.histogram(i_w['i'], bins=range(0, outsz + 1), weights=i_w['weight'], new=True)[0]
"""
method5="""
llv = [np.fromiter(list_list_values[i], 'int') for i in list_range_values]
lcv = [list_comp_values[i] for i in list_range_values]
counts = map(len, llv)
indices = np.concatenate(llv)
values = np.repeat(lcv, counts)
v += np.histogram(indices, bins=range(0, outsz + 1), weights=values, new=True)[0]
"""
t1 = Timer(method1,setup=setstr).timeit(100)
print t1
t2 = Timer(method2,setup=setstr).timeit(100)
print t2
t3 = Timer(method3,setup=setstr).timeit(100)
print t3
t4 = Timer(method4,setup=setstr).timeit(100)
print t4
t5 = Timer(method5,setup=setstr).timeit(100)
print t5
exec(setstr_test + method1 + "\nprint v\n")
exec("\nv = np.zeros((outsz,))\n" + method2 + "\nprint v\n")
exec("\nv = np.zeros((outsz,))\n" + method3 + "\nprint v\n")
exec("\nv = np.zeros((outsz,))\n" + method4 + "\nprint v\n")
exec("\nv = np.zeros((outsz,))\n" + method5 + "\nprint v\n")
答案 2 :(得分:1)
首先,设置你给出的变量:
import numpy as np
list_range_values = [1, 3, 5]
list_list_values = [[3, 6, 7], [5, 7, 11, 25, 99], [8, 45],
[4, 7, 8], [0, 1], [21, 31, 41]]
list_comp_values = [0.7, 9.8, 1.2, 5, 10, 11.7]
v = np.arange(100, dtype=float)
接下来,list_list_values
和list_comp_values
需要展平,因此它们是连续的:
list_list_lens = np.array(map(len, list_list_values))
comp_vals_expanded = np.repeat(list_comp_values, list_list_lens)
import itertools
list_vals_flat = np.fromiter(itertools.chain.from_iterable(list_list_values),
dtype=int)
然后,需要每个子阵列的起始索引:
list_list_starts = np.concatenate(([0], np.cumsum(list_list_lens)[:-1]))
现在我们有了起始值和结束值,我们可以使用indices function from this question来获取一个选择器索引数组:
def indices(start, end):
lens = end - start
np.cumsum(lens, out=lens)
i = np.ones(lens[-1], dtype=int)
i[0] = start[0]
i[lens[:-1]] += start[1:]
i[lens[:-1]] -= end[:-1]
np.cumsum(i, out=i)
return i
toadd = indices(list_list_starts[list_range_values],
(list_list_starts + list_list_lens)[list_range_values])
现在我们已经完成了所有这些魔术,可以像这样添加数组:
v[list_vals_flat[toadd]] += comp_vals_expanded[toadd]