3. SciPy创建稀疏矩阵

严格意义上讲ndarray数据类型应属数组而非矩阵,而matrix才是矩阵,这个在NumPy创建matrix一章里有讲述,是最基本的矩阵matrix创建方法,忘记了可以回头看看。

本章利用scipy.sparse模块下的类提供创建稀疏矩阵的方法,例如bsr_matrix稀疏矩阵类。

什么是稀疏矩阵?按数据结构领域知名学者严老师的定义稀疏矩阵是一个矩阵里有小于5%非0数据的矩阵可以视为稀疏矩阵,如果矩阵的总数据量较大,完成存储这个矩阵会有大量的0存储,浪费空间,所以对稀疏矩阵的存储有必要研究用少量的内存存储稀疏矩阵,在数据结构里有时用三元组来解决。

3.1 coo_matrix类创建稀疏矩阵

接下来我们看看在SciPy里如何解决对稀疏矩阵的存储?效率如何?

三元组,即ijv,记录稀疏矩阵里的非零数据的行i、列j坐标以及值v三个数据。下面按三元组的方式来创建稀疏矩阵。

#coding:utf-8

import numpy as np

import scipy.sparse as ss

import random

# 随机产生行、列坐标和值

a = random.sample(range(0, 9), 5)

b = random.sample(range(0, 9), 5)

c = random.sample(range(1, 100), 5)

# 将list数据转为array数组

rows = np.array(a)

print rows,"#rows"

cols = np.array(b)

print cols, "#cols"

v = np.array(c)

print v,"#values"

# coo_matrix函数生成稀疏矩阵

sparseM = ss.coo_matrix((v,(rows,cols)))

print sparseM, "#sparseM,", "shape is ", sparseM.shape

# todense将稀疏矩阵转为完全阵

fullM = sparseM.todense()

print fullM, "#fullM,", "shape is ", fullM.shape

程序执行结果:

[8 0 6 7 4] #rows

[3 8 1 2 7] #cols

[ 1 48 99 62 94] #values

(8, 3) 1

(0, 8) 48

(6, 1) 99

(7, 2) 62

(4, 7) 94 #sparseM, shape is (9, 9)

[[ 0 0 0 0 0 0 0 0 48]

[ 0 0 0 0 0 0 0 0 0]

[ 0 0 0 0 0 0 0 0 0]

[ 0 0 0 0 0 0 0 0 0]

[ 0 0 0 0 0 0 0 94 0]

[ 0 0 0 0 0 0 0 0 0]

[ 0 99 0 0 0 0 0 0 0]

[ 0 0 62 0 0 0 0 0 0]

[ 0 0 0 1 0 0 0 0 0]] #fullM, shape is (9, 9)

需主要sparseM和fullM每次可能都不同,因为行、列、值都是随机产生的。

有关coo_matrix稀疏矩阵的处理方法函数可以参考相应的帮助,示例里给出了一个todense函数的使用方法。

3.2 csc_matrix类创建稀疏矩阵

csc_matrix类提供了很多方法来创建稀疏矩阵。

1). 可以直接调用类的构造函数(参阅"类"一章下的__init__的解析)将一个数组或矩阵转化为稀疏矩阵存储。

import numpy as np

import scipy.sparse as ss

a = np.zeros((3, 4))

a[1, 2] = 12

a[2, 2] = 22

print a

print ss.csc_matrix(a)

程序执行结果:

[[ 0. 0. 0. 0.]

[ 0. 0. 12. 0.]

[ 0. 0. 22. 0.]] # a

(1, 2) 12.0

(2, 2) 22.0 # csc_matrix

2).可以创建一个空的稀疏矩阵,即全0,然后通过索引赋值获得一个非空的稀疏矩阵,但用csc_matrix这样去做时SciPy建议改为lil_matrix更高效,见执行结果的warning信息。

import scipy.sparse as ss

x = ss.csc_matrix((4, 3))

#x = ss.lil_matrix((4, 3))

print "x --"

print x

x[1, 2] = 12

x[3, 1] = 23

print x

print x.todense()

程序执行结果:

x --

/usr/lib/python2.7/dist-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csc_matrix is expensive. lil_matrix is more efficient.

SparseEfficiencyWarning)

(3, 1) 23.0

(1, 2) 12.0

[[ 0. 0. 0.]

[ 0. 0. 12.]

[ 0. 0. 0.]

[ 0. 23. 0.]]

3). 三元组的方法创建稀疏矩阵,和coo_matrix类创建的方式一样指定i、j、v,只需将3.1节的程序里的coo_matrix改为csc_matrix即可。

import numpy as np

import scipy.sparse as ss

import random

a = random.sample(range(0, 9), 5)

b = random.sample(range(0, 9), 5)

c = random.sample(range(1, 100), 5)

rows = np.array(a)

print rows,"#rows"

cols = np.array(b)

print cols, "#cols"

v = np.array(c)

print v,"#values"

sparseM = ss.csc_matrix((v,(rows,cols)))

