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')
示意图如下:
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]])}
参考文献
Scipy快速入门的更多相关文章
- numpy快速入门
numpy快速入门 numpy是python的科学计算的核心库,很多更高层次的库都基于numpy.博主不太喜欢重量级的MATLAB,于是用numpy进行科学计算成为了不二选择. 本文主要参考Scipy ...
- 快速入门 Python 数据分析实用指南
Python 现如今已成为数据分析和数据科学使用上的标准语言和标准平台之一.那么作为一个新手小白,该如何快速入门 Python 数据分析呢? 下面根据数据分析的一般工作流程,梳理了相关知识技能以及学习 ...
- python数据可视化神库:Matplotlib快速入门
Matplotlib易于使用,是Python中了不起的可视化库.它建立在NumPy数组的基础上,旨在与更广泛的SciPy堆栈一起工作,并由几个图组成:线图.条形图.散点图.直方图等. 快速入门 imp ...
- Web Api 入门实战 (快速入门+工具使用+不依赖IIS)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...
- SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)
SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...
- 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...
- 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- Mybatis框架 的快速入门
MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...
- grunt快速入门
快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...
随机推荐
- 2023年了,复习了一下spring boot配置使用mongodb
前言 MongoDB是一个基于分布式文件存储的开源数据库系统,使用C++语言编写.它是一个介于关系数据库和非关系数据库之间的产品,具有类似关系数据库的功能,但又有一些非关系数据库的特点.MongoDB ...
- 「codeforces - 1633F」Perfect Matching
link. 首先所有的 activated nodes 组合成了一棵以 \(1\) 为根的有根树.询问即求由 activated nodes 组成的树的最大匹配.对于树上最大匹配有一个贪心策略:自底向 ...
- Python面向对象——反射(hasattr、getattr、setattr、delattr)、内置方法(__str__和__del__)、元类(介绍,创建类的流程,exec,自定义元类)、属性查找
文章目录 反射 内置方法 __str__方法 __del__函数 元类 元类介绍 class关键字创建类的流程分析 补充:exec的用法 自定义元类控制类StanfordTeacher的创建 自定义元 ...
- sql分组后排序计算
用法:RANK() OVER(PARTITION BY 分组字段 ORDER BY 排序字段 ) 例子:要得到n4列 ---创建测试数据create table tb(n1 varchar2(40) ...
- FreeRTOS 操作系统
FreeRTOS操作系统 01 FreeRTOS 的定义和概述 定义:FreeRTOS(Free-Real-Time Operating System)是一个开源的实时操作系统内核,专门为嵌入式系统设 ...
- the solution of Mining Your Own Business
the description of problem (我看的是 PDF 里面的原题所以这里描述会和题目不一样,但是大意一致) 给定一个未必连通的无向图,问最少在几个点设置出口,可以保证任意一个点坍塌 ...
- 原创基于Scrum框架产研团队运作20问
学习完了 Scrum,实际使用中,是否遇到/思考过下面的问题? Product Owner的老板是谁.谁来给 Product Owner打绩效.考核的标准是啥? Scrum Master 的老板是谁. ...
- 14.9 Socket 高效文件传输
网络上的文件传输功能也是很有必要实现一下的,网络传输文件的过程通常分为客户端和服务器端两部分.客户端可以选择上传或下载文件,将文件分块并逐块发送到服务器,或者从服务器分块地接收文件.服务器端接收来自客 ...
- Kubernetes文档支持的版本
简介 https://kubernetes.io/zh-cn/docs/ 官方中文网站一共是可以阅读5个k8s版本的文档.这5个版本包括k8s最新版和最近的4个版本.例如当前最新版是1.28,那么文档 ...
- 虹科干货 | 什么是Redis数据集成(RDI)?
大量的应用程序.日益增长的用户规模.不断扩展的技术需求,以及对即时响应的持续追求.想想这些是否正是你在经历的.也许你尝试过自己构建工具来应对这些需求,但是大量的编码和集成工作使你焦头烂额.那你是否知道 ...