Python scipy.sparse矩阵使用方法
本文以csr_matrix为例来说明sparse矩阵的使用方法,其他类型的sparse矩阵可以参考https://docs.scipy.org/doc/scipy/reference/sparse.html
csr_matrix是Compressed Sparse Row matrix的缩写组合,下面介绍其两种初始化方法
csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
where data, row_ind and col_ind satisfy the relationship a[row_ind[k], col_ind[k]] = data[k].
csr_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSR representation where the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred from the index arrays.
上述官方文档给出了:稀疏矩阵的参数及其含义、稀疏矩阵的构造方式。阐述形式简单明了,读起来令人赏心悦目。
Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power
Advantages of the CSR format
- efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
- efficient row slicing
- fast matrix vector products
Disadvantages of the CSR format
- slow column slicing operations (consider CSC)
- changes to the sparsity structure are expensive (consider LIL or DOK)
上述官方文档时稀疏矩阵的一些特性以及csr_matrix的优缺点,并且在指明各种缺点的同时,提供了可以考虑的技术实现。
代码示例1
import numpy as np
from scipy.sparse import csr_matrix row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
a = csr_matrix((data, (row, col)), shape=(3, 3)).toarray() print(a)
运行结果:
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
代码示例2
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])
a = csr_matrix((data, indices, indptr), shape=(3, 3)).toarray() print(a)
允许结果:
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
上述两个代码示例也是摘自官方文档,表明了每种初始化方式的简单实现,给应用这种初始化方式的人很大启发。
总结:官方文档其实是很好的书写程序文档范例,欣赏她,模仿她,然后在实际中应用她...
Python scipy.sparse矩阵使用方法的更多相关文章
- Scipy.sparse矩阵的存储,读取和转化为稠密矩阵
import numpy as np import scipy.sparse as sp m = sp.lil_matrix((7329,7329)) np.save(path,m) #用numpy的 ...
- Python SciPy Sparse模块学习笔记
1. sparse模块的官方document地址:http://docs.scipy.org/doc/scipy/reference/sparse.html 2. sparse matrix的存储 ...
- python稀疏矩阵得到每列最大k项的值,对list内为类对象的排序(scipy.sparse.csr.csr_matrix)
print(train_set.tdm) print(type(train_set.tdm)) 输出得到: (0, 3200) 0.264940780338 (0, 1682) 0.356545827 ...
- python 稀疏向量和矩阵的表示形式
http://blog.csdn.net/nkwangjie/article/details/17502443 http://blog.csdn.net/bitcarmanlee/article/de ...
- python中的矩阵、多维数组----numpy
https://docs.scipy.org/doc/numpy-dev/user/quickstart.html (numpy官网一些教程) numpy教程:数组创建 python中的矩阵.多维数 ...
- scipy.sparse的一些整理
一.scipy.sparse中七种稀疏矩阵类型 1.bsr_matrix:分块压缩稀疏行格式 介绍 BSR矩阵中的inptr列表的第i个元素与i+1个元素是储存第i行的数据的列索引以及数据的区间索引, ...
- [转]Python中的矩阵转置
Python中的矩阵转置 via 需求: 你需要转置一个二维数组,将行列互换. 讨论: 你需要确保该数组的行列数都是相同的.比如: arr = [[1, 2, 3], [4, 5, 6], [7, 8 ...
- python数组和矩阵使用总结
python数组和矩阵使用总结 1.数组和矩阵常见用法 Python使用NumPy包完成了对N-维数组的快速便捷操作.使用这个包,需要导入numpy. SciPy包以NumPy包为基础,大大的扩展了n ...
- python小白之矩阵matrix笔记(updating)
Matrix #python学习之矩阵matrix 2018.4.18 # -*- coding: UTF-8 -*- from numpy import * import numpy as np i ...
随机推荐
- Python 之 for循环中的lambda
第一种 f = [lambda x: x*i for i in range(4)] (如果将x换成i,调用时候就不用传参数,结果都为3) 对于上面的表达式,调用结果: >>> f ...
- [DFNews] EnCase v7.08发布
EnCase v7.08 近日正式发布,7.08增加了Evidence Processor Manager以及Evidence Processor,不仅可以在本地实现证据处理队列,也支持了通过网络进行 ...
- StackPanel在增加控件的问题
今天遇到这样一个问题,就是我做了一个自定义控件.然后加到StackPanel中, <StackPanel Height="676" HorizontalAlignment=& ...
- ant 介绍 http://blog.csdn.net/sunjavaduke/archive/2007/03/08/1523819.aspx
转自: 本内容包含了Ant的历史简要介绍,Ant的功能以及Ant框架的介绍,并对下载安装使用Ant进行了示例介绍,同时通过一个Java程序讲解了Ant的基本使用方法. 1. Ant简介:这 ...
- ssh整合需要那些jar
struts2 commons-logging-1.0.4.jar -------主要用于日志处理 freemarker-2.3.8.jar ------- 模板相关操作需要包 ognl-2.6.1 ...
- IntelliJ IDEA - 代码辅助功能
Eclipse 和 IntelliJ IDEA 都提供了写代码的辅助功能,包括代码补全.代码生成.快速修饰和动态模板等功能. 1. 快速修复(Quick-fixes) 快捷键:Alt+Enter 所有 ...
- 初学android之简易的网易页面
WangYiActivity.java: public class WangYiActivity extends Activity implements OnClickListener{ public ...
- Android自动化学习笔记之MonkeyRunner:官方介绍和简单实例
---------------------------------------------------------------------------------------------------- ...
- oracle计算是否是同一周
函数已经解决跨年问题 select to_char(date'2016-12-31','iW') from dual; select to_char(date'2017-01-01','iW') fr ...
- Java集合框架(常用类) JCF
Java集合框架(常用类) JCF 为了实现某一目的或功能而预先设计好一系列封装好的具有继承关系或实现关系类的接口: 集合的由来: 特点:元素类型可以不同,集合长度可变,空间不固定: 管理集合类和接口 ...