python numpy函数的使用(updating)

0.写作目的

好记性不如烂笔头。

1. numpy 中的 axis的理解

以三维tensor为例:

numpy.sum( tensor, axis ) numpy.mean( tensor, axis)

其中axis = -1是对最里面的一个维度操作。如numpy.sum( tensor, axis = -1 ), 即对第2维度进行操作,即对channel进行相加。

实例为:

import numpy as np
a = [[[1, 2, 3,], [4, 5, 6]], [[1.1, 2.1, 3.1], [4.1, 5.1, 6.1]]]
print( np.sum(a, axis=-1) )
print( np.sum(a, axis=0) )
print( np.sum(a, axis=1) )
print( np.sum(a, axis=2) )


## output
#axis = -1  ## channel
[[ 6.  15. ]
 [ 6.3 15.3]]

# axis = 0  ## height
[[ 2.1  4.1  6.1]
 [ 8.1 10.1 12.1]]

# axis = 1   ## width
[[5.  7.  9. ]
 [5.2 7.2 9.2]]

# axis = 2  ## channel
[[ 6.  15. ]
 [ 6.3 15.3]]

2. numpy中 joining 的使用

np.stack  np.vstack  np.hstack  np.dstack  np.concatenate 以及np.block的使用

import numpy as np

a = [[1, 2], [3, 4]]
b = [[5, 6], [7, 8]]

c = np.stack( (a, b) ) ## add new one axis
## result 
[  [[1,2],[3, 4]], 
   [[5, 6], [7,8]]  ]


c = np.vstack( (a, b) ) ## stack according to vertically (row wise).
## result
[ [1, 2], [5, 6], [3, 4], [7, 8] ]


c = np.hstack( (a, b) )  ## Stack arrays in sequence horizontally (column wise)
## result
[ [1, 2, 5, 6], [3, 4, 7, 8] ]


c = np.dstack()  ## Stack arrays in sequence depth wise (along third axis)


c = np.concatenate( (a, b), axis=0 )  ##Join a sequence of arrays along an existing axis
## result   width channel in this exampel  == np.vstack
[ [1, 2], [5, 6], [3, 4], [7, 8] ]



c = np.concatenate( (a, b), axis=1 )  ##Join a sequence of arrays along an existing axis
## result   width channel in this exampel  == np.hstack
[ [1, 2, 5, 6], [3, 4, 7, 8] ]

 

[Reference]

[1] numpy 官方: https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html#joining-arrays

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值