我什么时候应该使用 hstack/vstack vs append vs concatenate vs column_stack? [英] When should I use hstack/vstack vs append vs concatenate vs column_stack?

查看:52
本文介绍了我什么时候应该使用 hstack/vstack vs append vs concatenate vs column_stack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

除了 np.concatenate 之外,所有函数都是用 Python 编写的.使用 IPython shell,您只需使用 ??.

All the functions are written in Python except np.concatenate. With an IPython shell you just use ??.

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

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.

和更新的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)

也就是说,它扩展所有输入的维度(有点像 np.expand_dims),然后连接.加上axis=0,效果和np.array一样.

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.

hstack 文档现在添加:

函数concatenatestackblock 提供更通用的堆叠和连接操作.

The functions concatenate, stack and block provide more general stacking and concatenation operations.

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

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

这篇关于我什么时候应该使用 hstack/vstack vs append vs concatenate vs column_stack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