相关疑难解决方法(0)

numpy elementwise外部产品

我想在numpy中做两个2d数组的元素外部产品.

A.shape = (100, 3) # A numpy ndarray
B.shape = (100, 5) # A numpy ndarray

C = element_wise_outer_product(A, B) # A function that does the trick
C.shape = (100, 3, 5) # This should be the result
C[i] = np.outer(A[i], B[i]) # This should be the result
Run Code Online (Sandbox Code Playgroud)

一个天真的实现可以如下.

tmp = []
for i in range(len(A):
    outer_product = np.outer(A[i], B[i])
    tmp.append(outer_product)
C = np.array(tmp)
Run Code Online (Sandbox Code Playgroud)

从堆栈溢出中获得更好的解决方案.

big_outer = np.multiply.outer(A, B)
tmp = np.swapaxes(tmp, 1, 2)
C_tmp = [tmp[i][i] for i …
Run Code Online (Sandbox Code Playgroud)

numpy vectorization matrix-multiplication elementwise-operations

8
推荐指数
1
解决办法
1532
查看次数