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 之 装饰器的写法
普通装饰器 1. 不带参数的普通装饰器 from functools import wraps def use_logging(func): @wraps(func) # 使得装饰器函数和原函数有一样 ...
- Retina Display and Eclipse Mac视网膜屏和Eclipse
I have a Retina Display and Eclipse looks blurry. How can I fix it? You need to tell Mac OS that Ecl ...
- (Collection)347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [转]Linux软连接和硬链接
1.Linux链接概念 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节 ...
- DVD管理器集合版
利用所学的集合写出的DVD管理系统,运用到了所学到集合基础. import java.text.ParseException; import java.text.SimpleDateFormat; i ...
- 关于ORACLE中配置文件的问题
关于windows服务和oracle的问题的解决 <?xml version="1.0" encoding="utf-8"?><configu ...
- js截取url的参数(转自。。)
用JS获取地址栏参数的方法(超级简单) 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg ...
- vi/vim 的使用
vi/vim 的使用 基本上 vi/vim 共分为三种模式,分别是一般模式.编辑模式与指令列命令模式. 这三种模式的作用分别是: 一般模式:以 vi 打开一个档案就直接进入一般模式了(这是默认的模式) ...
- AngularJS-chapter1-2-四大特性
4大特性 MVC MVC实例 数据模型,控制器,视图 HelloAngular_MVC.html 图中的 ng-controller="HelloAngular" 定义了Hel ...
- 使用 Wireshark 调试 HTTP/2 流量
https://imququ.com/post/http2-traffic-in-wireshark.html