<p><strong>@yx131</strong>,您可以查看下面的代码,以清楚地了解元组及其在定义numpy数组的形状时的用法。在</p>
<blockquote>
<p><strong>Note</strong>: Do not forget to see the code below as it has explanation of the problems related to <strong>Broadcasting in numpy</strong>.</p>
</blockquote>
<p>同时查看纽比的广播规则
<a href="https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html" rel="nofollow noreferrer">https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html</a>。在</p>
<blockquote>
<p>There's a difference between <code>(2)</code> and <code>(2,)</code>. The first one is a literal value <code>2</code> where as the 2nd one is a tuple.</p>
<p><code>(2,)</code> is 1 item tuple and <code>(2, 2)</code> is 2 items tuple. It is clear in the code example.</p>
<p><strong>Note</strong>: In case of numpy arrays, <code>(2,)</code> denotes shape of 1 dimensional array of 2 items and <code>(2, 2)</code> denotes the shape of 2 dimensional array (matrix) with 2 rows and 2 colums. If you want to add 2 arrays then their shape should be same.</p>
</blockquote>
<pre><code>v = (2) # Assignment of value 2
t = (2,) # Comma is necessary at the end of item to define 1 item tuple, it is not required in case of list
t2 = (2, 1) # 2 item tuple
t3 = (3, 4) # 2 item tuple
print(v, type(v))
print(t, type(t))
print(t2, type(t2))
print(t3, type(t3))
print(t + t2)
print(t2 + t3)
"""
2 <class 'int'>
(2,) <class 'tuple'>
(2, 1) <class 'tuple'>
(3, 4) <class 'tuple'>
(2, 2, 1)
(2, 1, 3, 4)
"""
</code></pre>
<p>现在,让我们看看下面的代码,找出与广播相关的错误。都和维度有关。在</p>
^{pr2}$
<p>所以在您的例子中,问题与要添加的不匹配维度(根据<a href="https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html" rel="nofollow noreferrer">numpy's broadcasting</a>)有关。谢谢。在</p>