Python numpy vstack 与 column_stack

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

numpy vstack vs. column_stack

pythonnumpy

提问by ZenBalance

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

numpyvstackcolumn_stack. 通读文档,它看起来好像column_stack是一vstack维数组的实现。它是更有效的实现吗?否则,我找不到只拥有vstack.

采纳答案by mgilson

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 hstackfor comparison as well. Notice how column_stackstacks along the second dimension whereas vstackstacks along the first dimension. The equivalent to column_stackis the following hstackcommand:

我也包括在内hstack进行比较。请注意如何column_stack沿第二维vstack堆叠,而如何沿第一维堆叠。等效于column_stack以下hstack命令:

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

I hope we can agree that column_stackis more convenient.

我希望我们能同意这样column_stack更方便。

回答by SethMMorton

In the Notes section to column_stack, it points out this:

column_stack的注释部分,它指出了这一点:

This function is equivalent to np.vstack(tup).T.

该函数等效于np.vstack(tup).T

There are many functions in numpythat are convenient wrappers of other functions. For example, the Notes section of vstacksays:

有许多函数numpy可以方便地封装其他函数。例如,vstack的注释部分说:

Equivalent to np.concatenate(tup, axis=0)if tup contains arrays that are at least 2-dimensional.

相当于np.concatenate(tup, axis=0)如果 tup 包含至少是二维的数组。

It looks like column_stackis just a convenience function for vstack.

看起来column_stack只是vstack.