擅长:python、mysql、java
<p>每<a href="https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html" rel="nofollow noreferrer">numpy docs</a>:</p>
<blockquote>
<p>When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when:</p>
<ul>
<li>they are equal, or</li>
<li>one of them is 1</li>
</ul>
</blockquote>
<p>换言之,如果您尝试将两个矩阵相乘(在线性代数意义上),则需要<code>X.dot(y)</code>,但如果您尝试将标量从矩阵<code>y</code>广播到<code>X</code>,则需要执行<code>X * y.T</code>。</p>
<p><strong>示例:</strong></p>
<pre><code>>>> import numpy as np
>>>
>>> X = np.arange(8).reshape(4, 2)
>>> y = np.arange(2).reshape(1, 2) # create a 1x2 matrix
>>> X * y
array([[0,1],
[0,3],
[0,5],
[0,7]])
</code></pre>