翼度科技»论坛 编程开发 python 查看内容

Scipy快速入门

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
Scipy快速入门

注意事项

图床在国外,配合美区、日区网络使用更佳,如遇图片加载不出来,考虑换个VPN吧。
监修中敬告

本文处于Preview阶段,不对文章内容负任何责任,如有意见探讨欢迎留言。
联系方式——绿泡泡:NeoNexusX
常量

稀疏矩阵 (scipy.sparse)

CSC 压缩稀疏列(csr_matrix()

用于高效的算数,快速列切分。
  1.     # csr
  2.     csr_arr = np.array([0, 0, 1, 0, 0, 0, 0, 1])
  3.     print(f'csc_matrix(csc_arr) is  : \n{csc_matrix(csr_arr)}\n')
复制代码
结果如下:
  1. csc_matrix(csc_arr) is  :
  2.   (0, 2)        1
  3.   (0, 7)        1
复制代码
CSR 压缩稀疏行(csc_matrix())

用于快速行切分,更快的矩阵向量乘积。
  1.     # csc
  2.     csc_arr = np.array([[0],
  3.                         [1],
  4.                         [0],
  5.                         [0],
  6.                         [0],
  7.                         [0],
  8.                         ])
  9.     print(f'csc_matrix(csc_arr) is  : \n{csc_matrix(csc_arr)}\n')
复制代码
结果如下:
  1. csc_matrix(csc_arr) is  :
  2.   (1, 0)        1
复制代码
举一个复杂一点的例子:
  1.     # 获取对应矩阵
  2.     cm_arr = np.array([[1, 0, 6, 0, 7],
  3.                        [0, 2, 0, 0, 0],
  4.                        [0, 0, 3, 0, 0],
  5.                        [0, 0, 0, 4, 0],
  6.                        [0, 0, 0, 0, 5],
  7.                        ])
  8.     print(f'csr_matrix(cm_arr) is  : \n{csr_matrix(cm_arr)}\n')
  9.     print(f'csc_matrix(cm_arr) is  : \n{csc_matrix(cm_arr)}\n')
复制代码
输出结果:
  1. csr_matrix(cm_arr) is  :
  2.   (0, 0)        1
  3.   (0, 2)        6
  4.   (0, 4)        7
  5.   (1, 1)        2
  6.   (2, 2)        3
  7.   (3, 3)        4
  8.   (4, 4)        5
  9. csc_matrix(cm_arr) is  :
  10.   (0, 0)        1
  11.   (1, 1)        2
  12.   (0, 2)        6
  13.   (2, 2)        3
  14.   (3, 3)        4
  15.   (0, 4)        7
  16.   (4, 4)        5
复制代码
获取非0元素(.data)

代码如下:
  1.     # 获取非0元素
  2.     print(f'csc_matrix(cm_arr).data is  : \n{csc_matrix(cm_arr).data}\n')
  3.     print(f'csr_matrix(cm_arr).data is  : \n{csr_matrix(cm_arr).data}\n')
复制代码
输出结果:
  1. csc_matrix(cm_arr).data is  :
  2. [1 2 6 3 4 7 5]
  3. csr_matrix(cm_arr).data is  :
  4. [1 6 7 2 3 4 5]
复制代码
获取非0元素个数(.count_nonzero() )
  1.     # 获取非0元素个数
  2.     print(f'csr_matrix(cm_arr).count_nonzero() is  : \n{csr_matrix(cm_arr).count_nonzero()}\n')
  3.     print(f'csc_matrix(cm_arr).count_nonzero() is  : \n{csc_matrix(cm_arr).count_nonzero()}\n')
复制代码
输出结果:
  1. csr_matrix(cm_arr).count_nonzero() is  :
  2. 7
  3. csc_matrix(cm_arr).count_nonzero() is  :
  4. 7
复制代码
删除零元素(.eliminate_zeros())

注意这是一个方法,你如果用在已经建立好的矩阵是没有效果的:
举个例子:
  1.     # 减少对应矩阵的0数目
  2.     c_m = csc_matrix(cm_arr)
  3.     c_m.eliminate_zeros()
  4.     r_m = csr_matrix(cm_arr)
  5.     r_m.eliminate_zeros()
  6.     print(f'csc_matrix(cm_arr).eliminate_zeros() is  : \n{c_m}\n')
  7.     print(f'csr_matrix(cm_arr).eliminate_zeros() is  : \n{r_m}\n')
复制代码
可以看到这里的输出和上文的内容并没有发生什么变化:
  1. csc_matrix(cm_arr).eliminate_zeros() is  :
  2.   (0, 0)        1
  3.   (1, 1)        2
  4.   (0, 2)        6
  5.   (2, 2)        3
  6.   (3, 3)        4
  7.   (0, 4)        7
  8.   (4, 4)        5
  9. csr_matrix(cm_arr).eliminate_zeros() is  :
  10.   (0, 0)        1
  11.   (0, 2)        6
  12.   (0, 4)        7
  13.   (1, 1)        2
  14.   (2, 2)        3
  15.   (3, 3)        4
  16.   (4, 4)        5
复制代码
我们再来举个例子:
  1.     row = [0, 0, 0, 1, 1, 1, 2, 2, 2]  # 行指标
  2.     col = [0, 1, 2, 0, 1, 2, 0, 1, 2]  # 列指标
  3.     data = [1, 0, 1, 0, 1, 1, 1, 1, 0]  # 在行指标列指标下的数字
  4.     team = csr_matrix((data, (row, col)), shape=(3, 3))
  5.     print(f'team is : \n{team}\n')
  6.     print(f'team type is : \n{type(team)}\n')
  7.     print(f'team.shape is : \n{team.shape}\n')
  8.     team.eliminate_zeros()
  9.     print(f'team.eliminate_zeros is : \n{team}\n')
复制代码
输出结果如下;
  1. team is :
  2.   (0, 0)        1
  3.   (0, 1)        0
  4.   (0, 2)        1
  5.   (1, 0)        0
  6.   (1, 1)        1
  7.   (1, 2)        1
  8.   (2, 0)        1
  9.   (2, 1)        1
  10.   (2, 2)        0
  11. team type is :
  12. <class 'scipy.sparse._csr.csr_matrix'>
  13. team.shape is :
  14. (3, 3)
  15. team.eliminate_zeros is :
  16.   (0, 0)        1
  17.   (0, 2)        1
  18.   (1, 1)        1
  19.   (1, 2)        1
  20.   (2, 0)        1
  21.   (2, 1)        1
复制代码
可以看到team转化为另一个非稀疏的矩阵类型。
CSC和CSR的转换 (.tocsr() / .tocsc())

这个就很简单了,没什么可说的:
  1.     # csr 2 csc
  2.     print(f'csr_matrix is  : \n{r_m}\n')
  3.     print(f'c_m.tocsr() is  : \n{c_m.tocsr()}\n')
复制代码
将对应的CSC转化成CSR:
  1. csr_matrix is  :
  2.   (0, 0)        1
  3.   (0, 2)        6
  4.   (0, 4)        7
  5.   (1, 1)        2
  6.   (2, 2)        3
  7.   (3, 3)        4
  8.   (4, 4)        5
  9. c_m.tocsr() is  :
  10.   (0, 0)        1
  11.   (0, 2)        6
  12.   (0, 4)        7
  13.   (1, 1)        2
  14.   (2, 2)        3
  15.   (3, 3)        4
  16.   (4, 4)        5
复制代码
图 (CSGraph)

使用邻接矩阵来构建一个图如下:
  1.     # graph part
  2.     # 构建了一个正方形的图
  3.     arr = np.array([
  4.         [0, 2, 0, 4],
  5.         [2, 0, 3, 0],
  6.         [0, 3, 0, 4],
  7.         [4, 0, 4, 0],
  8.     ])
  9.     graph = csr_matrix(arr)
  10.     print(f'graph is  : \n{graph}\n')
复制代码
示意图如下:
graph LR;A BCDA结果如下:
  1. graph is  :
  2.   (0, 1)        2
  3.   (0, 3)        4
  4.   (1, 0)        2
  5.   (1, 2)        3
  6.   (2, 1)        3
  7.   (2, 3)        4
  8.   (3, 0)        4
  9.   (3, 2)        4
复制代码
连通性检测 (connected_components())
  1.     n_components, labels = connected_components(graph, directed=False, connection='weak', return_labels=True)
  2.     print("连通分量数量:", n_components)
  3.     print("节点标签:", labels)
复制代码
连通性输出结果如下:
  1. 连通分量数量: 1
  2. 节点标签: [0 0 0 0]
复制代码
由于这里没有设置节点标签,所以输出全是0.
最短路 (Dijkstra()、floyd_warshall() 、bellman_ford() )

三个函数只需要将图输入进去就可以得到对应的到各个节点的最短路径。
  1. # dijkstra
  2. print(f'dijkstra seq is : \n{dijkstra(graph, indices=0)}\n')
  3. # Floyd warshall
  4. print(f'floyd_warshall matrix is : \n{floyd_warshall(graph)}\n')
  5. # bellman ford
  6. print(f'bellman_ford matrix is : \n{bellman_ford(graph, indices=0)}\n')
复制代码
结果如下:
  1. dijkstra seq is :
  2. [0. 2. 5. 1.]
  3. floyd_warshall matrix is :
  4. [[0. 2. 5. 1.]
  5. [2. 0. 3. 3.]
  6. [5. 3. 0. 4.]
  7. [1. 3. 4. 0.]]
  8. bellman_ford matrix is :
  9. [0. 2. 5. 1.]
复制代码
广搜与深搜 (depth_first_order(), breadth_first_order())

两个函数的作用都是以某个参数为基点返回对应的顺序和对应节点的前驱序列。
举个例子:
  1.     # depth first order
  2.     print(f'depth_first_order seq is : \n{depth_first_order(graph, 0)}\n')
  3.     # breadth first order
  4.     print(f'breadth_first_order seq is : \n{breadth_first_order(graph, 0)}\n')
复制代码
输出结果:
  1. depth_first_order seq is :
  2. (array([0, 1, 2, 3]), array([-9999,     0,     1,     2]))
  3. breadth_first_order seq is :
  4. (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())
  1. # matlab part
  2. # 导出matlab 数据 等等
  3. matlab_output = io.savemat('filename.mat', {'data': arr})
  4. print(f'matlab_output is \n {matlab_output} \n')
  5. # 读取 matlab 数据 等等
  6. matlab_intput = io.loadmat('filename.mat')
  7. print(f'matlab_input is \n{matlab_intput}\n')
  8. matlab_intput_data = matlab_intput['data']
  9. print(f'matlab_input \'s data is \n{matlab_intput_data}\n')
复制代码
输出结果如下:
返回的是字典包含了很多信息,我们可以通过字典的方式来提取内容。
  1. matlab_output is
  2. None
  3. matlab_input is
  4. {'__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],
  5.        [2, 0, 3, 0],
  6.        [0, 3, 0, 4],
  7.        [1, 0, 4, 0]])}
  8. matlab_input 's data is
  9. [[0 2 0 1]
  10. [2 0 3 0]
  11. [0 3 0 4]
  12. [1 0 4 0]]
复制代码
数据的外围又被包上了一个数组,我们可以通过如下方式来实现读取,将其变为1维的:
  1.     matlab_intput_without = io.loadmat('filename.mat', squeeze_me=True)
  2.     print(f'matlab_intput_without is \n{matlab_intput_without}\n')
  3.     matlab_intput_data_without = matlab_intput_without['data']
  4.     print(f'matlab_intput_data_without \'s data is \n{matlab_intput_data_without}\n')
复制代码
输出结果如下:
  1. matlab_intput_without is
  2. {'__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],
  3.        [2, 0, 3, 0],
  4.        [0, 3, 0, 4],
  5.        [1, 0, 4, 0]])}
复制代码
参考文献

.eliminate_zeros()函数-CSDN博客

来源:https://www.cnblogs.com/NeoNexus/p/17933434.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

上一篇: Scipy快速入门

下一篇: Scipy快速入门

举报 回复 使用道具