np.stack()方法

  • np.stack()方法大家肯定经常遇到,但是对于stack()的具体作用可能不一定非常明确,这里结合实例以及官方文档作用解释。
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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

主要的参数有两个,一个是arrays,也就是用来作为堆叠的数组,要求形状维度必须相等,第二个参数为axis也就是指定依照哪个维度进行堆叠,下面使用官方给的例子来进行解释。

>>> arrays = [np.random.randn(3, 4) for _ in range(10)]
# 利用列表生成式生成一个10*3*4的列表

>>> np.stack(arrays, axis=0).shape
(10, 3, 4)
# 原始的维度为3*4,共有10个,采取了axis=0,也就是为原数组新增一个维度,这个维度在第0位,由于一共有10个3*4数组,那么堆叠后的第一位的值便是10,所以堆叠后的维度为10*3*4


>>> np.stack(arrays, axis=1).shape
(3, 10, 4)
# 如上面解释,在axis-1的位置插入维度,由于一共10个数组,所以堆叠后的形状为3*10*4

>>> np.stack(arrays, axis=2).shape
(3, 4, 10)
# 还是想上面的解释,在最后一位增加维度,堆叠后的形状是3*4*10

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b)) #默认axis=0
array([[1, 2, 3],
       [2, 3, 4]])
#第一维度增加,也就是1*3变为了2*1*3

>>> np.stack((a, b), axis=-1)
array([[1, 2],
       [2, 3],
       [3, 4]])
# 根据前面介绍的,axis=-1也就是在最后一维后增加一维,原始为1*3,堆叠后的维度就是1*3*2,由于这里的a和b都只是一维数组,所以axis=1和axis=-1是相同的效果

a = np.array([[1, 2, 3],[2,3,4]])
b = np.array([[2, 3, 4],[4,5,6]])

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

       [[2, 3, 4],
        [4, 5, 6]]])

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

       [[2, 3, 4],
        [4, 5, 6]]])

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

       [[2, 4],
        [3, 5],
        [4, 6]]])

最后一个比较难理解一点,但是想到是根据多个数组进行堆叠,并且每个数组是2*3,输出的形状是2*3*2,便可以想到如上的结果,这个不是特别好描述,需要自己好好理解一下,参考样例应该会比较好了解。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

又重新加了个例子,可能看这个例子会更好地理解stack的作用,实例如下

a = np.arange(1, 10).reshape((3, 3))
b = np.arange(11, 20).reshape((3, 3))
c = np.arange(101, 110).reshape((3, 3))

np.stack((a,b,c),axis=0)
# axis=0可以认为只是将原数组上下堆叠,增加了0维的个数
array([[[  1,   2,   3],
        [  4,   5,   6],
        [  7,   8,   9]],

       [[ 11,  12,  13],
        [ 14,  15,  16],
        [ 17,  18,  19]],

       [[101, 102, 103],
        [104, 105, 106],
        [107, 108, 109]]])


np.stack((a,b,c),axis=1)
#axis=1,可以看出第一个3*3的数组是由是a,b,c中每个数组的第一行堆叠而成
array([[[  1,   2,   3],
        [ 11,  12,  13],
        [101, 102, 103]],

       [[  4,   5,   6],
        [ 14,  15,  16],
        [104, 105, 106]],

       [[  7,   8,   9],
        [ 17,  18,  19],
        [107, 108, 109]]])

np.stack((a,b,c),axis=2)
#axis=2,可以看到第一个3*3的数组是由a,b,c中的第一列向量组合而成
array([[[  1,  11, 101],
        [  2,  12, 102],
        [  3,  13, 103]],

       [[  4,  14, 104],
        [  5,  15, 105],
        [  6,  16, 106]],

       [[  7,  17, 107],
        [  8,  18, 108],
        [  9,  19, 109]]])
posted @ 2020-02-04 11:40  CeasonCing  阅读(681)  评论(0)    收藏  举报