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]]]
相關文章
相關標籤/搜索