5 python numpy.expand_dims的用法

1 查看help

其实感觉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)

help(np.expand_dims)
Help on function expand_dims in module numpy.lib.shape_base:

expand_dims(a, axis)
    Expand the shape of an array.

    Insert a new axis, corresponding to a given position in the array shape.

    Parameters
    ----------
    a : array_like
        Input array.
    axis : int
        Position (amongst axes) where new axis is to be inserted.

    Returns
    -------
    res : ndarray
        Output array. The number of dimensions is one greater than that of
        the input array.

    See Also
    --------
    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[:,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

2 测试一维的数据

x = np.array([1,2,3])
print x
print x.shape
[1 2 3]
(3,)
y = np.expand_dims(x,axis=0)
print y
print "y.shape: ",y.shape
print "y[0][1]: ",y[0][1]
[[1 2 3]]
y.shape:  (1, 3)
y[0][1]:  2
y = np.expand_dims(x,axis=1)
print y
print "y.shape: ",y.shape
print "y[1][0]: ",y[1][0]
[[1]
 [2]
 [3]]
y.shape:  (3, 1)
y[1][0]:  2
y = np.expand_dims(x,axis=3)
print y
print "y.shape: ",y.shape
print "y[2][0]: ",y[2][0]
[[1]
 [2]
 [3]]
y.shape:  (3, 1)
y[2][0]:  3

3 测试二维的数据

x = np.array([[1,2,3],[4,5,6]])
print x
print x.shape
[[1 2 3]
 [4 5 6]]
(2, 3)
y = np.expand_dims(x,axis=0)
print y
print "y.shape: ",y.shape
print "y[0][1]: ",y[0][1]
[[[1 2 3]
  [4 5 6]]]
y.shape:  (1, 2, 3)
y[0][1]:  [4 5 6]
y = np.expand_dims(x,axis=1)
print y
print "y.shape: ",y.shape
print "y[1][0]: ",y[1][0]
[[[1 2 3]]

 [[4 5 6]]]
y.shape:  (2, 1, 3)
y[1][0]:  [4 5 6]
y = np.expand_dims(x,axis=3)
print y
print "y.shape: ",y.shape
print "y[2][0]: ",y[2][0]
[[[1]
  [2]
  [3]]

 [[4]
  [5]
  [6]]]
y.shape:  (2, 3, 1)
y[2][0]: 


---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-16-392d9cded3f4> in <module>()
      2 print y
      3 print "y.shape: ",y.shape
----> 4 print "y[2][0]: ",y[2][0]


IndexError: index 2 is out of bounds for axis 0 with size 2
  • 48
    点赞
  • 111
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值