numpy 实践记录

squeeze 压缩维度为1的numpy向量

argmax 获取最大值的下标

np.random.shuffle(index)

reshape是从低维度到高维度。max,sum等函数都是注意axis,不选择就是全体计算。

swapaxes 转换轴,将两个选择的轴对调,在CNN中X乘W有的时候需要拉伸,如果轴不同结果不对。

看print 出来的np.array,最后在一维的是最后的维度。

可以看下面的示例。

import numpy as np
a=np.arange(3*4*5).reshape(3,4,5)

# array([[[ 0,  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, 37, 38, 39]],
# 
#        [[40, 41, 42, 43, 44],
#         [45, 46, 47, 48, 49],
#         [50, 51, 52, 53, 54],
#         [55, 56, 57, 58, 59]]])
b=a.reshape(6,10)
# array([[ 0,  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, 37, 38, 39],
#        [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
#        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])


b.swapaxes(1,2)
# array([[[ 0,  5, 10, 15],
#         [ 1,  6, 11, 16],
#         [ 2,  7, 12, 17],
#         [ 3,  8, 13, 18],
#         [ 4,  9, 14, 19]],
# 
#        [[20, 25, 30, 35],
#         [21, 26, 31, 36],
#         [22, 27, 32, 37],
#         [23, 28, 33, 38],
#         [24, 29, 34, 39]],
# 
#        [[40, 45, 50, 55],
#         [41, 46, 51, 56],
#         [42, 47, 52, 57],
#         [43, 48, 53, 58],
#         [44, 49, 54, 59]]])

random.randn(n) n这里指的是获取n个独立高斯分布的变量,使用元组就是帮你reshape一下。

因此如果直接用n=1000,那么方差就是1000而不是1,这里要注意。

np.random.randn(100).reshape(10,5,2)

posted on 2018-01-29 20:14  猪的饲养员  阅读(164)  评论(0编辑  收藏  举报

导航