numpy.stack vs concatenate vs hstack vs vstack

官方文档的解释如下:

stack:

Join a sequence of arrays along a new axis

concatenate:

Join a sequence of arrays along a existing axis

因此stack 是在新轴axis=n上加入矩阵,已有的axis>=n往后挪动,譬如以前的axis=n挪动到axis=n+1上

而concatenate是在已有的轴axis=n上加入矩阵。

 

 

stack 例子:

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b))
array([[1, 2, 3],
       [2, 3, 4]])

concatenate例子:

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.concatenate((a, b))
array([1, 2, 3, 2, 3, 4])



hstack:

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

vstack:

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

因此当hstack 和 vstack 在维度等于1时,其作用相当于stack, 创建新轴。

而当维度大于等于2时,其作用相当于cancatenate, 在已有轴上进行操作。

posted on 2017-10-28 20:13  Tron1  阅读(789)  评论(0编辑  收藏  举报

导航