[英]Fast way to apply function elementwise to a numpy array
I have a sets of numpy arrays which I create using 我有一组使用创建的numpy数组
for longtuple in itertools.product([0,1], repeat = n + m -1 ):
outputs = set(np.convolve(v, longtuple, 'valid').tostring() for v in itertools.product([0,1], repeat = m))
if (len(outputs) == 2**m):
print "Hooray!"
However I would actually like to take every element x of np.convolve(v, longtuple, 'valid')
and apply x >> k & 1
(for values of k that I will specify) and then add that resulting array to the set instead. 但是我实际上想获取np.convolve(v, longtuple, 'valid')
每个元素x并应用x >> k & 1
(对于我将指定的k值),然后将结果数组添加到集合中代替。 Is there an efficient way to do this? 有一种有效的方法可以做到这一点吗?
My use of set and tostring() is simply to see if there are any duplicates. 我使用set和tostring()只是为了查看是否有重复项。 I am not sure it is correct however. 我不确定这是正确的。
您可以只求卷积的结果并将表达式应用于它:
set((np.convolve(v, longtuple, 'valid') >> k & 1).tostring() for v in itertools.product([0,1], repeat = m))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.