print sparseM, "#sparseM,", "shape is ", sparseM.shape

fullM = sparseM.todense()

print fullM, "#fullM,", "shape is ", fullM.shape

print sparseM.sum(), sparseM.nonzero()

print sparseM.get_shape()

4). 真正的csc_matrix创建稀疏矩阵

csc_matrix类,实际就是数据结构里按列存储稀疏矩阵时,给出每列里非零个数,以及列里那几行是非零值。

下面是官方文档:

csc_matrix((data, indices, indptr), [shape=(M, N)])

is the standard CSC representation where the row indices for column 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.

官方例子:

import numpy as np

from scipy.sparse import csc_matrix

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])

print csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()

程序执行结果:

array([[1, 0, 4],

[0, 0, 5],

[2, 3, 6]])

以下是解析这个例子:

#第0列

i = 0

# 0列那些行非0?

indices[indptr[i]:indptr[i+1]]

= indices[indptr[0]:indptr[1]]

= indices[0:2]

= [0, 2]

# 0列非零行对应的值

data[indptr[i]:indptr[i+1]]

= data[indptr[0]:indptr[1]]

= data[0:2]

= [1, 2]

i = 1

indices[indptr[i]:indptr[i+1]]

= indices[indptr[1]:indptr[2]]

= indices[1:2]

= [2]

data[indptr[i]:indptr[i+1]]

= data[indptr[1]:indptr[2]]

= data[2:3]

= [3]

# 第2列

i = 2

# 非0行?

indices[indptr[i]:indptr[i+1]]

= indices[indptr[2]:indptr[3]]

= indices[3:6]

= [0, 1, 2]

# 对应的值

data[indptr[i]:indptr[i+1]]

= data[2:3]

= [4,5,6]

总结一下csc_matrix的各个参数含义,data是稀疏矩阵的值;indices给出各列非0数据所在的行号;indptr则是给出前i列非0元素个数:indptr[0]表示第0列前有0个,indptr[1]第1列前共有0列那么多个实际是第0列非0个数,indptr[2]在则是记录了0、1列一共有多少个非0元素。

5). 思考一下如下的稀疏矩阵怎么用csc_matrix构造出来?

[[ 0. 0. 0. 0.]

[ 0. 0. 12. 0.]

[ 0. 0. 0. 22.]]

首先可以写出data = [12, 22],然后给出行坐标indices = [1, 2] ,最后给出前n列的非零数值的个数indptr = [0,0,0,1,2]。

import numpy as np

from scipy.sparse import csc_matrix

indptr = np.array([0,0,0,1,2])

indices = np.array([1, 2])

data = np.array([12, 22])

csc = csc_matrix((data, indices, indptr), shape=(3, 4))

print csc, "#csc"

print csc.todense(), "#csc.todense"

程序执行结果

(1, 2) 12

(2, 3) 22 #csc

[[ 0 0 0 0]

[ 0 0 12 0]

[ 0 0 0 22]] #csc.todense

3.3 csr_matrix类创建稀疏矩阵

csr_matrix类是按行存储矩阵,和csc_matrix按列真好相对应。也有很多函数,就不多解释了。

同样是下面这个矩阵,怎样用csr_matrix实现?

[[ 0. 0. 0. 0.]

[ 0. 0. 12. 0.]

[ 0. 0. 0. 22.]]

首先可以写出data = [12, 22],然后给出列坐标indices = [2, 3] ,最后给出前n行的非0值个数indptr = [0,0,1,2]。

程序如下:

import numpy as np

from scipy.sparse import csr_matrix

indptr = np.array([0,0,1,2])

indices = np.array([2,3])

data = np.array([12, 22])

csr = csr_matrix((data, indices, indptr), shape=(3, 4))

print csr, "#csr"

print csr.todense(), "#csr.todense"

3.4 bsr_matrix类创建稀疏矩阵

在理解了csr_matrix、csc_matrix类之后在看bsr_matrix类就不难了,这里的b是block的意思。csr_matrix、csc_matrix类是用data里的值去在indices和indptr确定的位置上填充一个数据,而bsr_matrix类,则是用一个矩阵x去填充这个位置的数据、0值位置用与x矩阵同型0矩阵填充,所以整个稀疏矩阵会扩大。

import numpy as np

from scipy.sparse import bsr_matrix

indptr = np.array([0,0,1,2])

indices = np.array([2,3])

data = np.array([12, 22]).repeat(6).reshape(2, 2, 3)

bsr = bsr_matrix((data, indices, indptr), shape=(6, 12))

print bsr, "#bsr"

print bsr.todense(), "#bsr.todense"

程序执行结果如下:

(2, 6) 12

(2, 7) 12

(2, 8) 12

(3, 6) 12

(3, 7) 12

(3, 8) 12

(4, 9) 22

(4, 10) 22

(4, 11) 22

(5, 9) 22

(5, 10) 22

(5, 11) 22 #bsr

