本文以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. asp.net webform 中使用Microsoft ASP.NET Web Optimization压缩js及css

    使用静态资源压缩可以合并静态资源文件减少客户端请求数量,压缩文件大小,减少网络流量的损耗. 注:只有通过web.config关闭调试功能,压缩才会生效 <system.web> <c ...

  2. RequireJS 加载 easyui

    requireJS 可以让js加载起来比较优雅,像java里import一样.有了这个,我们可以创建自己的 js控件库,在需要时,页面中只引入 requireJS,然后通过代码方式引入需要用到的控件, ...

  3. 如何在tpl模版的div块中加ztree

    ld-ztree.tpl <div class="ld-ztree-container"> <div class="ld-ztree-header te ...

  4. 【Python】supervisor安装和管理celery

    参考:http://blog.csdn.net/wawa8899/article/details/52743861 参考:http://www.cnblogs.com/mountaingeek/p/5 ...

  5. MongoDB学习笔记(入门)

    一.文档的注意事项:1.  键值对是有序的,如:{ "name" : "stephen", "genda" : "male&quo ...

  6. 利用JAVA生成二维码

    本文章整理于慕课网的学习视频<JAVA生成二维码>,如果想看视频内容请移步慕课网. 维基百科上对于二维码的解释. 二维条码是指在一维条码的基础上扩展出另一维具有可读性的条码,使用黑白矩形图 ...

  7. 18.虚拟机linux上网问题

    1.1.VMware中虚拟机网络的三种设置第一种:桥接(bridged)第二种:NAT第三种:Host only .该模式下仅主机可以上网,虚拟机不能上网. 1.2.虚拟机上网方式1:NAT方式设置步 ...

  8. 用jquery解析JSON数据的方法以及字符串转换成json的3种方法

    用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是 json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject ...

  9. python 实现 斐波那契数列

    #!usr/bin/python#_*_coding=utf-8_*_ def fin(n): li=[0,1] for i in range(2,n): li.append(li[-1]+li[-2 ...

  10. Mysql技术内幕(第四版)读书笔记(一)

    题记:写代码已经有2年了,学到了很多知识,但是没有一个好习惯去记录,去分享,好多知识点都会忘记,所以从今天开始学着像大牛一样去记录自己经历项目的点点滴滴,先从最近读<Mysql技术内幕>开 ...