This question already has an answer here:
这个问题在这里已有答案:
- concatenate two one-dimensional to two columns array 1 answer
- 连接两个一维到两列的数组1个答案
I want to concatenate two arrays vertically in Python using the NumPy package:
我想使用NumPy包在Python中垂直连接两个数组:
a = array([1,2,3,4])
b = array([5,6,7,8])
I want something like this:
我想要这样的东西:
c = array([[1,2,3,4],[5,6,7,8]])
How we can do that using the concatenate
function? I checked these two functions but the results are the same:
我们如何使用连接函数来做到这一点?我检查了这两个函数但结果是一样的:
c = concatenate((a,b),axis=0)
# or
c = concatenate((a,b),axis=1)
We have this in both of these functions:
我们在以下两个功能中都有这个:
c = array([1,2,3,4,5,6,7,8])
4 个解决方案
#1
8
The problem is that both a
and b
are 1D arrays and so there's only one axis to join them on.
问题是a和b都是1D数组,因此只有一个轴可以连接它们。
Instead, you can use vstack
(v for vertical):
相反,您可以使用vstack(v表示垂直):
>>> np.vstack((a,b))
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Also, row_stack
is an alias of the vstack
function:
另外,row_stack是vstack函数的别名:
>>> np.row_stack((a,b))
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
It's also worth noting that multiple arrays of the same length can be stacked at once. For instance, np.vstack((a,b,x,y))
would have four rows.
值得注意的是,可以同时堆叠相同长度的多个阵列。例如,np.vstack((a,b,x,y))将有四行。
Under the hood, vstack
works by making sure that each array has at least two dimensions (using atleast_2D
) and then calling concatenate
to join these arrays on the first axis (axis=0
).
在引擎盖下,vstack通过确保每个数组至少具有两个维度(使用atleast_2D)然后调用concatenate以在第一个轴(轴= 0)上连接这些数组来工作。
#2
4
Maybe it's not a good solution, but it's simple way to makes your code works, just add reshape:
也许它不是一个好的解决方案,但它是使代码工作的简单方法,只需添加重塑:
a = array([1,2,3,4])
b = array([5,6,7,8])
c = concatenate((a,b),axis=0).reshape((2,4))
print c
out:
出:
[[1 2 3 4]
[5 6 7 8]]
In general if you have more than 2 arrays with the same length:
通常,如果您有两个以上具有相同长度的数组:
reshape((number_of_arrays, length_of_array))
#3
2
To use concatenate
, you need to make a
and b
2D arrays instead of 1D, as in
要使用连接,您需要制作a和b 2D数组而不是1D,如
c = concatenate((atleast_2d(a), atleast_2d(b)))
Alternatively, you can simply do
或者,你可以做到
c = array((a,b))
#4
2
Use np.vstack
:
使用np.vstack:
In [4]:
import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.vstack((a,b))
c
Out[4]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [5]:
d = np.array ([[1,2,3,4],[5,6,7,8]])
d
Out[5]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [6]:
np.equal(c,d)
Out[6]:
array([[ True, True, True, True],
[ True, True, True, True]], dtype=bool)
#1
8
The problem is that both a
and b
are 1D arrays and so there's only one axis to join them on.
问题是a和b都是1D数组,因此只有一个轴可以连接它们。
Instead, you can use vstack
(v for vertical):
相反,您可以使用vstack(v表示垂直):
>>> np.vstack((a,b))
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Also, row_stack
is an alias of the vstack
function:
另外,row_stack是vstack函数的别名:
>>> np.row_stack((a,b))
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
It's also worth noting that multiple arrays of the same length can be stacked at once. For instance, np.vstack((a,b,x,y))
would have four rows.
值得注意的是,可以同时堆叠相同长度的多个阵列。例如,np.vstack((a,b,x,y))将有四行。
Under the hood, vstack
works by making sure that each array has at least two dimensions (using atleast_2D
) and then calling concatenate
to join these arrays on the first axis (axis=0
).
在引擎盖下,vstack通过确保每个数组至少具有两个维度(使用atleast_2D)然后调用concatenate以在第一个轴(轴= 0)上连接这些数组来工作。
#2
4
Maybe it's not a good solution, but it's simple way to makes your code works, just add reshape:
也许它不是一个好的解决方案,但它是使代码工作的简单方法,只需添加重塑:
a = array([1,2,3,4])
b = array([5,6,7,8])
c = concatenate((a,b),axis=0).reshape((2,4))
print c
out:
出:
[[1 2 3 4]
[5 6 7 8]]
In general if you have more than 2 arrays with the same length:
通常,如果您有两个以上具有相同长度的数组:
reshape((number_of_arrays, length_of_array))
#3
2
To use concatenate
, you need to make a
and b
2D arrays instead of 1D, as in
要使用连接,您需要制作a和b 2D数组而不是1D,如
c = concatenate((atleast_2d(a), atleast_2d(b)))
Alternatively, you can simply do
或者,你可以做到
c = array((a,b))
#4
2
Use np.vstack
:
使用np.vstack:
In [4]:
import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.vstack((a,b))
c
Out[4]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [5]:
d = np.array ([[1,2,3,4],[5,6,7,8]])
d
Out[5]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [6]:
np.equal(c,d)
Out[6]:
array([[ True, True, True, True],
[ True, True, True, True]], dtype=bool)