numpy库

创建数组

>>> np.ones([3,4])          # 值为浮点1的矩阵
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
>>> np.zeros([3,4])         # 值为浮点0的矩阵
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
>>> np.array([[1,2,3],[4,5,6]])    # 深拷贝
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.asarray([[1,2,3],[4,5,6]])  # 浅拷贝
array([[1, 2, 3], 
       [4, 5, 6]])    

数组属性

>>> d=np.random.rand(2,3,4)   
>>> d
array([[[ 0.75432633,  0.57838693,  0.9298954 ,  0.01667251],
        [ 0.81329288,  0.18277114,  0.40584013,  0.30055277],
        [ 0.12805651,  0.38665696,  0.29655644,  0.61897223]],

       [[ 0.1072343 ,  0.34588952,  0.96080471,  0.56854714],
        [ 0.76898631,  0.4881976 ,  0.85973732,  0.1127037 ],
        [ 0.27826845,  0.81381869,  0.03623546,  0.11600406]]])
>>> d.size   # 元素个数
24
>>> d.shape  # 数组形状
(2, 3, 4)
>>> d.dtype  # 元素类型
dtype('float64')
>>> d.ndim   # 数组维度
3
>>> d.itemsize # 元素字节长度
8

随机数(均匀分布)

>>> np.random.uniform(0,100)  # 创建指定范围的随机浮点数
79.95384787852716
>>> np.random.randint(0,100)  # 创建指定范围的随机整数
13
>>> np.random.rand(3,3)       # 创建指定[0, 1]之间的浮点数矩阵
array([[ 0.3672736 ,  0.64473369,  0.51615523],
       [ 0.12016337,  0.80318054,  0.02896355],
       [ 0.22252509,  0.41416445,  0.9141706 ]])

正态分布

np.random.normal(1, 0.5, (3,4))   # 指定均值、标准差、维度
array([[ 0.86131114,  0.97396862,  1.41560527,  0.9125575 ],
       [ 0.50615441,  1.30600456,  0.28319673,  0.73952316],
       [ 1.683847  ,  1.88634808,  0.81664031,  1.04438156]])

slice(切片)

 >>> a
array([[ 0.91108116,  0.38937371,  0.44120738,  0.9711543 ,  0.50400801,
         0.8159346 ],
       [ 0.23592619,  0.9446476 ,  0.83056827,  0.80585383,  0.98337604,
         0.34744683]])
>>> a[:,3:-1]
array([[ 0.9711543 ,  0.50400801],
       [ 0.80585383,  0.98337604]])

reshape (reshape前后元素个数要一致)

>>> a=np.random.rand(2,6)
>>> b=a.reshape(3,4)
>>> a
array([[ 0.91108116,  0.38937371,  0.44120738,  0.9711543 ,  0.50400801,
         0.8159346 ],
       [ 0.23592619,  0.9446476 ,  0.83056827,  0.80585383,  0.98337604,
         0.34744683]])
>>> b
array([[ 0.91108116,  0.38937371,  0.44120738,  0.9711543 ],
       [ 0.50400801,  0.8159346 ,  0.23592619,  0.9446476 ],
       [ 0.83056827,  0.80585383,  0.98337604,  0.34744683]])

条件计算

>>> a
array([[80, 88],
       [82, 81],
       [84, 75],
       [86, 83],
       [75, 81]])
>>> a>80
array([[False,  True],
       [ True,  True],
       [ True, False],
       [ True,  True],
       [False,  True]], dtype=bool)
>>> np.where(a<=80, 0, 1)  # <=80的元素替换为0,否则替换为1
array([[0, 1],
       [1, 1],
       [1, 0],
       [1, 1],
       [0, 1]])

最大(小)值

>>> a
array([[80, 88],
       [82, 81],
       [84, 75],
       [86, 83],
       [75, 81]])
>>> np.amax(a, axis=0)   # 按列求最大值
array([86, 88])
>>> np.amax(a, axis=1)   # 按行求最大值
array([88, 82, 84, 86, 81])
>>> np.amin(a, axis=0)   # 按列求最小值
array([75, 75])
>>> np.amin(a, axis=1)   # 按行求最小值
array([80, 81, 75, 83, 75])
>>> np.mean(a, axis=1)   # 平均值
array([ 84. ,  81.5,  79.5,  84.5,  78. ])
>>> np.mean(a, axis=0)   
array([ 81.4,  81.6])
>>> np.std(a, axis=0)    # 标准差
array([ 3.77359245,  4.1761226 ])  
>>> np.std(a, axis=1)
array([ 4. ,  0.5,  4.5,  1.5,  3. ])

数组与数值的运算

a
array([[84, 92],
       [86, 85],
       [88, 79],
       [90, 87],
       [79, 85]])
>>> a[:,0]+=4
>>> a
array([[88, 92],
       [90, 85],
       [92, 79],
       [94, 87],
       [83, 85]])
>>> a[2:4,1] -= 10
>>> a
array([[88, 92],
       [90, 85],
       [92, 69],
       [94, 77],
       [83, 85]])

数组拼接

>>> v1
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
>>> v2
[[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
>>> np.vstack((v1, v2))    # 垂直拼接
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]])
>>> np.hstack((v1, v2))   # 水平拼接
array([[ 0,  1,  2,  3,  4,  5, 12, 13, 14, 15, 16, 17],
       [ 6,  7,  8,  9, 10, 11, 18, 19, 20, 21, 22, 23]])
>>> 

矩阵乘法

>>> a = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
>>> b = np.array([[0.4], [0.6]])
>>> np.dot(a, b)
array([[ 84.8],
       [ 81.4],
       [ 78.6],
       [ 84.2],
       [ 78.6]])

参考文档:

https://www.jianshu.com/p/a260a8c43e44

https://docs.scipy.org/doc/numpy/user/quickstart.html

原文地址:https://www.cnblogs.com/chenny7/p/9973609.html