2016-09-13 38 views
2

说我有两个矩阵BM,我想执行以下语句:新增多个没有建立一个新的

B += 3*M 

我执行该指令多次,所以我不希望建立每次矩阵3*M3可能会改变,这只是为了让我只做一个标量矩阵产品)。它是一个numpy函数,它使得这个计算“到位”?

更确切地说,我有标量as列表和矩阵Ms的名单,我想执行“点积”(这是不是真的一个,因为两个操作数是不同类型的)两个,也就是说:

sum(a*M for a, M in zip(as, Ms)) 

np.dot功能没有做什么,我除外...

回答

5

您可以使用np.tensordot -

np.tensordot(As,Ms,axes=(0,0)) 

或者np.einsum -

np.einsum('i,ijk->jk',As,Ms) 

采样运行 -

In [41]: As = [2,5,6] 

In [42]: Ms = [np.random.rand(2,3),np.random.rand(2,3),np.random.rand(2,3)] 

In [43]: sum(a*M for a, M in zip(As, Ms)) 
Out[43]: 
array([[ 6.79630284, 5.04212877, 10.76217631], 
     [ 4.91927651, 1.98115548, 6.13705742]]) 

In [44]: np.tensordot(As,Ms,axes=(0,0)) 
Out[44]: 
array([[ 6.79630284, 5.04212877, 10.76217631], 
     [ 4.91927651, 1.98115548, 6.13705742]]) 

In [45]: np.einsum('i,ijk->jk',As,Ms) 
Out[45]: 
array([[ 6.79630284, 5.04212877, 10.76217631], 
     [ 4.91927651, 1.98115548, 6.13705742]]) 
1

另一种方法可以做到这一点,特别是如果你青睐的可读性,是利用broadcasting

所以,你可以从一维和二维阵列进行三维数组,然后在合适的轴总结:

>>> Ms = np.random.randn(4, 2, 3) # 4 arrays of size 2x3 
>>> As = np.random.randn(4) 
>>> np.sum(As[:, np.newaxis, np.newaxis] * Ms) 
array([[-1.40199248, -0.40337845, -0.69986566], 
     [ 3.52724279, 0.19547118, 2.1485559 ]]) 
>>> sum(a*M for a, M in zip(As, Ms)) 
array([[-1.40199248, -0.40337845, -0.69986566], 
     [ 3.52724279, 0.19547118, 2.1485559 ]]) 

不过,值得注意的是,np.einsumnp.tensordot通常更有效:

>>> %timeit np.sum(As[:, np.newaxis, np.newaxis] * Ms, axis=0) 
The slowest run took 7.38 times longer than the fastest. This could mean that an intermediate result is being cached. 
100000 loops, best of 3: 8.58 µs per loop 
>>> %timeit np.einsum('i,ijk->jk', As, Ms) 
The slowest run took 19.16 times longer than the fastest. This could mean that an intermediate result is being cached. 
100000 loops, best of 3: 2.44 µs per loop 

这也适用于更大的数字:

>>> Ms = np.random.randn(100, 200, 300) 
>>> As = np.random.randn(100) 
>>> %timeit np.einsum('i,ijk->jk', As, Ms) 
100 loops, best of 3: 5.03 ms per loop 
>>> %timeit np.sum(As[:, np.newaxis, np.newaxis] * Ms, axis=0) 
100 loops, best of 3: 14.8 ms per loop 
>>> %timeit np.tensordot(As,Ms,axes=(0,0)) 
100 loops, best of 3: 2.79 ms per loop 

所以np.tensordot在这种情况下效果最好。

使用np.sum和广播的唯一好理由是使代码更具可读性(当你有小矩阵时有帮助)。

相关问题