【问题标题】:numpy array concatenate: "ValueError: all the input arrays must have same number of dimensions"numpy 数组连接:“ValueError:所有输入数组必须具有相同的维数”
【发布时间】:2017-06-18 19:12:33
【问题描述】:

如何连接这些numpy 数组?

第一个np.array,形状为(5,4)

[[  6487    400 489580      0]
 [  6488    401 492994      0]
 [  6491    408 489247      0]
 [  6491    408 489247      0]
 [  6492    402 499013      0]]

第二个np.array,形状为(5,)

[  16.   15.   12.  12.  17. ]

最终结果应该是

[[  6487    400    489580    0   16]
 [  6488    401    492994    0   15]
 [  6491    408    489247    0   12]
 [  6491    408    489247    0   12]
 [  6492    402    499013    0   17]]

我试过np.concatenate([array1, array2]) 但我得到这个错误

ValueError: all the input arrays must have same number of dimensions

我做错了什么?

【问题讨论】:

  • 第二个数组的形状到底是什么(1,)?对象数组在这里发生了什么奇怪的事情吗?
  • 这就是我运行array2.shape时得到的
  • 那么你的数组在某种程度上严重混乱了,你需要弄清楚发生了什么。
  • 在那之前我运行了这个array2 = np.array(np.round(data[:,0]/20))

标签: python numpy


【解决方案1】:

要使用np.concatenate,我们需要将第二个数组扩展为2D,然后将axis=1连接起来-

np.concatenate((a,b[:,None]),axis=1)

或者,我们可以使用 np.column_stack 来处理它 -

np.column_stack((a,b))

示例运行 -

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

如果b 是这样一个1D 数组dtype=object 形状为(1,),很可能所有数据都包含在其中的唯一元素中,我们需要展平 在连接之前将其输出。为此,我们也可以在上面使用np.concatenate。这是一个示例运行以说明这一点 -

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

【讨论】:

  • b[:, None] 表示法很棒,但也值得一提 b.reshape()
  • @PaulPanzer 这值得一个新帖子!考虑发布它。
  • 第二个的形状是因为这个array2 = np.array(np.round(data[:,0]/20))我用array2 = np.array(np.round(data[:,0]/20)).astype(int)固定的
【解决方案2】:

还有np.c_

>>> a = np.arange(20).reshape(5, 4)
>>> b = np.arange(-1, -6, -1)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])                                                                                                                                   
>>> b                                                                                                                                                       
array([-1, -2, -3, -4, -5])                                                                                                                                 
>>> np.c_[a, b]
array([[ 0,  1,  2,  3, -1],          
       [ 4,  5,  6,  7, -2],                       
       [ 8,  9, 10, 11, -3],                      
       [12, 13, 14, 15, -4],                                
       [16, 17, 18, 19, -5]])

【讨论】:

  • 你敢解码这个:np.r_['1,2,0', a, -1:-6:-1] :)
  • 我想知道np.c_是否总是可以替代np.column_stack?
  • @hpaulj 在我看来很像,但我对np.column_stack 不太熟悉。它基本上是一个 2d concatenate 特殊情况下 1d 输入,对吧?
【解决方案3】:

你可以这样做。

import numpy as np

x = np.random.randint(100, size=(5, 4))
y = [16, 15, 12, 12, 17]

print(x)

val = np.concatenate((x,np.reshape(y,(x.shape[0],1))),axis=1)
print(val)

这个输出:

[[32 37 35 53]
 [64 23 95 76]
 [17 76 11 30]
 [35 42  6 80]
 [61 88  7 56]]

[[32 37 35 53 16]
 [64 23 95 76 15]
 [17 76 11 30 12]
 [35 42  6 80 12]
 [61 88  7 56 17]]

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 2018-06-12
    • 2016-12-15
    • 2018-06-23
    • 1970-01-01
    • 2019-09-22
    • 2018-05-18
    • 1970-01-01
    • 2013-11-03
    相关资源
    最近更新 更多