numpy vstack 与 column_stack [英] numpy vstack vs. column_stack

查看:44
本文介绍了numpy vstack 与 column_stack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy vstackcolumn_stack 到底有什么区别.通读文档,看起来 column_stack 是一维数组的 vstack 实现.它是更有效的实现吗?否则,我找不到只有 vstack 的理由.

解决方案

我认为以下代码很好地说明了差异:

<预><代码>>>>np.vstack(([1,2,3],[4,5,6]))数组([[1, 2, 3],[4, 5, 6]])>>>np.column_stack(([1,2,3],[4,5,6]))数组([[1, 4],[2, 5],[3, 6]])>>>np.hstack(([1,2,3],[4,5,6]))数组([1, 2, 3, 4, 5, 6])

我也包含了 hstack 用于比较.注意 column_stack 如何沿第二维堆叠,而 vstack 如何沿第一维堆叠.与 column_stack 等效的是以下 hstack 命令:

<预><代码>>>>np.hstack(([[1],[2],[3]],[[4],[5],[6]]))数组([[1, 4],[2, 5],[3, 6]])

我希望我们能同意 column_stack 更方便.

What exactly is the difference between numpy vstack and column_stack. Reading through the documentation, it looks as if column_stack is an implementation of vstack for 1D arrays. Is it a more efficient implementation? Otherwise, I cannot find a reason for just having vstack.

解决方案

I think the following code illustrates the difference nicely:

>>> np.vstack(([1,2,3],[4,5,6]))
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.column_stack(([1,2,3],[4,5,6]))
array([[1, 4],
       [2, 5],
       [3, 6]])
>>> np.hstack(([1,2,3],[4,5,6]))
array([1, 2, 3, 4, 5, 6])

I've included hstack for comparison as well. Notice how column_stack stacks along the second dimension whereas vstack stacks along the first dimension. The equivalent to column_stack is the following hstack command:

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

I hope we can agree that column_stack is more convenient.

这篇关于numpy vstack 与 column_stack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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