numpy.expand_dims

作者: 西方失败9527 | 来源:发表于2018-10-12 14:15 被阅读0次

官方文档中[1],如下解释:

numpy.expand_dims(aaxis)

   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.indexingatleast_1datleast_2datleast_3d

test codes:<自行脑补>

import numpyas np

x= np.array([[1,2,3],[4,5,6]])

print(x)

print(x.shape)

print("-----------------------------")

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

print(y)

print("y.shape: ", y.shape)

print("-----------------------------")

y= np.expand_dims(x, axis=1)

print(y)

print("y.shape: ", y.shape)

print("-----------------------------")

y= np.expand_dims(x, axis=2)

print(y)

print("y.shape: ", y.shape)

结果:

[[1 2 3]

[4 5 6]]

(2, 3)

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

[[[1 2 3]

  [4 5 6]]]

y.shape:  (1, 2, 3)

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

[[[1 2 3]]

[[4 5 6]]]

y.shape:  (2, 1, 3)

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

[[[1]

  [2]

  [3]]

[[4]

  [5]

  [6]]]

y.shape:  (2, 3, 1)

顺带着:<张量拼接>

y1= np.array([ [[1,0],[1,0]] , [[2,2],[3,3]] ])

print(y1.shape)

y2= np.array([ [[4,4],[5,5]] , [[0,1],[0,1]] ])

print(y2.shape)

y3= np.concatenate((y1,y2),axis=1)

print(y3.shape)

print(y3)

输出结果:

(2, 2, 2)

(2, 2, 2)

(2, 4, 2)

[[[1 0]

  [1 0]

  [4 4]

  [5 5]]

[[2 2]

  [3 3]

  [0 1]

  [0 1]]]

此处为按1轴的拼接:首先我们找到y1和y2的0轴,在对应0轴内每个元素按照1轴进行拼接。y1的0轴第一个元素[[1,0],[1,0]]与y2的0轴第一个元素[[0,0],[0,0]]按照2轴拼接成:

[[1, 0],

[1, 0],

[4, 4],

[5,5]]

[1] https://docs.scipy.org/doc/numpy/reference/generated/numpy.expand_dims.html

[2] https://blog.csdn.net/qq_16234613/article/details/79380957

相关文章

  • numpy.expand_dims

    官方文档中[1],如下解释: numpy.expand_dims(a,axis) Expand the shap...

网友评论

    本文标题:numpy.expand_dims

    本文链接:https://www.haomeiwen.com/subject/tfzqattx.html