Scipy快速入門 註意事項 圖床在國外,配合美區、日區網路使用更佳,如遇圖片載入不出來,考慮換個VPN吧。 監修中敬告 本文處於Preview階段,不對文章內容負任何責任,如有意見探討歡迎留言。 聯繫方式——綠泡泡:NeoNexusX 常量 稀疏矩陣 (scipy.sparse) CSC 壓縮稀疏 ...
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]])}