numpy.stack和numpy.concatenate的区别

  在使用numpy进行矩阵运算的时候踩到的坑,原因是不能正确区分numpy.concatenate和numpy.stack在功能上的差异。

  先说numpy.concatenate,直接看文档:

numpy.concatenate((a1a2...)axis=0out=None)

Join a sequence of arrays along an existing axis.

Parameters
a1, a2, … : sequence of array_like

The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).

axis : int, optional

The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.

out : ndarray, optional

If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.

Returns
res : ndarray

The concatenated array.

  重点在这一句:在一个已经存在的维度上连接数组列。可见numpy.concatenate可以同时连接好几个数组,并且不会生成新的维度: along an existing axis。示例如下:

>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])

  

  再说numpy.stack:

numpy.stack(arraysaxis=0out=None)

Join a sequence of arrays along a new axis.

The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

New in version 1.10.0.

Parameters
arrays : sequence of array_like

Each array must have the same shape.

axis : int, optional

The axis in the result array along which the input arrays are stacked.

out : ndarray, optional

If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified.

Returns
stacked : ndarray

The stacked array has one more dimension than the input arrays.

  和concatenate不同的是,stack Joins a sequence of arrays along a new axis.也就是说stack会生成一个新的维度。而且stack适用的条件很强,数组序列必须全部有相同的shape。用例子来说明,使用最多的大概是在第0维stack:

>>> arrays = [np.random.randn(3, 4) for _ in range(10)]    # arrays是一个长度为10的List,每一个元素都是(3,4)的ndarray
>>> np.stack(arrays, axis=0).shape
(10, 3, 4)
>>> np.stack(arrays, axis=1).shape
(3, 10, 4)
>>> np.stack(arrays, axis=2).shape
(3, 4, 10)

  一个清晰的区别是返回的数组比输入数组多了一维。

posted @ 2019-11-11 10:53  思念殇千寻  阅读(2057)  评论(0编辑  收藏  举报