Numpy stack 数组的堆叠

举报
千江有水千江月 发表于 2020/12/26 20:52:33 2020/12/26
【摘要】 所属的课程名称及链接[AI基础课程--常用框架工具]环境信息* ModelArts  * Notebook - Multi-Engine 2.0 (python3)    * JupyterLab - Notebook - Conda-python3      * numpy 1.19.1Numpy stack 数组的堆叠a = np.arange(9).reshape(3,3)b = np...

所属的课程名称及链接

[AI基础课程--常用框架工具]


环境信息

  • * ModelArts
    •   * Notebook - Multi-Engine 2.0 (python3)
      •     * JupyterLab - Notebook - Conda-python3
        •       * numpy 1.19.1


Numpy stack 数组的堆叠

a = np.arange(9).reshape(3,3)
b = np.arange(9,18).reshape(3,3)

# Join a sequence of arrays along a new axis.
# 沿新轴连接一系列数组。
# a和b都是 二维的,经过stack后输出一个 三维数组

c_axis_n1 = np.stack((a,b),axis=-1)  # ``axis=-1`` it will be the last dimension
c_axis_0 = np.stack((a,b),axis=0)  # ``axis=0`` it will be the first dimension
c_axis_1 = np.stack((a,b),axis=1)
c_axis_2 = np.stack((a,b),axis=2)

print("a",a.shape,"\n",a,"\n")
print("b",b.shape,"\n",b,"\n")
print("c_axis_n1",c_axis_n1.shape,"\n",c_axis_n1,"\n")
print("c_axis_0",c_axis_0.shape,"\n",c_axis_0,"\n")
print("c_axis_1",c_axis_1.shape,"\n",c_axis_1,"\n")
print("c_axis_2",c_axis_2.shape,"\n",c_axis_2,"\n")

a (3, 3) 
 [[0 1 2]
 [3 4 5]
 [6 7 8]] 

b (3, 3) 
 [[ 9 10 11]
 [12 13 14]
 [15 16 17]] 

c_axis_n1 (3, 3, 2) 
 [[[ 0  9]
  [ 1 10]
  [ 2 11]]

 [[ 3 12]
  [ 4 13]
  [ 5 14]]

 [[ 6 15]
  [ 7 16]
  [ 8 17]]] 

c_axis_0 (2, 3, 3) 
 [[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]] 

c_axis_1 (3, 2, 3) 
 [[[ 0  1  2]
  [ 9 10 11]]

 [[ 3  4  5]
  [12 13 14]]

 [[ 6  7  8]
  [15 16 17]]] 

c_axis_2 (3, 3, 2) 
 [[[ 0  9]
  [ 1 10]
  [ 2 11]]

 [[ 3 12]
  [ 4 13]
  [ 5 14]]

 [[ 6 15]
  [ 7 16]
  [ 8 17]]] 


help

help(numpy.stack)

Help on function stack in module numpy:

stack(arrays, axis=0, out=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.
    
    .. versionadded:: 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.
    
    See Also
    --------
    concatenate : Join a sequence of arrays along an existing axis.
    block : Assemble an nd-array from nested lists of blocks.
    split : Split array into a list of multiple sub-arrays of equal size.
    
    Examples
    --------
    >>> arrays = [np.random.randn(3, 4) for _ in range(10)]
    >>> 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)
    
    >>> a = np.array([1, 2, 3])
    >>> b = np.array([2, 3, 4])
    >>> np.stack((a, b))
    array([[1, 2, 3],
           [2, 3, 4]])
    
    >>> np.stack((a, b), axis=-1)
    array([[1, 2],
           [2, 3],
           [3, 4]])


备注

1. 感谢老师的教学与课件  
2. 欢迎各位同学一起来交流学习心得^_^  
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。  


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。