在二维 numpy 数组的末尾添加一列

Add a column at the end of a 2D numpy array

这个问题已经被问过很多次了。我还是想不出答案。对不起。 这里我举一个最小的例子:

import numpy as np

A=np.array([[1.,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(A)
x,y=A.shape
B=np.full(x,-1.0)
print(B)
#np.concatenate((A,B),1)
np.hstack((A,B))

我想要一个像这样的数组:

C=np.array([[1.,2,3,-1.],[4,5,6,-1.0],[7,8,9,-1.0],[10,11,12,-1.0]])
print(C)
>>>>
[[ 1.  2.  3. -1.]
 [ 4.  5.  6. -1.]
 [ 7.  8.  9. -1.]
 [10. 11. 12. -1.]]

我尝试了所有方法 (append, hstack, concatenate, insert),但一直收到此尺寸不匹配错误。请帮忙。

您可以使用 numpy.column_stack:

>>> A=np.array([[1.,2,3],[4,5,6],[7,8,9],[10,11,12]])
>>> x,y = A.shape
>>> B = np.full(x,-1.0)
>>> np.column_stack((A,B))
array([[ 1.,  2.,  3., -1.],
       [ 4.,  5.,  6., -1.],
       [ 7.,  8.,  9., -1.],
       [10., 11., 12., -1.]])

你也可以试试这个方法:

>>> B = np.full((x,y+1),-1)
>>> B[:,:-1] = A
>>> B
array([[ 1,  2,  3, -1],
       [ 4,  5,  6, -1],
       [ 7,  8,  9, -1],
       [10, 11, 12, -1]])

试试 numpy.insert():

import numpy as np

A = np.array([
    [1., 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]
])
# print(A)
A = np.insert(A, 3, values=[-1] * 4, axis=1)
print(A)

或者,更一般地,使用形状:

import numpy as np

A = np.array([
    [1., 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]
])
# print(A)
x, y = A.shape
A = np.insert(A, y, values=[-1] * x, axis=1)
print(A)

在这两种情况下,您应该得到:

[[ 1.  2.  3. -1.]
 [ 4.  5.  6. -1.]
 [ 7.  8.  9. -1.]
 [10. 11. 12. -1.]]

因为B是一维矩阵,而A是二维矩阵。 将B改为np.array([[-1.] for _ in range(x)]),则np.hstack((A,B))即可。

>>> B=np.array([[-1.] for _ in range(x)])
        
>>> B
        
array([[-1.],
       [-1.],
       [-1.],
       [-1.]])
>>> np.hstack((A,B))
        
array([[ 1.,  2.,  3., -1.],
       [ 4.,  5.,  6., -1.],
       [ 7.,  8.,  9., -1.],
       [10., 11., 12., -1.]])
In [14]: A=np.array([[1.,2,3],[4,5,6],[7,8,9],[10,11,12]])
    ...: print(A)
    ...: x,y=A.shape
    ...: B=np.full(x,-1.0)
    ...: print(B)
[[ 1.  2.  3.]
 [ 4.  5.  6.]
 [ 7.  8.  9.]
 [10. 11. 12.]]
[-1. -1. -1. -1.]
In [15]: A.shape
Out[15]: (4, 3)
In [16]: B.shape
Out[16]: (4,)

您的尺寸错误:

In [17]: np.concatenate((A,B), 1)
Traceback (most recent call last):
  File "<ipython-input-17-8dc80544006c>", line 1, in <module>
    np.concatenate((A,B), 1)
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

问题应该很明显了。一个数组是 (4,3),另一个是 (4,),2d 和 1d。

我们可以轻松地向 B 添加一个维度(B.reshape(4,1) 也可以):

In [18]: B[:,None].shape
Out[18]: (4, 1)
In [19]: np.concatenate((A,B[:,None]), 1)
Out[19]: 
array([[ 1.,  2.,  3., -1.],
       [ 4.,  5.,  6., -1.],
       [ 7.,  8.,  9., -1.],
       [10., 11., 12., -1.]])

尝试其他功能没有帮助。从 hstack 错误中可以明显看出,该错误只是将作业传递给 concatenate:

In [20]: np.hstack((A,B))
Traceback (most recent call last):
  File "<ipython-input-20-56593299da4e>", line 1, in <module>
    np.hstack((A,B))
  File "<__array_function__ internals>", line 5, in hstack
  File "/usr/local/lib/python3.8/dist-packages/numpy/core/shape_base.py", line 346, in hstack
    return _nx.concatenate(arrs, 1)
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

np.append 也一样。

column_stack 也使用 concatenate,但将数组调整为 2d:

In [22]: np.column_stack((A,B))
Out[22]: 
array([[ 1.,  2.,  3., -1.],
       [ 4.,  5.,  6., -1.],
       [ 7.,  8.,  9., -1.],
       [10., 11., 12., -1.]])

注意错误信息,并尝试从中吸取教训。你未来的编程自我会感谢你!