【Python】 numpy.expand_dims的用法

博客主要介绍了np.expand_dims函数,该函数可在指定轴上添加数据。通过对一维和二维数据的测试,展示了不同axis值下数据shape的变化,如一维数据axis=0和axis=1时shape的不同变化,还给出了具体代码示例及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

expand_dims(a, axis)
    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

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
--------------------- 
作者:HxShine 
来源:CSDN 
原文:https://blog.csdn.net/qq_16949707/article/details/53418912 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值