[英]element wise combine list of numpy arrays
I am trying to do some linear combination of numpy arrays. 我正在尝试做一些numpy数组的线性组合。
I have three lists of numpy arrays: 我有numpy数组的三个列表:
a = [np.random.normal(0,1, [1,2]), np.random.normal(0,1, [3,4]), np.random.normal(0,1, [10,11])] b = [np.random.normal(0,1, [1,2]), np.random.normal(0,1, [3,4]), np.random.normal(0,1, [10,11])] c = [np.random.normal(0,1, [1,2]), np.random.normal(0,1, [3,4]), np.random.normal(0,1, [10,11])]
I want to element-wise combine each element in each array in list a
and b
based on corresponding element's value of c
, to get a new list d
: say d_i = a_i * c_i + (1-c_i) *b_i
(linear combination). 我想根据对应元素的
c
值将列表a
和b
中每个数组中的每个元素逐元素组合,以获得新的列表d
:说d_i = a_i * c_i + (1-c_i) *b_i
(线性组合) 。
What I thought was to pick each element in each array in a
and find corresponding elements in b
and c
and then combine. 我的想法是在
a
中的每个数组中选取每个元素,然后在b
和c
找到相应的元素,然后合并。 However, I found this is troublesome, inefficient and a bit stupid. 但是,我发现这很麻烦,效率低下并且有点愚蠢。 Could anyone suggest a better way?
有人可以提出更好的方法吗?
Well assuming all of your lists are the same length then I don't think that there is going to be anything much more efficient than 好吧,假设您所有列表的长度都相同,那么我认为没有比这更有效的方法了
d = [a[i] * c[i] + (1-c[i]) * b[i] for i in range(len(a))]
Now if all you need to do is operate upon the list d
one time then maybe you could speed things up with a generator comprehension? 现在,如果您需要做的只是对列表
d
一次操作,那么也许您可以借助生成器理解来加快处理速度?
d = (a[i] * c[i] + (1-c[i]) * b[i] for i in range(len(a)))
But at the end of the day there is no way to create a linear combination of elements in less than linear time. 但是最终,没有办法在少于线性时间内创建元素的线性组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.