numpy基础

1 经常使用函数

1.1 正态分布

产生num个,均值为0.05,标准差为0.0001的正态分布随机数。数组

ran = np.random.normal(0.05, 0.0001, num)

1.2 np.apply_along_axis()函数

1.3 np.percentile()函数

计算沿指定轴的数据的第q个百分点。返回数组元素的第q个百分点。app

a1 = np.array([[10, 7, 4], [3, 2, 1]]) 
res1 = np.percentile(a1, 60, axis=1)

res1:
指定轴为横轴,取第60个百分点——4+(10-4)0.6=7.6,1+(3-1)0.6=2.2
output:dom

[7.6 2.2]

1.4 np.empty()函数

返回给定形状和类型的新数组,而无需初始化条目。函数

print(np.empty([10, 1], dtype=float))
print(np.empty(10, dtype=float))

output1:输出二维数组,10行1列code

[[1.37505327e-311]
 [6.95218360e-310]
 [1.37710552e-311]
 [1.37710552e-311]
 [1.37710860e-311]
 [1.37710552e-311]
 [1.37710552e-311]
 [1.37710836e-311]
 [0.00000000e+000]
 [0.00000000e+000]]

output2:输出一维数组orm

[1.37505327e-311 6.95218360e-310 1.37710552e-311 1.37710552e-311
1.37710860e-311 1.37710552e-311 1.37710552e-311 1.37710836e-311
 0.00000000e+000 0.00000000e+000]

1.5 multiply()函数

两个矩阵的相同位置的元素相乘,结果矩阵的shape等于输入矩阵的最大行列。当两个输入矩阵的行、列只有一个不相同时,输出矩阵的行列能够广播到最大的行列值(1.2,1.3);当行、列都不一样时,运行错误(1.4)。ip

1) 两个输入矩阵的shape相同时:

alp = np.mat(np.ones((5,2)))
lab = np.mat(([[-1,-3],[1,0],[2,1],[-1,0],[-3,-1]]))
print("矩阵alp:\n%s \n 矩阵lab:\n%s" %(alp,lab))
np.multiply(alp,lab)

Output:get

# 矩阵alp:
    [[ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]] 
    # 矩阵lab:
        [[-1 -3]
         [ 1  0]
         [ 2  1]
         [-1  0]
         [-3 -1]]
    matrix([[-1., -3.],
            [ 1.,  0.],
            [ 2.,  1.],
            [-1.,  0.],
            [-3., -1.]])

2) 两个输入矩阵的shape不相同时:

alp = np.mat(np.ones((5,2)))
lab = np.mat(([[-1],[1],[2],[-1],[-3]]))
print("矩阵alp:\n%s \n 矩阵lab:\n%s" %(alp,lab))
np.multiply(alp,lab)

Output:it

# 矩阵alp:
    [[ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]] 
     # 矩阵lab:
        [[-1]
         [ 1]
         [ 2]
         [-1]
         [-3]]
    matrix([[-1., -1.],
            [ 1.,  1.],
            [ 2.,  2.],
            [-1., -1.],
            [-3., -3.]])

3) 两个输入矩阵的shape不相同时:

alp = np.mat(np.ones((5,2)))
lab = np.mat(([[-1,0]]))
print("矩阵alp:\n%s \n 矩阵lab:\n%s" %(alp,lab))
np.multiply(alp,lab)

Output:ast

# 矩阵alp:
    [[ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]
     [ 1.  1.]] 
    # 矩阵lab:
        [[-1  0]]
    matrix([[-1.,  0.],
            [-1.,  0.],
            [-1.,  0.],
            [-1.,  0.],
            [-1.,  0.]])

4) 两个输入矩阵的shape不相同时:

alp = np.mat(np.ones((5,2)))
lab = np.mat(([[-1],[1],[2],[-1]]))
print("矩阵alp:\n%s \n 矩阵lab:\n%s" %(alp,lab))
np.multiply(alp,lab)

Output:

# 矩阵alp:
            [[ 1.  1.]
             [ 1.  1.]
             [ 1.  1.]
             [ 1.  1.]
     [ 1.  1.]] 
    # 矩阵lab:
        [[-1]
         [ 1]
         [ 2]
         [-1]]
   ValueError: operands could not be broadcast together with shapes (5,2) (4,1)

1.6 np.random.shuffle()

将输入矩阵或列表顺序 随机 打乱。

1) 输入是列表

import numpy as np
if __name__ == "__main__":
    L = [1, 2, 3, 4 , 5, 6, 7, 8, 9]
    for i in range(5):
        #  打乱5次,结果随机
        np.random.shuffle(L)
        print(L)

Output:

[7, 6, 9, 3, 2, 8, 1, 4, 5]
[8, 9, 6, 2, 7, 1, 4, 3, 5]
[3, 7, 2, 1, 5, 4, 6, 9, 8]
[3, 6, 8, 7, 1, 2, 4, 5, 9]
[5, 9, 1, 2, 3, 6, 8, 4, 7]

2) 输入是矩阵

import numpy as np
if __name__ == "__main__":
    R = [
        [5, 3, 0, 1],
        [4, 0, 0, 1],
        [1, 1, 0, 5],
        [1, 0, 0, 4],
        [0, 1, 5, 4],
    ]
    for i in range(5):
        np.random.shuffle(R)
        print(R)

Output:

[[0, 1, 5, 4], [5, 3, 0, 1], [1, 0, 0, 4], [4, 0, 0, 1], [1, 1, 0, 5]]
[[5, 3, 0, 1], [1, 1, 0, 5], [1, 0, 0, 4], [4, 0, 0, 1], [0, 1, 5, 4]]
[[1, 1, 0, 5], [0, 1, 5, 4], [4, 0, 0, 1], [5, 3, 0, 1], [1, 0, 0, 4]]
[[0, 1, 5, 4], [1, 1, 0, 5], [4, 0, 0, 1], [5, 3, 0, 1], [1, 0, 0, 4]]
[[0, 1, 5, 4], [5, 3, 0, 1], [1, 0, 0, 4], [4, 0, 0, 1], [1, 1, 0, 5]]
## 当函数的输入是矩阵时,只打乱一个维度上的的顺序,如:
import numpy as np
if __name__ == "__main__":
    M = [[[1, 2], [3, 4]], [[1, 3], [2, 4]], [[5, 6], [7, 8]]]
    for i in range(5):
        np.random.shuffle(M)
        print(M)

Output:

[[[5, 6], [7, 8]], [[1, 2], [3, 4]], [[1, 3], [2, 4]]]
[[[5, 6], [7, 8]], [[1, 3], [2, 4]], [[1, 2], [3, 4]]]
[[[5, 6], [7, 8]], [[1, 2], [3, 4]], [[1, 3], [2, 4]]]
[[[1, 2], [3, 4]], [[1, 3], [2, 4]], [[5, 6], [7, 8]]]
[[[1, 3], [2, 4]], [[5, 6], [7, 8]], [[1, 2], [3, 4]]]
相关文章
相关标签/搜索