【发布时间】:2019-12-17 05:25:32
【问题描述】:
我想"multiply"(因为缺乏更好的描述)一个大小为 M 的 numpy 数组 X 和一个大小为 N 的更小的 numpy 数组 Y,对于 X 中的每 N 个元素。然后,我想sum 结果数组(几乎像dotproduct)。
我希望这个例子更清楚:
Example
X = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Y = [1,2,3]
Z = mymul(X, Y)
= [0*1, 1*2, 2*3, 3*1, 4*2, 5*3, 6*1, 7*2, 8*3, 9*1]
= [ 0, 2, 6, 3, 8, 15, 6, 14, 24, 9]
result = sum(Z) = 87
X 和 Y 可以有不同的长度,并且 Y 总是小于 X,但不一定能整除(例如 M % N != 0)
我有一些解决方案,但速度很慢。我希望有更快的方法来做到这一点。
import numpy as np
X = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int)
Y = np.array([1,2,3], dtype=int)
# these work but are slow for large X, Y
# simple for-loop
t = 0
for i in range(len(X)):
t += X[i] * Y[i % len(Y)]
print(t) #87
# extend Y M/N times so np.dot can be applied
Ytiled = np.tile(Y, int(np.ceil(len(X) / len(Y))))[:len(X)]
t = np.dot(X, Ytiled)
print(t) #87
【问题讨论】: