scipy笔记:scipy.sparse
1 稀疏矩阵介绍
在networkx包中,很多运算返回的是sparse matrix(如nx.laplacian_matrix),这是稀疏矩阵格式。隶属于scipy.sparse
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([2, 3])
G.add_edge(1, 2)
G.add_edges_from([(1, 3)])print(G.edges([3,2]))
#[(3, 1), (2, 1)nx.laplacian_matrix(G)
'''
<3x3 sparse matrix of type '<class 'numpy.intc'>'with 7 stored elements in Compressed Sparse Row format>
'''
在矩阵中,若数值为0的元素数目远远多于非0元素的数目,并且非0元素分布没有规律时,则称该矩阵为稀疏矩阵。
2 稀疏矩阵举例:
2.1 bsr矩阵
block sparse row matrix
bsr_matrix(arg1[, shape, dtype, copy, blocksize])
BSR矩阵有三个参数:
- inptr列表的第i个元素与i+1个元素是储存第i行的数据的列索引以及数据的区间索引
- 即indices[indptr[i]:indptr[i+1]]【左闭右开】为第i行元素的列索引
- data[indptr[i]: indptr[i+1]]【左闭右开】为第i行元素的data。
from scipy.sparse import *
indptr = np.array([0, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6]).repeat(4).reshape(6, 2, 2)data
'''
array([[[1, 1],[1, 1]],[[2, 2],[2, 2]],[[3, 3],[3, 3]],[[4, 4],[4, 4]],[[5, 5],[5, 5]],[[6, 6],[6, 6]]])
'''bsr_matrix((data,indices,indptr), shape=(6, 6)).toarray()
'''
array([[1, 1, 0, 0, 2, 2],[1, 1, 0, 0, 2, 2],[0, 0, 0, 0, 3, 3],[0, 0, 0, 0, 3, 3],[4, 4, 5, 5, 6, 6],[4, 4, 5, 5, 6, 6]])
'''
我们逐行分析
对于第0行,indptr[0]:indptr[1] -> 0:2,即[0,2)。因此第0行的列为indice[0:2]=[0,2],
data为:,对应的就是最后结果的第0,1行。
对于第1行,indptr[1]:indptr[2] -> 2:3,即[2,3)。因此第0行的列为indice[2:3]=[2],
data为:,对应的就是最后结果的第2,3行。
对于第2行,indptr[2]:indptr[3] -> 3:6,即[3,6)。因此第2行的列为indice[3:6]=[0,1,2],
data为:,对应的就是最后结果的第4,5行。
2.2 coo矩阵
coo_matrix(arg1[, shape, dtype, copy])
coo_matrix(arg1[, shape, dtype, copy])
coo_matrix是可以根据行和列索引进行data值的累加。
from scipy.sparse import *
row = np.array([0, 0, 1, 3, 1, 0, 0])
col = np.array([0, 2, 1, 3, 1, 0, 0])
data = np.array([1, 1, 1, 1, 1, 1, 2])
coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
'''
array([[4, 0, 1, 0],[0, 2, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1]])
'''
2.3 csc矩阵 & csr矩阵
Compressed Sparse Column & Row matrix
并没有看出来和前面的coo有什么区别。。。希望评论区批评指针~
from scipy.sparse import *
row = np.array([0, 0, 1, 3, 1, 0, 0])
col = np.array([0, 2, 1, 3, 1, 0, 0])
data = np.array([1, 1, 1, 1, 1, 1, 2])
csc_matrix((data, (row, col)), shape=(4, 4)).toarray()
'''
array([[4, 0, 1, 0],[0, 2, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1]], dtype=int32)
'''
from scipy.sparse import *
row = np.array([0, 0, 1, 3, 1, 0, 0])
col = np.array([0, 2, 1, 3, 1, 0, 0])
data = np.array([1, 1, 1, 1, 1, 1, 2])
csr_matrix((data, (row, col)), shape=(4, 4)).toarray()
'''
array([[4, 0, 1, 0],[0, 2, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1]], dtype=int32)
'''
2.4 dia矩阵
dia_matrix(arg1[, shape, dtype, copy])
from scipy.sparse import *data = np.array([[1, 2, 3, 4],[1,3,5,7],[2,4,5,7]])offsets = np.array([0, -1, 2])dia_matrix((data, offsets), shape=(4, 4)).toarray()
'''
array([[1, 0, 5, 0],[1, 2, 0, 7],[0, 3, 3, 0],[0, 0, 5, 4]])
'''
data定义对角线元素
offsets定义对角线的偏移量,0代表正对角线,正数代表往上偏移,负数代表往下偏移
一上图为例:最终的结果是:data[0]就是正对角线、data[1]向下偏移一格,data[2]向上偏移两格
2.5 dok矩阵
Dictionary Of Keys based sparse matrix
dok_matrix可以高效地逐渐构造稀疏矩阵。
from scipy.sparse import *
S = dok_matrix((5, 5), dtype=np.float32)
for i in range(5):for j in range(5):S[i, j] = i + j
S.toarray()
'''
array([[0., 1., 2., 3., 4.],[1., 2., 3., 4., 5.],[2., 3., 4., 5., 6.],[3., 4., 5., 6., 7.],[4., 5., 6., 7., 8.]], dtype=float32)
'''
这个一目了然了
3 基本初始化方法
3.1 sparse_matrix((data, (row, col)), shape=(4, 4)).toarray()
除了dok和dia,其他的都适用
row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
print(bsr_matrix((data, (row, col)), shape=(4, 4)).toarray())
print(coo_matrix((data, (row, col)), shape=(4, 4)).toarray())
print(csc_matrix((data, (row, col)), shape=(4, 4)).toarray())
print(csr_matrix((data, (row, col)), shape=(4, 4)).toarray())
#print(dia_matrix((data, (row, col)), shape=(4, 4)).toarray())
#print(dok_matrix((data, (row, col)), shape=(4, 4)).toarray())
'''
[[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]]
'''
3.2 sparse_matrix(array).toarray()
array可以是list,也可以是np.array
适用于所有类型的矩阵
row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
print(bsr_matrix(array).toarray())
print(coo_matrix(array).toarray())
print(csc_matrix(array).toarray())
print(csr_matrix(array).toarray())
print(dia_matrix(array).toarray())
print(dok_matrix(array).toarray())
'''
[[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]]
'''
4 判别函数
issparse(x) isspmatrix(x) |
x是否为sparse类型 |
isspmatrix_csc(x) | x是否为csc_matrix类型 |
isspmatrix_csr(x) | x是否为csr_matrix类型 |
isspmatrix_bsr(x) | x是否为bsr_matrix类型 |
isspmatrix_lil(x) | x是否为lil_matrix类型 |
isspmatrix_dok(x) | x是否为dok_matrix类型 |
isspmatrix_coo(x) | x是否为coo_matrix类型 |
isspmatrix_dia(x) | x是否为dia_matrix类型 |
5 文件操作
save_npz(file, matrix[, compressed]) | 以.npz格式保存稀疏矩阵 |
load_npz(file) | 导入.npz格式的稀疏矩阵 |
6 转化函数
todense([order, out]) | 返回稀疏矩阵的np.matrix形式 |
toarray([order, out]) | 返回稀疏矩阵的np.array形式 |
tobsr([blocksize, copy]) | 返回稀疏矩阵的bsr_matrix形式 |
tocoo([copy]) | 返回稀疏矩阵的coo_matrix形式 |
tocsc([copy]) | 返回稀疏矩阵的csc_matrix形式 |
tocsr([copy]) | 返回稀疏矩阵的csr_matrix形式 |
todia([copy]) | 返回稀疏矩阵的dia_matrix形式 |
todok([copy]) | 返回稀疏矩阵的dok_matrix形式 |
7 其他函数(待补充)
find(A) |
返回稀疏矩阵A中的非零元的位置以及数值 |
scipy笔记:scipy.sparse相关推荐
- scipy笔记—scipy.misc.imresize用法(方便训练图像数据)
scipy.misc.imresize 不同于普通的reshape, imresize不是单纯的改变图像矩阵的维度,而是能将图片重采样为指定像素,这样给深度学习中训练图像数据带来方便. import ...
- scipy笔记:wishart (威沙特分布)
威沙特分布理论部分:概率统计笔记:威沙特分布(Wishart Distribution)_UQI-LIUWJ的博客-CSDN博客 1 使用方法 scipy.stats.wishart(df=None, ...
- scipy 笔记:solve_triangular
1 基本用法 scipy.linalg.solve_triangular(a, b, trans=0, lower=False, unit_diagonal=False, overwrite_b=Fa ...
- python《计算机视觉编程》第一章笔记——1.4.3形态学,1.4.2有用的scipy模块scipy.os模块,scipy.misc模块。1.5ROF去噪模型
1.4.3形态学:对象计数 形态学(数学形态学):度量和分析基本形状的图像处理方法的基本框架和集合.形态学通常用来处理二值图像,也能用于灰度图像.二值图像是指图像的每个像素只能取0或1. scipy. ...
- 【scipy】 scipy.ndimage 数学形态学(Mathematical morphology)
____tz_zs笔记 形态学 形态学(morphology)一词通常表示生物学的一个分支,该分支主要研究动植物的形态和结构.而我们图像处理中指的形态学,往往表示的是数学形态学. 数学形态学(Math ...
- python scipy stats_Python Scipy stats.binned_statistic_dd()用法及代码示例
stats.binned_statistic_dd(arr, values, statistic='mean', bins=10, range=None)函数为给定的二维数据计算合并的统计值. 它的工 ...
- Scipy.integrate(scipy积分部分中文文档翻译,进度70%)
文章目录 积分(scipy.integrate) 通用积分(quad) 一般多重积分(dblquad,tplquad,nquad) 高斯正交 Romberg积分 使用采样数据进行积分 使用低级回调函数 ...
- Scipy优化scipy.optimize.minimize
如果喜欢请点赞,收藏,谢谢! 要解决一个优化问题 m i n i m i z e x [ 0 ] , x [ 1 ] \mathrm{minimize}_{x[0],x[1]} minimizex[0 ...
- 【论文笔记】Sparse filtering
论文来源:NIPS2011 相比于其它的无监督学习算法,Sparse filtering并不是去学习数据的分布,它是去分析特征的分布,良好的特征分布满足一定的特性,有了这个思想,只需要优化简单的目标函 ...
最新文章
- 5、kubernetes 集群 YAML 文件详解
- 神策数据丨教育行业线索转化全链路解决方案
- 抽象方法vs虚方法 c# 1613719040
- 【LeetCode题解】排序
- Cocos2d-x动画播放(序列帧)
- 流量计算机组成keypad,曾经的我
- TortoiseSVN 官网 中文语言包位置
- uniapp开发的多端APP源码
- Python实现批量采集美女shipin<无水印>
- 使用Requests爬取网页图片并保存
- 【JavaWeb】如何优雅的实现第三方开放api接口签名(有状态/无状态)
- 写给程序员的心理学入门知识(一)
- 关于硬盘分区(主分区、扩展分区和逻辑分区)
- Excel按照单元格内设定好的次序进行工作表排序
- 解决 k8s flannel网络 一直 Init:ImagePullBackOff和coredns状态为Pending
- twitter APi的使用与twitter数据的应用
- R 数字 字符 向量
- Echarts——中国地图绘制
- Neat Download Manager Mac(多线程下载管理器)
- Java代码点和代码单元及其区别
热门文章
- 练习angularjs的ng-click的应用
- 移动相关HTML设置
- 将图片保存到系统相冊的两种方法
- 使用自定义的按钮替换默认的input type='file'
- Android之父卸任意味着什么?
- CSP认证201604-1	折点计数[C++题解]:枚举、遍历
- 算法提高课-数学知识-矩阵乘法-AcWing 1303. 斐波那契前 n 项和:矩阵乘法,快速幂,线性代数
- JPEG压缩matlab实现
- java servlet 返回图片_SpringMVC返回图片的几种方式
- java cpu 内存_如何检查Java中的CPU和内存使用情况?