【问题标题】:Append arrays of different dimensions to get a single array附加不同维度的数组以获得单个数组
【发布时间】:2018-05-11 00:11:06
【问题描述】:

我有三个向量(numpy 数组),vector_1, vector_2, vector_3 如下:

维度(vector1)=(200,2048)

维度(vector2)=(200,8192)

维度(vector3)=(200,32768)

我想附加这些向量以获得vector_4

维度(vector4)= (200,2048+8192+32768)= (200, 43008)

分别添加vector1然后vector2然后vector3

我尝试以下方法:

vector4=numpy.concatenate((vector1,vector2,vector3),axis=0)

ValueError: all the input array dimensions except for the concatenation axis must match exactly

vector4=numpy.append(vector4,[vector1,vector2,vectors3],axis=0)

TypeError: append() missing 1 required positional argument: 'values'

【问题讨论】:

  • 要连接,数组必须具有相同的列数。您的数组具有相同的行数。一种选择是用np.nan 值填充缺失的列,这样您就可以得到具有相同形状的数组,并且可以使用concatenate
  • 更改为axis=1
  • @alec_djinn 不,np.concatenate 可以沿任何轴连接。
  • @juanpa.arrivillaga,是的,你是对的,我没有说清楚,我以为他想在 axis=0 上做这件事(因为他在代码中指定了它)同时有不同的形状。

标签: python arrays numpy append


【解决方案1】:

我相信你正在寻找numpy.hstack

>>> import numpy as np
>>> a = np.arange(4).reshape(2,2)
>>> b = np.arange(6).reshape(2,3)
>>> c = np.arange(8).reshape(2,4)
>>> a
array([[0, 1],
       [2, 3]])
>>> b
array([[0, 1, 2],
       [3, 4, 5]])
>>> c
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> np.hstack((a,b,c))
array([[0, 1, 0, 1, 2, 0, 1, 2, 3],
       [2, 3, 3, 4, 5, 4, 5, 6, 7]])

【讨论】:

  • 由于数组已经是二维的,hstack 只是concatenateaxis=1axis=-1(最后一个)。
  • 另请注意,hstack 文档建议:“此功能继续支持向后兼容,但您应该更喜欢np.concatenatenp.stack
【解决方案2】:

错误消息几乎可以准确地告诉您问题所在:

ValueError: all the input array dimensions except for the concatenation axis must match exactly

但是您正在做相反的事情,串联轴尺寸完全匹配,但其他尺寸不匹配。考虑:

In [3]: arr1 = np.random.randint(0,10,(20, 5))

In [4]: arr2 = np.random.randint(0,10,(20, 3))

In [5]: arr3 = np.random.randint(0,10,(20, 11))

注意尺寸。只要给它正确的轴。所以使用第二个而不是第一个:

In [8]: arr1.shape, arr2.shape, arr3.shape
Out[8]: ((20, 5), (20, 3), (20, 11))

In [9]: np.concatenate((arr1, arr2, arr3), axis=1)
Out[9]:
array([[3, 1, 4, 7, 3, 6, 1, 1, 6, 7, 4, 6, 8, 6, 2, 8, 2, 5, 0],
       [4, 2, 2, 1, 7, 8, 0, 7, 2, 2, 3, 9, 8, 0, 7, 3, 5, 9, 6],
       [2, 8, 9, 8, 5, 3, 5, 8, 5, 2, 4, 1, 2, 0, 3, 2, 9, 1, 0],
       [6, 7, 3, 5, 6, 8, 3, 8, 4, 8, 1, 5, 4, 4, 6, 4, 0, 3, 4],
       [3, 5, 8, 8, 7, 7, 4, 8, 7, 3, 8, 7, 0, 2, 8, 9, 1, 9, 0],
       [5, 4, 8, 3, 7, 8, 3, 2, 7, 8, 2, 4, 8, 0, 6, 9, 2, 0, 3],
       [0, 0, 1, 8, 6, 4, 4, 4, 2, 8, 4, 1, 4, 1, 3, 1, 5, 5, 1],
       [1, 6, 3, 3, 9, 2, 3, 4, 9, 2, 6, 1, 4, 1, 5, 6, 0, 1, 9],
       [4, 5, 4, 7, 1, 4, 0, 8, 8, 1, 6, 0, 4, 6, 3, 1, 2, 5, 2],
       [6, 4, 3, 2, 9, 4, 1, 7, 7, 0, 0, 5, 9, 3, 7, 4, 5, 6, 1],
       [7, 7, 0, 4, 1, 9, 9, 1, 0, 1, 8, 3, 6, 0, 5, 1, 4, 0, 7],
       [7, 9, 0, 4, 0, 5, 5, 9, 8, 9, 9, 7, 8, 8, 2, 6, 2, 3, 1],
       [4, 1, 6, 5, 4, 5, 6, 7, 9, 2, 5, 8, 6, 6, 6, 8, 2, 3, 1],
       [7, 7, 8, 5, 0, 8, 5, 6, 4, 4, 3, 5, 9, 8, 7, 9, 8, 8, 1],
       [3, 9, 3, 6, 3, 2, 2, 4, 0, 1, 0, 4, 3, 0, 1, 3, 4, 1, 3],
       [5, 1, 9, 7, 1, 8, 3, 9, 4, 7, 6, 7, 4, 7, 0, 1, 2, 8, 7],
       [6, 3, 8, 0, 6, 2, 1, 8, 1, 0, 0, 3, 7, 2, 1, 5, 7, 0, 7],
       [5, 4, 7, 5, 5, 8, 3, 2, 6, 1, 0, 4, 6, 9, 7, 3, 9, 2, 5],
       [1, 4, 8, 5, 7, 2, 0, 2, 6, 2, 6, 5, 5, 4, 6, 1, 8, 8, 1],
       [4, 4, 5, 6, 2, 6, 0, 5, 1, 8, 4, 5, 8, 9, 2, 1, 0, 4, 2]])

In [10]: np.concatenate((arr1, arr2, arr3), axis=1).shape
Out[10]: (20, 19)

【讨论】:

    猜你喜欢
    • 2015-01-29
    • 2021-03-20
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多