[[ 0 0 0 0 0 0 0 0 0 0 0 0]

[ 0 0 0 0 0 0 0 0 0 0 0 0]

[ 0 0 0 0 0 0 12 12 12 0 0 0]

[ 0 0 0 0 0 0 12 12 12 0 0 0]

[ 0 0 0 0 0 0 0 0 0 22 22 22]

[ 0 0 0 0 0 0 0 0 0 22 22 22]] #bsr.todense

3.5 其他稀疏矩类

scipy.sparse里还有dia_matrix 、dok_matrix比较简单,在这里就不再继续展示了。

python建立空矩阵_SciPy创建稀疏矩阵相关推荐

  1. python建立空矩阵_创建空矩阵Python

    首先,您应该在最里面的列表中插入一些内容(比如None).其次,当您在最外层列表中使用乘法时,它会将引用复制到内部列表,因此当您更改一个元素时,您也会在所有其他列表中更改此元素:>> pa ...

  2. python建立空集合_「python」集合类型及操作

    目录: 集合类型定义 集合操作符 集合处理方法 集合类型应用场景 1 集合类型定义 集合是多个元素的无序组合 集合用大括号 {} 表示,元素间用逗号分隔 建立集合类型用 {} 或 set() 建立空集 ...

  3. python怎么定义空矩阵_Python创建一个空的稀疏矩阵

    我试图将一些真实数据解析为一个.mat对象,以便在我的 matlab脚本中加载. 我收到此错误: TypeError: 'coo_matrix' object does not support ite ...

  4. Python列表实现矩阵的创建、输入输出、转化转置、加减乘运算并设计一个矩阵计算器GUI界面

    背景:在解决一些编程问题中如棋盘的初始化,链表,队列的构建:数据处理中如用SAS软件输入数据等涉及到矩阵的概念,而用编程语言实现矩阵的方式有C中的数组,python中的列表等.现在给你一个数据如下,或 ...

  5. python可以处理矩阵吗_Python 稀疏矩阵处理

    一.Scipy 1.COO.CSC和CSRcoo_matrix(arg1[, shape, dtype, copy])A sparse matrix in COOrdinate format. csc ...

  6. python建立文件数据库_Python创建CRNN训练用的LMDB数据库文件

    CRNN简介 CRNN由 Baoguang Shi, Xiang Bai, Cong Yao提出,2015年7月发表论文:"An End-to-End Trainable Neural Ne ...

  7. python建立虚拟环境付款_python 创建虚拟环境(virtualenv)

    virtualenv 安装 1.Install pip and Virtualenv by issuing the following commands : $ sudo easy_install p ...

  8. python数组和矩阵用法

    python数组和矩阵 先创建一个一维数组 直接定义一个数组: a = [1,2,3,4,5] b = ['a','c','c','s'] print(a) print(b) 输出结果: 通过键盘输入 ...

  9. python怎么定义空矩阵_python 空矩阵

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 稀疏矩阵不必以标准矩阵形式表示. 有很多方法可以缓解这种标准形式给我们的计算系统 ...

最新文章

  1. 3dmax Vray建筑可视化入门学习教程
  2. 3 css 奖品出现弹出动画_【技术】nuxt中引入wow和animate.css 页面随滚动条出现动画...
  3. 人工智能用哪个语言好 选择Python语言怎么样
  4. kafka消费中的partition与消费者的关系
  5. HIVE的安装配置、mysql的安装、hive创建表、创建分区、修改表等内容、hive beeline使用、HIVE的四种数据导入方式、使用Java代码执行hive的sql命令
  6. WEB API 系列(二) Filter的使用以及执行顺序
  7. 模拟Struts2实现
  8. python-函数入门(二)
  9. 解决Adobe PhotoShop用户界面字体过小的问题
  10. 训练集和测试集的划分
  11. python 中in
  12. 【码农学编曲】吉他伴奏
  13. 免费在线文档转换工具,一招告诉你超简单.
  14. 数据报表类(BI)项目测试应该如何去啃?
  15. win10无法安装完成若要在此计算机上,windows10无法完成安装怎么解决_win10提示windows无法完成安装的解决教程...
  16. 全球与中国导热凝胶市场竞争格局深度分析与运营投资研究报告2021年版
  17. 基于FPGA的八位数字抢答器
  18. 聊一聊智能汽车和物联网IoT设备的OTA远程升级
  19. 面试时计算机专业兴趣爱好,单招面试时常见问题答疑
  20. 计算机毕业设计java+jsp的网上订餐外卖系统

热门文章

  1. Centos 中 TCPWrappers访问控制
  2. 高并发或高负载下的系统设计
  3. tensorflow--logistic regression
  4. iis+nginx实现负载均衡
  5. jquery瀑布流布局和鼠标滚动加载
  6. 运行管理员线程和用户线程小练习
  7. twitter bbs
  8. C++中调用DLL中的函数的两种方式
  9. 沃尔沃投资两家以色列科技创企 布局人工智能
  10. QQ使用了什么通讯协议?为什么要这样做?为什么采用 UDP 协议,而不采用 TCP 协议实现?