2013-05-07 141 views

回答

4

我更喜歡下面的語法,這是短期的,但明確

A = np.ones((3,4)) 
B = np.arange(3) 
print A * B[:,None] 

>>> array([[ 0., 0., 0., 0.], 
     [ 1., 1., 1., 1.], 
     [ 2., 2., 2., 2.]]) 

A = np.ones((4,3)) 
B = np.arange(3) 
print A * B[None,:] 
>>> array([[ 0., 1., 2.], 
     [ 0., 1., 2.], 
     [ 0., 1., 2.], 
     [ 0., 1., 2.]]) 
2

1.柱乘法

In [39]: A = array([[1,2,3],[1,2,3],[1,2,3],[1,2,3]]) 

In [40]: X = array([10,20,30]) 

In [41]: A 
Out[41]: 
array([[1, 2, 3], 
     [1, 2, 3], 
     [1, 2, 3], 
     [1, 2, 3]]) 

In [42]: X 
Out[42]: array([10, 20, 30]) 

In [43]: A * X 
Out[43]: 
array([[10, 40, 90], 
     [10, 40, 90], 
     [10, 40, 90], 
     [10, 40, 90]]) 

1行乘法

In [44]: B = array([[1,1,1,1],[2,2,2,2],[3,3,3,3]]) 

In [45]: B 
Out[45]: 
array([[1, 1, 1, 1], 
     [2, 2, 2, 2], 
     [3, 3, 3, 3]]) 

In [46]: X = array([10,20,30]) 

In [47]: X 
Out[47]: array([10, 20, 30]) 

In [48]: (B.transpose() * X).transpose() 
Out[48]: 
array([[10, 10, 10, 10], 
     [40, 40, 40, 40], 
     [90, 90, 90, 90]]) 
相關問題