numpy 下的axis(轴)详细含义,np.expand_dims(x,axis=0),np.newaxis解释

以下举例: 
np.array([1, 2, 3]) 
当你看以上数组时,从1到2,到3。这就是所谓的axis=0轴 
np.array([ [1, 2], [3, 4], [4, 5] ]) 

再用相同的方法,看上面数组,首先是从[1, 2]到 [3, 4]到[4, 5]。这就是从0轴视角看的数据,当我们选择0轴所在的第一个元素[1, 2]时,我们看到的是从1到2。这就是从1轴看到的数据。假若有n维数据,axis=0表示最外面括号说包含的里面所有内容,axis=1表示第2个括号里面所包含所有内容,比如:[[[内容1],[内容2]]]。

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

[[1, 0], 
[1, 0], 
[0, 0], 
[0, 0]]
y1 = np.array([ [[1,0],[1,0]] , [[0,0],[0,0]] ])
y2 = np.array([ [[0,0],[0,0]] , [[0,1],[0,1]] ])
np.concatenate((y1,y2),axis=1)  #按轴=1拼,也就是一轴里是怎样的格式内容,拼接也需要与其一致,一轴在拼接结束时也要保持末端有2个括号。array([[[1, 0],        [1, 0],
        [0, 0],
        [0, 0]],#2个括号,与1轴一致,

       [[0, 0],
        [0, 0],
        [0, 1],
        [0, 1]]])

理解numpy中的轴:

:表示当前维的所有索引值都取
import numpy as np
t = np.array(
  [
      [
          [
           [1,2,3],  
           [4,5,6]
          ],  
          [
           [7,8,9],  
           [10,11,12]
          ],  
          [
           [13,14,15],  
           [16,17,18]
          ]
      ],  
      [
          [
           [19,20,21],  
           [22,23,24]
          ],  
          [
           [25,26,27],  
           [28,29,30]
          ],  
          [
           [31,32,33],
           [34,35,36]
          ]
      ]
  ])
print(t[0,:,:,:])#第0轴或者说第4维(也就是第一个[])下的第一项所有的数据
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]

 [[13 14 15]
  [16 17 18]]]
print(t[:,0,:,:])#第axis=1轴或者第2个[]下说包含的第一项所有数据
[[[ 1  2  3]
  [ 4  5  6]]

 [[19 20 21]
  [22 23 24]]]
print(t[:,:,0,:])
[[[ 1  2  3]
  [ 7  8  9]
  [13 14 15]]

 [[19 20 21]
  [25 26 27]
  [31 32 33]]]
print(t[:,:,:,0])
[[[ 1  4]
  [ 7 10]
  [13 16]]

 [[19 22]
  [25 28]
  [31 34]]]
  • np.newaxis 

np.newaxis相当于新插入一个轴

a=np.array([1,2,3,4,5])
b=a[np.newaxis,:]#插在最前面,从左到右,就按照从高维到低维位置排,在最面加个括号,增加了一维
print a.shape,b.shape
print a
print b

输出结果:
(5,) (1, 5)
[1 2 3 4 5]
[[1 2 3 4 5]]
a=np.array([1,2,3,4,5])
b=a[:,np.newaxis]#插在第二个位置,从左到右,就按照从高维到低维位置排,在里面加个括号
print a.shape,b.shape
print a
print b

输出结果
(5,) (5, 1)
[1 2 3 4 5]
[[1]
 [2]
 [3]
 [4]
 [5]]
  • numpy.expand_dims 

numpy.expand_dims同样是用于扩充数组维度

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

>>> y = np.expand_dims(x, axis=0)  #扩充最高维,等价于 x[np.newaxis,:]或x[np.newaxis]
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)   #看np.newaxis位置(在:之前)可知插入在2之前

>>> y = np.expand_dims(x, axis=1)  #等价于x[:,newaxis]
>>> y
array([[1],
       [2]])
>>> y.shape
(2, 1)  #看np.newaxis位置(在:之后)可知插入在2之后

>>> np.newaxis is None
True

二维情况:

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

[[1 2 3]
 [4 5 6]]
(2, 3)#两行三列,因为第一个大括号下有两组list故是2,第一个大括号下的任意一组[]下又有3个数,故为3.

y = np.expand_dims(x,axis=0)#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=2)#在第个括号里添加括号,以此作为新的一维
print y
print "y.shape: ",y.shape


[[[1]
  [2]
  [3]]

 [[4]
  [5]
  [6]]]
y.shape:  (2, 3, 1)总结:numpy数组所表示的很多[]层层嵌套来表示的话,为了理解记忆,从最左边数,有多少[[[,一般表示是多少维数据.在y.shape下则就有(,,)括号中多少减1个逗号。(,,,,)内从左到右表示响应的[[[[下的维数,由高维到低维,比如:y.shape:  (2, 3, 1),则(从左到右相对应axis=0到axis=2)。y.shape:  (2, 3, 1)中的2表示最高维0轴下有两组list;3表示在每个axis=1轴(前提是在0轴的基础下)下有3组list;最后1表示在每个的axis=2轴下有一组list。
以前总会把axis=0死记为表示行,1就表示列,如果是二维数据,就没错。更高维就不行了。所以要理解里面数字相对于numpy数组一一对应的关系是怎样的,这是我的个人理解,很口语化,为了防止日后忘记,便记录于此。


以下有numpy官网的部分介绍点击打开链接

猜你喜欢

转载自blog.csdn.net/qq_35860352/article/details/80463111