numpy下的axis(维度轴),np.concatenate(),np.expand_dims(),np.newaxis()的图视化解释

一、数组与维度

 从以上可知数组最前面有多少个[,就表示有多少维。


二、axis维度轴与数组与维度

备注:axis=0表示最高维度n轴,axis=1表示n-1维轴,以此类推


维度在数组中的理解,见下文:

>>> 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.shape)
(2, 3, 2, 3)
>>> 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.concatenate(),np.expand_dims(),np.newaxis()

3.1、np.concatenate(对数列或矩阵进行合并)

一维情况:

>>> import numpy as np
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> np.concatenate((a,b),axis=0) # 默认情况下,axis=0可以不写

[1, 2, 3, 4, 5, 6]

备注:因为上述a和b都是一维的,所以当指定axis=1时,程序就会报错。

二维情况:

>>> y1 = np.array([ [[1,0],[1,0]] , [[0,0],[0,0]] ])
>>> y2 = np.array([ [[0,0],[0,0]] , [[0,1],[0,1]] ])
>>> print(y1.shape)

>>> y3 = np.concatenate((y1,y2),axis=0)
# 可知y1,y2都是3维数组,axis=1则表示y1,y2的拼接方式按照3-0=3维的结构来拼接

>>> print(y3)
>>> print(y3.shape)

(2, 2, 2)

[[[1 0]
  [1 0]]

 [[0 0]
  [0 0]]

 [[0 0]
  [0 0]]

 [[0 1]
  [0 1]]]

(4, 2, 2)
>>> y1 = np.array([ [[1,0],[1,0]] , [[0,0],[0,0]] ])
>>> y2 = np.array([ [[0,0],[0,0]] , [[0,1],[0,1]] ])
>>> print(y1.shape)

>>> y3 = np.concatenate((y1,y2),axis=1)
# 可知y1,y2都是3维数组,axis=1则表示y1,y2的拼接方式按照3-1=2维的结构来拼接

>>> print(y3)
>>> print(y3.shape)

(2, 2, 2)

[[[1 0]
  [1 0]
  [0 0]
  [0 0]]

 [[0 0]
  [0 0]
  [0 1]
  [0 1]]]

(2, 4, 2)
>>> y1 = np.array([ [[1,0],[1,0]] , [[0,0],[0,0]] ])
>>> y2 = np.array([ [[0,0],[0,0]] , [[0,1],[0,1]] ])
>>> print(y1.shape)

>>> y3 = np.concatenate((y1,y2),axis=2)
# 可知y1,y2都是3维数组,axis=2则表示y1,y2的拼接方式按照3-2=1维的结构来拼接

>>> print(y3)
>>> print(y3.shape)

(2, 2, 2)

[[[1 0 0 0]
  [1 0 0 0]]

 [[0 0 0 1]
  [0 0 0 1]]]

(2, 2, 4)

备注:所谓的按A维拼接,意思是两组数据对应的A维度数据相加,其他维度不改变(很关键,理解这句话和上面内容)

3.2、np.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)

3.3、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]]

参考资料网址:

1、numpy 下的axis(轴)详细含义,np.expand_dims(x,axis=0),np.newaxis解释:https://blog.csdn.net/qq_35860352/article/details/80463111

2、np.concatenate的用法:https://www.jianshu.com/p/a094a954ff61

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值