<p>要使用<code>np.concatenate</code>,我们需要将第二个数组扩展到<code>2D</code>,然后沿<code>axis=1</code>连接-</p>
<pre><code>np.concatenate((a,b[:,None]),axis=1)
</code></pre>
<p>或者,我们可以使用<code>np.column_stack</code>来处理它-</p>
<pre><code>np.column_stack((a,b))
</code></pre>
<p>样本运行-</p>
<pre><code>In [84]: a
Out[84]:
array([[54, 30, 55, 12],
[64, 94, 50, 72],
[67, 31, 56, 43],
[26, 58, 35, 14],
[97, 76, 84, 52]])
In [85]: b
Out[85]: array([56, 70, 43, 19, 16])
In [86]: np.concatenate((a,b[:,None]),axis=1)
Out[86]:
array([[54, 30, 55, 12, 56],
[64, 94, 50, 72, 70],
[67, 31, 56, 43, 43],
[26, 58, 35, 14, 19],
[97, 76, 84, 52, 16]])
</code></pre>
<p>如果<code>b</code>是由<code>(1,)</code>形状的<code>dtype=object</code>组成的<code>1D</code>数组,那么很可能所有数据都包含在其中唯一的元素中,我们需要在连接之前将其展平。为此,我们也可以对它使用<code>np.concatenate</code>。下面是一个样本来说明这一点-</p>
<pre><code>In [118]: a
Out[118]:
array([[54, 30, 55, 12],
[64, 94, 50, 72],
[67, 31, 56, 43],
[26, 58, 35, 14],
[97, 76, 84, 52]])
In [119]: b
Out[119]: array([array([30, 41, 76, 13, 69])], dtype=object)
In [120]: b.shape
Out[120]: (1,)
In [121]: np.concatenate((a,np.concatenate(b)[:,None]),axis=1)
Out[121]:
array([[54, 30, 55, 12, 30],
[64, 94, 50, 72, 41],
[67, 31, 56, 43, 76],
[26, 58, 35, 14, 13],
[97, 76, 84, 52, 69]])
</code></pre>