首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python中numpy.moveaxis以及numpy.expand_dims的用法介绍

python中numpy.moveaxis以及numpy.expand_dims的用法介绍

作者头像
用户7886150
修改2021-01-04 10:12:50
修改2021-01-04 10:12:50
8850
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: Python中的numpy.insert

1. numpy.

moveaxis

(

a, 

source, 

destination

)

[source]

Move axes of an array to new positions.

Other axes remain in their original order.

 New in version 1.11.0.

Parameters:a : np.ndarray

     The array whose axes should be reordered.

    source : int or sequence of int

     Original positions of the axes to move. These must be unique.

    destination : int or sequence of int

     Destination positions for each of the original axes. These must also be unique.

    Returns:result : np.ndarray

     Array with moved axes. This array is a view of the input array.

 See also

   transpose

   Permute the dimensions of an array.

   swapaxes

   Interchange two axes of an array.

Examples

  >>> x = np.zeros((3, 4, 5))

>>> np.moveaxis(x, 0, -1).shape

(4, 5, 3)

>>> np.moveaxis(x, -1, 0).shape

(5, 3, 4)

These all achieve the same result:

>>> np.transpose(x).shape

(5, 4, 3)

>>> np.swapaxes(x, 0, -1).shape

(5, 4, 3)

>>> np.moveaxis(x, [0, 1], [-1, -2]).shape

(5, 4, 3)

>>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape

(5, 4, 3)

2.expand_dims(a, axis)

就是在axis的那一个轴上把数据加上去,这个数据在axis这个轴的0位置。 

例如原本为一维的2个数据,axis=0,则shape变为(1,2),axis=1则shape变为(2,1) 

再例如 原本为 (2,3),axis=0,则shape变为(1,2,3),axis=1则shape变为(2,1,3)

numpy.

expand_dims

(

a, 

axis

)

[source]

Expand the shape of an array.

Insert a new axis that will appear at the axis position in the expanded array shape.

 Note

 Previous to NumPy 1.13.0, neither axis < -a.ndim - 1 nor axis > a.ndim raised errors or put the new axis where documented. Those axis values are now deprecated and will raise an AxisError in the future.

Parameters:a : array_like

     Input array.

    axis : int

     Position in the expanded axes where the new axis is placed.

    Returns:res : ndarray

     Output array. The number of dimensions is one greater than that of the input array.

 See also

   squeeze

   The inverse operation, removing singleton dimensions

   reshape

   Insert, remove, and combine dimensions, and resize existing ones

 doc.indexing, atleast_1d, atleast_2d, atleast_3d

Examples

  >>> x = np.array([1,2])

>>> x.shape

(2,)

The following is equivalent to x[np.newaxis,:] or x[np.newaxis]:

  >>> y = np.expand_dims(x, axis=0)

>>> y

array([[1, 2]])

>>> y.shape

(1, 2)

  >>> y = np.expand_dims(x, axis=1)  # Equivalent to x[:,np.newaxis]

>>> y

array([[1],

       [2]])

>>> y.shape

(2, 1)

Note that some examples may use None instead of np.newaxis. These are the same objects:

  >>> np.newaxis is None

True

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档