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

查看:108
本文介绍了我什么时候应该使用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,您只需使用??.

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 ??.

如果没有,则为他们的代码摘要:

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天全站免登陆