简体   繁体   English

关于numpy的串联,hstack,vstack函数?

[英]About numpy's concatenate, hstack, vstack functions?

See some examples 看一些例子

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate((a,b), axis=0)) # [1,2,3,4,5,6]
print(np.hstack((a,b))) # [1,2,3,4,5,6]

print(np.vstack((a,b))) # [[1,2,3],[4,5,6]]
print(np.concatenate((a,b), axis=1)) # IndexError: axis 1 out of bounds [0, 1)

The result of hstack is the same as concatenate along axis=0, but the api document says hstack=concatenate along axis=1, please look at the https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.hstack.html#numpy.hstack hstack的结果与沿axis = 0的连接相同,但api文档说hstack =沿axis = 1的连接,请查看https://docs.scipy.org/doc/numpy-dev/reference/生成/numpy.hstack.html#numpy.hstack

And concatenating along the axis=1 raise an IndexError, the api document says hstack=concatenate along axis=0, please look at the https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.vstack.html#numpy.vstack 并沿axis = 1串联会引发IndexError,api文档说hstack = concatenate沿axis = 0,请查看https://docs.scipy.org/doc/numpy-dev/reference/generation/numpy.vstack的.html#numpy.vstack

Can anybody explain it?By the way, can anybody explain how to broadcast when the ndarray's dimension is less than 2 and concatenating along axis=1? 有人可以解释吗?有人可以解释当ndarray的维数小于2并沿axis = 1并置时如何广播吗?

Look at the actual code for hstack : 查看hstack的实际代码:

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)

I don't see anything in the docs about axis=1 . 我在文档中看不到关于axis=1任何内容。 The term it uses is 'stack them horizontally' . 它使用的术语是'stack them horizontally'

As I noted a year ago, Concatenation of 2 1D numpy arrays along 2nd axis , earlier versions don't raise an error if the axis is too high. 正如我一年前指出的那样,如果沿轴第二个2D一维numpy数组的并置 ,则早期版本不会产生错误(如果轴太高)。 But in 1.12 we get an error. 但是在1.12中我们得到一个错误。

There is a newish np.stack that can add a dimension where needed: 有一个新的np.stack可以在需要的地方添加尺寸:

In [46]: np.stack((np.arange(3), np.arange(4,7)),axis=1)
Out[46]: 
array([[0, 4],
       [1, 5],
       [2, 6]])

The base function is concatenate . 基本函数是concatenate The various stack functions adjust array dimensions in one way or other, and then do concatenate . 各种stack函数以一种或另一种方式调整数组尺寸,然后进行concatenate Look at their code to see the details. 查看他们的代码以查看详细信息。 (I've summarized the differences in earlier posts as well). (我也总结了早期文章中的差异)。

np.hstack(tup) and np.concatenate(tup, axis=1) are indeed equivalent but only if tup contains arrays that are at least 2-dimensional. np.hstack(tup)np.concatenate(tup, axis=1)确实是等效的,但是只有当tup包含的至少2维阵列。 This was in fact spelled out in the documentation for vstack , so it looks like it was just an oversight that it did not also in the documentation for hstack ; 这实际上是在vstack的文档中vstack ,因此看起来只是一个疏忽,在hstack的文档中也没有hstack ; it will for future versions though. 它将用于将来的版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM