numpy之stacking

Stacking

Arrays can be stacked horizontally, depth wise, or vertically. We can use, for that purpose, thevstack(),dstack(), hstack(), column_stack(),row_stack(), andconcatenate() functions.

Time for action--stacking arrays

First, set up some arrays:

In: a = arange(9).reshape(3,3)
In: a
Out:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In: b = 2 * a
In: b
Out:
 array([[0, 2, 4],
        [6, 8 ,10],
        [12, 14, 16]])

1. Horizontal stacking: Starting with horizontal stacking, form a tuple of the ndarray objects and give it to the hstack() function as follows:

In: hstack((a,b))
Out:
array([[0, 1, 2, 0, 2, 4],
       [3, 4, 5, 6, 8, 10],
       [6, 7, 8, 12, 14, 16]])
Achieve the same with the concatenate() function as follows (the axis argument here is equivalent to axes in Cartesian(笛卡尔) coordinate system and correspond and corresponds to the array dimensions):

In: concatenate((a,b),axis=1)
Out:
array([[0, 1, 2, 0, 2, 4],
       [3, 4, 5, 6, 8, 10],
       [6, 7, 8, 12, 14, 16]])
This image shows horizontal stacking with the concatenate() function:



2. Vertical stacking: WIth vertical stacking, again, a tuple is formed. This time, it is given to thevstack() function as follows:

In: vstack((a,b))
Out:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [0, 2, 4],
       [6, 8, 10],
       [12, 14, 16]])
The concatenate() function produces the same result with the axis set to 0. This is the default value for the axis argument:

In: concatenate((a,b), axis=0)
Out:
array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8],
          [0, 2, 4],
          [6, 8, 10],
          [12, 14, 16]])

The following diagram shows vertical stacking with concatenate() function:



3. Depth stacking: Additionally, depth-wise stacking using dstack() and a tuple stacks a list of arrays along the third axis(deoth). For instance, stack two-dimension arrays of image data on top of each other:

In: dstack((a,b))
Out:
array([[[0, 0],
        [1, 2],
        [2, 4]],
       [[3, 6],
        [4, 8],
        [5, 10]],
       [[6, 12],
        [7, 14],
        [8, 16]]])

4. Column stacking: Stacking the one-dimensional arrays with the column_stack() function column-wise as follows:

In: oned = arange(2)
In: oned
Out: array([0,1])
In: twice_oned = 2 * oned
In: twice_oned
Out: array([0,2])
In: column_stack((oned, twice_oned))
Out:
array([[0,0],
       [1,2]])
Two-dimensional arrays are stacked the way hstack() stacks them:

In: column_stack((a,b))
Out:
array([[0, 1, 2, 0, 2, 4],
       [3, 4, 5, 6, 8, 10],
       [6, 7, 8, 12, 14, 16]])
In: column_stack((a,b)) == hstack((a,b))
Out:
array([[True, True, True, True, True, True],
       [True, True, True, True, True, True],
       [True, True, True, True, True, True]], dtype=bool)
Yes, you guessed it right! We compared two arrays the == operator.

The == operator is used in Python to compare for equality. When applied to Numpy arrays, the operator performs element-wise comparisons. 

5. Row stacking:Numpy, of course, also has a function that does row-wise stacking.

It is called row_stack(), and for one-dimensional arrays, it just stacks the arrays in rows into atwo-dimensional array:

In: row_stack((oned, twice_oned))
Out:
array([[0,1],
       [0,2]])
The row_stack() function results for two-dimensional arrays are equal  to, yes, exactly, the vstack() function results:
In: row_stack((a,b))
Out:
array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8],
          [0, 2, 4],
          [6, 8, 10],
          [12, 14, 16]])
In: row_stack((a,b)) == vstack((a,b))
Out:
array([[True, True, True],
       [True, True, True],
       [True, True, True],
       [True, True, True],
       [True, True, True],
       [True, True, True]], dtype=bool)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值