本文以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 datarow_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矩阵使用方法的更多相关文章

  1. Scipy.sparse矩阵的存储,读取和转化为稠密矩阵

    import numpy as np import scipy.sparse as sp m = sp.lil_matrix((7329,7329)) np.save(path,m) #用numpy的 ...

  2. Python SciPy Sparse模块学习笔记

    1. sparse模块的官方document地址:http://docs.scipy.org/doc/scipy/reference/sparse.html   2. sparse matrix的存储 ...

  3. 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 ...

  4. python 稀疏向量和矩阵的表示形式

    http://blog.csdn.net/nkwangjie/article/details/17502443 http://blog.csdn.net/bitcarmanlee/article/de ...

  5. python中的矩阵、多维数组----numpy

    https://docs.scipy.org/doc/numpy-dev/user/quickstart.html  (numpy官网一些教程) numpy教程:数组创建 python中的矩阵.多维数 ...

  6. scipy.sparse的一些整理

    一.scipy.sparse中七种稀疏矩阵类型 1.bsr_matrix:分块压缩稀疏行格式 介绍 BSR矩阵中的inptr列表的第i个元素与i+1个元素是储存第i行的数据的列索引以及数据的区间索引, ...

  7. [转]Python中的矩阵转置

    Python中的矩阵转置 via 需求: 你需要转置一个二维数组,将行列互换. 讨论: 你需要确保该数组的行列数都是相同的.比如: arr = [[1, 2, 3], [4, 5, 6], [7, 8 ...

  8. python数组和矩阵使用总结

    python数组和矩阵使用总结 1.数组和矩阵常见用法 Python使用NumPy包完成了对N-维数组的快速便捷操作.使用这个包,需要导入numpy. SciPy包以NumPy包为基础,大大的扩展了n ...

  9. python小白之矩阵matrix笔记(updating)

    Matrix #python学习之矩阵matrix 2018.4.18 # -*- coding: UTF-8 -*- from numpy import * import numpy as np i ...

随机推荐

  1. Android 常用控件的介绍

    http://www.cnblogs.com/linjiqin/category/284058.html 最流行的android组件大全:http://www.cnblogs.com/linjiqin ...

  2. Shift的用法

    Shell编程中Shift的用法     位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的 ...

  3. HEVC学习之二CTU, CU, CTB, CB, PB, TB

    在H264标准中,编码层的核心是宏块,一个宏块大小为16X16,包含一个16X16的亮度块,以及对于常用的4:2:0采样格式来说还包含两个8X8的色度块.相对应的在HEVC中类似的结构为编码树单元(C ...

  4. 记录Castle ActiveRecord访问Sqlite的配置

    1.ActiveRecord配置文件ARConfig.xml,并将配置文件的“生成操作”改成“嵌入的资源” <?xml version=; i < ; i++)             { ...

  5. 一起学习KenDo

    这几年用Telerik做WEB,积累了一些感觉.因为打算涉足移动APP开发,打算接下来学习KenDo.

  6. python download

    今天下载 python3 , 从官网下 速度 平均 十几K,网上 搜了下.提供的下载地址 几乎都是 官网的. 于是 下了个 百度同步盘,做 公开分享. 提供给 大家下载,速度 有 300 多K,提高了 ...

  7. ExpressJs server中Router的设置

    expressjs的路由设置方法 一.基本方法: app.METHOD(PATH, HANDLER)把路径path和操作方法method(可以是http的get/put/delete等),映射到一个处 ...

  8. 软件测试之loadrunner学习笔记-01事务

    loadrunner学习笔记-01事务<转载至网络> 事务又称为Transaction,事务是一个点为了衡量某个action的性能,需要在开始和结束位置插入一个范围,定义这样一个事务. 作 ...

  9. ATL中窗口句柄与窗口过程的关联方法

    ATL中采用了一种动态生成机器指令的方式进行窗口句柄与窗口对象进行关联,以是详细分析: CWindowImpl会在第一次调用Create时注册窗口类,该窗口类是的信息是在CWindowImpl的子类中 ...

  10. 使用Spring Data JPA查询时,报result returns more than one elements异常

    public static <T> T get(String hql, Class<T> t) { EntityManager em = getFactory().create ...