I am wondering if there is a quicker way/dedicated NumPy function to perform element-wise multiplication of 2D NumPy arrays and then sum all the elements.
I currently use np.sum(np.multiply(A, B))
where A, B are NumPy arrays of equal dimension m x n
.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use np.tensordot
-
np.tensordot(A,B, axes=((0,1),(0,1)))
Another way with np.dot
after flattening the inputs -
A.ravel().dot(B.ravel())
Another with np.einsum
-
np.einsum('ij,ij',A,B)
Sample run -
In [14]: m,n = 4,5
In [15]: A = np.random.rand(m,n)
In [16]: B = np.random.rand(m,n)
In [17]: np.sum(np.multiply(A, B))
Out[17]: 5.1783176986341335
In [18]: np.tensordot(A,B, axes=((0,1),(0,1)))
Out[18]: array(5.1783176986341335)
In [22]: A.ravel().dot(B.ravel())
Out[22]: 5.1783176986341335
In [21]: np.einsum('ij,ij',A,B)
Out[21]: 5.1783176986341326
Runtime test
In [23]: m,n = 5000,5000
In [24]: A = np.random.rand(m,n)
...: B = np.random.rand(m,n)
...:
In [25]: %timeit np.sum(np.multiply(A, B))
...: %timeit np.tensordot(A,B, axes=((0,1),(0,1)))
...: %timeit A.ravel().dot(B.ravel())
...: %timeit np.einsum('ij,ij',A,B)
...:
10 loops, best of 3: 52.2 ms per loop
100 loops, best of 3: 19.5 ms per loop
100 loops, best of 3: 19.5 ms per loop
100 loops, best of 3: 19 ms per loop