Python numpy 数组连接:“ValueError:所有输入数组必须具有相同的维数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41989950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy array concatenate: "ValueError: all the input arrays must have same number of dimensions"
提问by RaduS
How to concatenate these numpy
arrays?
如何连接这些numpy
数组?
first np.array
with a shape (5,4)
首先np.array
有一个形状(5,4)
[[ 6487 400 489580 0]
[ 6488 401 492994 0]
[ 6491 408 489247 0]
[ 6491 408 489247 0]
[ 6492 402 499013 0]]
second np.array
with a shape (1,5)
第二个np.array
有形状(1,5)
[ 16. 15. 12. 12. 17. ]
final result should be
最终结果应该是
[[ 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]]
I tried np.concatenate([array1, array2])
but i get this error
我试过了,np.concatenate([array1, array2])
但我收到这个错误
ValueError: all the input arrays must have same number of dimensions
ValueError: all the input arrays must have same number of dimensions
What am I doing wrong?
我究竟做错了什么?
回答by Divakar
To use np.concatenate
, we need to extend the second array to 2D
and then concatenate along axis=1
-
要使用np.concatenate
,我们需要将第二个数组扩展到2D
,然后沿着axis=1
-
np.concatenate((a,b[:,None]),axis=1)
Alternatively, we can use np.column_stack
that takes care of it -
或者,我们可以使用np.column_stack
它来处理它 -
np.column_stack((a,b))
Sample run -
样品运行 -
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]])
If b
is such that its a 1D
array of dtype=object
with a shape of (1,)
, most probably all of the data is contained in the only element in it, we need to flattenit out before concatenating. For that purpose, we can use np.concatenate
on it too. Here's a sample run to make the point clear -
如果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]])
回答by Paul Panzer
There's also np.c_
还有 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]])
回答by Wasi Ahmad
You can do something like this.
你可以做这样的事情。
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)
This outputs:
这输出:
[[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]]