Python 垂直连接两个 NumPy 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21887754/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:49:00  来源:igfitidea点击:

Concatenate two NumPy arrays vertically

pythonarraysnumpyconcatenation

提问by toom

I tried the following:

我尝试了以下方法:

>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> np.concatenate((a,b), axis=0)
array([1, 2, 3, 4, 5, 6])
>>> np.concatenate((a,b), axis=1)
array([1, 2, 3, 4, 5, 6])

However, I'd expect at least that one result looks like this

但是,我希望至少有一个结果看起来像这样

array([[1, 2, 3],
       [4, 5, 6]])

Why is it not concatenated vertically?

为什么不垂直连接?

采纳答案by gg349

Because both aand bhave only one axis, as their shape is (3), and the axis parameter specifically refers to the axis of the elements to concatenate.

因为ab都只有一个轴,因为它们的形状是(3),轴参数特指要连接的元素的轴。

this example should clarify what concatenateis doing with axis. Take two vectors with two axis, with shape (2,3):

这个例子应该阐明concatenate轴的作用。取两个带有两个轴的向量,形状为(2,3)

a = np.array([[1,5,9], [2,6,10]])
b = np.array([[3,7,11], [4,8,12]])

concatenates along the 1st axis (rows of the 1st, then rows of the 2nd):

沿第 1 个轴串联(第 1 行,然后是第 2 行):

np.concatenate((a,b), axis=0)
array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

concatenates along the 2nd axis (columns of the 1st, then columns of the 2nd):

沿第二个轴连接(第一个的列,然后是第二个的列):

np.concatenate((a, b), axis=1)
array([[ 1,  5,  9,  3,  7, 11],
       [ 2,  6, 10,  4,  8, 12]])

to obtain the output you presented, you can use vstack

要获得您提供的输出,您可以使用 vstack

a = np.array([1,2,3])
b = np.array([4,5,6])
np.vstack((a, b))
array([[1, 2, 3],
       [4, 5, 6]])

You can still do it with concatenate, but you need to reshape them first:

你仍然可以用 来做concatenate,但你需要先重塑它们:

np.concatenate((a.reshape(1,3), b.reshape(1,3)))
array([[1, 2, 3],
       [4, 5, 6]])

Finally, as proposed in the comments, one way to reshape them is to use newaxis:

最后,正如评论中所建议的,重塑它们的一种方法是使用newaxis

np.concatenate((a[np.newaxis,:], b[np.newaxis,:]))

回答by bougui

A not well known feature of numpy is to use r_. This is a simple way to build up arrays quickly:

numpy 的一个鲜为人知的特性是使用r_. 这是一种快速构建数组的简单方法:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.r_[a[None,:],b[None,:]]
print(c)
#[[1 2 3]
# [4 5 6]]

The purpose of a[None,:]is to add an axis to array a.

的目的a[None,:]是向 array 添加一个轴a

回答by hpaulj

a = np.array([1,2,3])
b = np.array([4,5,6])
np.array((a,b))

works just as well as

效果一样好

np.array([[1,2,3], [4,5,6]])

Regardless of whether it is a list of lists or a list of 1d arrays, np.arraytries to create a 2d array.

无论是列表列表还是一维数组列表,都np.array尝试创建一个二维数组。

But it's also a good idea to understand how np.concatenateand its family of stackfunctions work. In this context concatenateneeds a list of 2d arrays (or any anything that np.arraywill turn into a 2d array) as inputs.

但了解np.concatenatestack功能及其系列的工作方式也是一个好主意。在这种情况下,concatenate需要一个 2d 数组列表(或任何np.array将变成 2d 数组的任何东西)作为输入。

np.vstackfirst loops though the inputs making sure they are at least 2d, then does concatenate. Functionally it's the same as expanding the dimensions of the arrays yourself.

np.vstack首先循环输入以确保它们至少是 2d,然后进行连接。从功能上讲,它与自己扩展数组的维度相同。

np.stackis a new function that joins the arrays on a new dimension. Default behaves just like np.array.

np.stack是一个在新维度上连接数组的新函数。默认行为就像np.array.

Look at the code for these functions. If written in Python you can learn quite a bit. For vstack:

查看这些函数的代码。如果用 Python 编写,您可以学到很多东西。对于vstack

return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)

回答by Kshitij Saraogi

If the actual problem at hand is to concatenate two 1-D arrays vertically, and we are not fixated on using concatenateto perform this operation, I would suggest the use of np.column_stack:

如果手头的实际问题是垂直连接两个一维数组,并且我们不专注于使用concatenate来执行此操作,我建议使用np.column_stack

In []: a = np.array([1,2,3])
In []: b = np.array([4,5,6])
In []: np.column_stack((a, b))
array([[1, 4],
       [2, 5],
       [3, 6]])