日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Scipy快速入门

發布時間:2023/12/29 windows 23 coder
生活随笔 收集整理的這篇文章主要介紹了 Scipy快速入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Scipy快速入門

注意事項

圖床在國外,配合美區、日區網絡使用更佳,如遇圖片加載不出來,考慮換個VPN吧。

監修中敬告

本文處于Preview階段,不對文章內容負任何責任,如有意見探討歡迎留言。

聯系方式——綠泡泡:NeoNexusX

常量

稀疏矩陣 (scipy.sparse)

CSC 壓縮稀疏列(csr_matrix()

用于高效的算數,快速列切分。

    # csr
    csr_arr = np.array([0, 0, 1, 0, 0, 0, 0, 1])
    print(f'csc_matrix(csc_arr) is  : \n{csc_matrix(csr_arr)}\n')

結果如下:

csc_matrix(csc_arr) is  : 
  (0, 2)	1
  (0, 7)	1

CSR 壓縮稀疏行(csc_matrix())

用于快速行切分,更快的矩陣向量乘積。

    # csc
    csc_arr = np.array([[0],
                        [1],
                        [0],
                        [0],
                        [0],
                        [0],
                        ])
    print(f'csc_matrix(csc_arr) is  : \n{csc_matrix(csc_arr)}\n')

結果如下:

csc_matrix(csc_arr) is  : 
  (1, 0)	1

舉一個復雜一點的例子:

    # 獲取對應矩陣
    cm_arr = np.array([[1, 0, 6, 0, 7],
                       [0, 2, 0, 0, 0],
                       [0, 0, 3, 0, 0],
                       [0, 0, 0, 4, 0],
                       [0, 0, 0, 0, 5],
                       ])
    print(f'csr_matrix(cm_arr) is  : \n{csr_matrix(cm_arr)}\n')
    print(f'csc_matrix(cm_arr) is  : \n{csc_matrix(cm_arr)}\n')

輸出結果:

csr_matrix(cm_arr) is  : 
  (0, 0)	1
  (0, 2)	6
  (0, 4)	7
  (1, 1)	2
  (2, 2)	3
  (3, 3)	4
  (4, 4)	5

csc_matrix(cm_arr) is  : 
  (0, 0)	1
  (1, 1)	2
  (0, 2)	6
  (2, 2)	3
  (3, 3)	4
  (0, 4)	7
  (4, 4)	5

獲取非0元素(.data)

代碼如下:

    # 獲取非0元素
    print(f'csc_matrix(cm_arr).data is  : \n{csc_matrix(cm_arr).data}\n')
    print(f'csr_matrix(cm_arr).data is  : \n{csr_matrix(cm_arr).data}\n')

輸出結果:

csc_matrix(cm_arr).data is  : 
[1 2 6 3 4 7 5]

csr_matrix(cm_arr).data is  : 
[1 6 7 2 3 4 5]

獲取非0元素個數(.count_nonzero() )

    # 獲取非0元素個數
    print(f'csr_matrix(cm_arr).count_nonzero() is  : \n{csr_matrix(cm_arr).count_nonzero()}\n')
    print(f'csc_matrix(cm_arr).count_nonzero() is  : \n{csc_matrix(cm_arr).count_nonzero()}\n')

輸出結果:

csr_matrix(cm_arr).count_nonzero() is  : 
7

csc_matrix(cm_arr).count_nonzero() is  : 
7

刪除零元素(.eliminate_zeros())

注意這是一個方法,你如果用在已經建立好的矩陣是沒有效果的:

舉個例子:

    # 減少對應矩陣的0數目
    c_m = csc_matrix(cm_arr)
    c_m.eliminate_zeros()
    r_m = csr_matrix(cm_arr)
    r_m.eliminate_zeros()
    print(f'csc_matrix(cm_arr).eliminate_zeros() is  : \n{c_m}\n')
    print(f'csr_matrix(cm_arr).eliminate_zeros() is  : \n{r_m}\n')

可以看到這里的輸出和上文的內容并沒有發生什么變化:

csc_matrix(cm_arr).eliminate_zeros() is  : 
  (0, 0)	1
  (1, 1)	2
  (0, 2)	6
  (2, 2)	3
  (3, 3)	4
  (0, 4)	7
  (4, 4)	5

csr_matrix(cm_arr).eliminate_zeros() is  : 
  (0, 0)	1
  (0, 2)	6
  (0, 4)	7
  (1, 1)	2
  (2, 2)	3
  (3, 3)	4
  (4, 4)	5

我們再來舉個例子:

    row = [0, 0, 0, 1, 1, 1, 2, 2, 2]  # 行指標
    col = [0, 1, 2, 0, 1, 2, 0, 1, 2]  # 列指標
    data = [1, 0, 1, 0, 1, 1, 1, 1, 0]  # 在行指標列指標下的數字
    team = csr_matrix((data, (row, col)), shape=(3, 3))

    print(f'team is : \n{team}\n')
    print(f'team type is : \n{type(team)}\n')
    print(f'team.shape is : \n{team.shape}\n')

    team.eliminate_zeros()
    print(f'team.eliminate_zeros is : \n{team}\n')

輸出結果如下;

team is : 
  (0, 0)	1
  (0, 1)	0
  (0, 2)	1
  (1, 0)	0
  (1, 1)	1
  (1, 2)	1
  (2, 0)	1
  (2, 1)	1
  (2, 2)	0

team type is : 
<class 'scipy.sparse._csr.csr_matrix'>

team.shape is : 
(3, 3)

team.eliminate_zeros is : 
  (0, 0)	1
  (0, 2)	1
  (1, 1)	1
  (1, 2)	1
  (2, 0)	1
  (2, 1)	1

可以看到team轉化為另一個非稀疏的矩陣類型。

CSC和CSR的轉換 (.tocsr() / .tocsc())

這個就很簡單了,沒什么可說的:

    # csr 2 csc
    print(f'csr_matrix is  : \n{r_m}\n')
    print(f'c_m.tocsr() is  : \n{c_m.tocsr()}\n')

將對應的CSC轉化成CSR:

csr_matrix is  : 
  (0, 0)	1
  (0, 2)	6
  (0, 4)	7
  (1, 1)	2
  (2, 2)	3
  (3, 3)	4
  (4, 4)	5

c_m.tocsr() is  : 
  (0, 0)	1
  (0, 2)	6
  (0, 4)	7
  (1, 1)	2
  (2, 2)	3
  (3, 3)	4
  (4, 4)	5

圖 (CSGraph)

使用鄰接矩陣來構建一個圖如下:

    # graph part
    # 構建了一個正方形的圖

    arr = np.array([
        [0, 2, 0, 4],
        [2, 0, 3, 0],
        [0, 3, 0, 4],
        [4, 0, 4, 0],
    ])
    graph = csr_matrix(arr)
    print(f'graph is  : \n{graph}\n')

示意圖如下:

graph LR; A <--2-->B<--3-->C<--4-->D<--4-->A

結果如下:

graph is  : 
  (0, 1)	2
  (0, 3)	4
  (1, 0)	2
  (1, 2)	3
  (2, 1)	3
  (2, 3)	4
  (3, 0)	4
  (3, 2)	4

連通性檢測 (connected_components())

    n_components, labels = connected_components(graph, directed=False, connection='weak', return_labels=True)

    print("連通分量數量:", n_components)
    print("節點標簽:", labels)

連通性輸出結果如下:

連通分量數量: 1
節點標簽: [0 0 0 0]

由于這里沒有設置節點標簽,所以輸出全是0.

最短路 (Dijkstra()、floyd_warshall() 、bellman_ford() )

三個函數只需要將圖輸入進去就可以得到對應的到各個節點的最短路徑。

# dijkstra
print(f'dijkstra seq is : \n{dijkstra(graph, indices=0)}\n')

# Floyd warshall
print(f'floyd_warshall matrix is : \n{floyd_warshall(graph)}\n')

# bellman ford
print(f'bellman_ford matrix is : \n{bellman_ford(graph, indices=0)}\n')

結果如下:

dijkstra seq is : 
[0. 2. 5. 1.]

floyd_warshall matrix is : 
[[0. 2. 5. 1.]
 [2. 0. 3. 3.]
 [5. 3. 0. 4.]
 [1. 3. 4. 0.]]

bellman_ford matrix is : 
[0. 2. 5. 1.]

廣搜與深搜 (depth_first_order(), breadth_first_order())

兩個函數的作用都是以某個參數為基點返回對應的順序和對應節點的前驅序列。

舉個例子:

    # depth first order
    print(f'depth_first_order seq is : \n{depth_first_order(graph, 0)}\n')

    # breadth first order
    print(f'breadth_first_order seq is : \n{breadth_first_order(graph, 0)}\n')

輸出結果:

depth_first_order seq is : 
(array([0, 1, 2, 3]), array([-9999,     0,     1,     2]))

breadth_first_order seq is : 
(array([0, 1, 3, 2]), array([-9999,     0,     1,     0]))

詳見:scipy.sparse.csgraph.depth_first_order — SciPy v1.11.4 Manual

matlab數據讀取與導出( io.savemat()、io.loadmat())

# matlab part
# 導出matlab 數據 等等
matlab_output = io.savemat('filename.mat', {'data': arr})
print(f'matlab_output is \n {matlab_output} \n')

# 讀取 matlab 數據 等等
matlab_intput = io.loadmat('filename.mat')
print(f'matlab_input is \n{matlab_intput}\n')
matlab_intput_data = matlab_intput['data']
print(f'matlab_input \'s data is \n{matlab_intput_data}\n')

輸出結果如下:

返回的是字典包含了很多信息,我們可以通過字典的方式來提取內容。

matlab_output is 
 None 

matlab_input is 
{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Sun Dec 10 21:40:56 2023', '__version__': '1.0', '__globals__': [], 'data': array([[0, 2, 0, 1],
       [2, 0, 3, 0],
       [0, 3, 0, 4],
       [1, 0, 4, 0]])}

matlab_input 's data is 
[[0 2 0 1]
 [2 0 3 0]
 [0 3 0 4]
 [1 0 4 0]]

數據的外圍又被包上了一個數組,我們可以通過如下方式來實現讀取,將其變為1維的:

    matlab_intput_without = io.loadmat('filename.mat', squeeze_me=True)
    print(f'matlab_intput_without is \n{matlab_intput_without}\n')
    matlab_intput_data_without = matlab_intput_without['data']
    print(f'matlab_intput_data_without \'s data is \n{matlab_intput_data_without}\n')

輸出結果如下:

matlab_intput_without is 
{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Sun Dec 10 21:44:24 2023', '__version__': '1.0', '__globals__': [], 'data': array([[0, 2, 0, 1],
       [2, 0, 3, 0],
       [0, 3, 0, 4],
       [1, 0, 4, 0]])}

參考文獻

.eliminate_zeros()函數-CSDN博客

總結

以上是生活随笔為你收集整理的Scipy快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。