python numpy函数的使用(updating)

版权声明:原创博客未经允许请勿转载! https://blog.csdn.net/holmes_MX/article/details/83350788

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

猜你喜欢

转载自blog.csdn.net/holmes_MX/article/details/83350788
今日推荐