Python 我什么时候应该使用 hstack/vstack vs append vs concatenate vs column_stack

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

when should i use hstack/vstack vs append vs concatenate vs column_stack

pythonnumpy

提问by Stefan Sullivan

Simple question: what is the advantage of each of these methods. It seems that given the right parameters (and ndarray shapes) they all work seemingly equivalently. Do some work in place? have better performance? which functions should I use when?

简单的问题:每种方法的优点是什么。似乎给定正确的参数(和 ndarray 形状),它们似乎都等效地工作。做一些工作吗?有更好的表现吗?我应该在什么时候使用哪些功能?

采纳答案by hpaulj

Do you have access to the code of these functions? All are written in Python except np.concatenate. With an IPython shell you just use ??.

您可以访问这些函数的代码吗?除了np.concatenate. 使用 IPython shell,您只需使用??.

If not, here's a summary of their code:

如果没有,这里是他们的代码摘要:

vstack
concatenate([atleast_2d(_m) for _m in tup], 0)
i.e. turn all inputs in to 2d (or more) and concatenate on first

hstack
concatenate([atleast_1d(_m) for _m in tup], axis=<0 or 1>)

colstack
transform arrays with (if needed)
    array(arr, copy=False, subok=True, ndmin=2).T

append
concatenate((asarray(arr), values), axis=axis)

In other words, they all work by tweaking the dimensions of the input arrays, and then concatenating on the right axis. They are just convenience functions.

换句话说,它们都是通过调整输入数组的维度,然后在右轴上连接来工作的。它们只是方便的功能。



And newer np.stack:

和更新np.stack

arrays = [asanyarray(arr) for arr in arrays]
shapes = set(arr.shape for arr in arrays)
result_ndim = arrays[0].ndim + 1
axis = normalize_axis_index(axis, result_ndim)
sl = (slice(None),) * axis + (_nx.newaxis,)

expanded_arrays = [arr[sl] for arr in arrays]
concatenate(expanded_arrays, axis=axis, out=out)

That is, it expands the dims of all inputs (a bit like np.expand_dims), and then concatenates. With axis=0, the effect is the same as np.array.

也就是说,它扩展所有输入的暗淡(有点像np.expand_dims),然后连接。与axis=0,效果相同np.array

hstackdocumentation now adds:

hstack文档现在添加:

The functions concatenate, stackand blockprovide more general stacking and concatenation operations.

函数concatenatestackblock提供更一般的堆叠和连接操作。

np.blockis also new. It, in effect, recursively concatenates along the nested lists.

np.block也是新的。实际上,它沿嵌套列表递归连接。

回答by maxymoo

In IPython you can look at the source code of a function by typing its name followed by ??. Taking a look at hstackwe can see that it's actually just a wrapper around concatenate(similarly with vstackand column_stack):

在 IPython 中,您可以通过键入函数名称后跟??. 看一下hstack我们可以看到它实际上只是一个包装器concatenate(与vstack和类似column_stack):

np.hstack??
def hstack(tup):
...
    arrs = [atleast_1d(_m) for _m in tup]
    # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
    if arrs[0].ndim == 1:
        return _nx.concatenate(arrs, 0)
    else:
        return _nx.concatenate(arrs, 1)

So I guess just use whichever one has the most logical sounding name to you.

所以我想只要使用对你来说最合乎逻辑的名字就可以了。

回答by Yuchao Jiang

numpy.vstack: stack arrays in sequence vertically(row wise).Equivalent to np.concatenate(tup, axis=0)example see: https://docs.scipy.org/doc/numpy/reference/generated/numpy.vstack.html

numpy.vstack:垂直(按行)按顺序堆叠数组。等效于np.concatenate(tup, axis=0)示例参见:https: //docs.scipy.org/doc/numpy/reference/generated/numpy.vstack.html

numpy.hstack: Stack arrays in sequence horizontally(column wise).Equivalent to np.concatenate(tup, axis=1), except for 1-D arrays where it concatenates along the first axis. example see: https://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html

numpy.hstack:水平(按列)按顺序堆叠数组。等效于np.concatenate(tup, axis=1),除了沿第一个轴连接的一维数组。示例见:https: //docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html

append is a function for python's built-in data structure list. Each time you add an element to the list. Obviously, To add multiple elements, you will use extend. Simply put, numpy's functions are much more powerful.

append 是 python 内置数据结构的函数list。每次向列表中添加一个元素。显然,要添加多个元素,您将使用extend. 简单来说,numpy 的功能要强大得多。

example:

例子:

suppose gray.shape = (n0,n1)

假设 gray.shape = (n0,n1)

np.vstack((gray,gray,gray))will have shape (n0*3, n1), you can also do it by np.concatenate((gray,gray,gray),axis=0)

np.vstack((gray,gray,gray))将具有形状(n0 * 3,n1),您也可以通过 np.concatenate((gray,gray,gray),axis=0)

np.hstack((gray,gray,gray))will have shape (n0, n1*3), you can also do it by np.concatenate((gray,gray,gray),axis=1)

np.hstack((gray,gray,gray))将具有形状(n0,n1 * 3),您也可以通过 np.concatenate((gray,gray,gray),axis=1)

np.dstack((gray,gray,gray))will have shape (n0, n1,3).

np.dstack((gray,gray,gray))将具有形状 (n0, n1,3)。