【问题标题】:Add a column at the end of a 2D numpy array在 2D numpy 数组的末尾添加一列
【发布时间】:2021-04-24 16:40:33
【问题描述】:

这个问题已经被问过很多次了。我仍然无法弄清楚答案。对不起。 这里我举一个最小的例子:

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.array 给出(至少在打印时)逗号分隔的数组(如在下面提到的操作之后),有时不给出(如问题中的我的 A 数组)。是有显着差异还是它们的行为相似?
  • numpy 数组的 print(X) 省略了逗号。 repr 显示包括它们(以及 np.array。目的是通过微妙的方式将它们与列表区分开来。

标签: python arrays numpy insert append


【解决方案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.]])

【讨论】:

    【解决方案2】:
    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,但将数组调整为二维:

    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.]])
    

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

    【讨论】:

      【解决方案3】:

      试试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.]]
      

      【讨论】:

        【解决方案4】:

        你可以使用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]])
        
        

        【讨论】:

          猜你喜欢
          • 2020-10-14
          • 2020-10-01
          • 1970-01-01
          • 2021-02-07
          • 2017-09-02
          • 1970-01-01
          • 1970-01-01
          • 2014-03-22
          • 2019-05-15
          相关资源
          最近更新 更多