[英]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
),但一直收到此尺寸不匹配错误。 请帮忙。
因为 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.]])
您可以使用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.]]
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
,但将 arrays 调整为 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.]])
注意错误信息,并尝试从中学习。 你未来的编程自我会感谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.