概述

在用python进行科学运算时,常常需要把一个稀疏的np.array压缩,这时候就用到scipy库中的sparse.csr_matrix(csr:Compressed Sparse Row marix) 和sparse.csc_matric(csc:Compressed Sparse Column marix)

scipy.sparse.csr_matrix

官方API介绍(省略前几种容易理解的了)
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.

#  示例解读
>>> 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])
>>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 2],[0, 0, 3],[4, 5, 6]])
# 按row行来压缩
# 对于第i行,非0数据列是indices[indptr[i]:indptr[i+1]] 数据是data[indptr[i]:indptr[i+1]]
# 在本例中
# 第0行,有非0的数据列是indices[indptr[0]:indptr[1]] = indices[0:2] = [0,2]
# 数据是data[indptr[0]:indptr[1]] = data[0:2] = [1,2],所以在第0行第0列是1,第2列是2
# 第1行,有非0的数据列是indices[indptr[1]:indptr[2]] = indices[2:3] = [2]
# 数据是data[indptr[1]:indptr[2] = data[2:3] = [3],所以在第1行第2列是3
# 第2行,有非0的数据列是indices[indptr[2]:indptr[3]] = indices[3:6] = [0,1,2]
# 数据是data[indptr[2]:indptr[3]] = data[3:6] = [4,5,6],所以在第2行第0列是4,第1列是5,第2列是6

scipy.sparse.csc_matrix

官方API介绍(省略前几种容易理解的了)
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.

#  示例解读
>>> 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])
>>> csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 4],[0, 0, 5],[2, 3, 6]])
# 按col列来压缩
# 对于第i列,非0数据行是indices[indptr[i]:indptr[i+1]] 数据是data[indptr[i]:indptr[i+1]]
# 在本例中
# 第0列,有非0的数据行是indices[indptr[0]:indptr[1]] = indices[0:2] = [0,2]
# 数据是data[indptr[0]:indptr[1]] = data[0:2] = [1,2],所以在第0列第0行是1,第2行是2
# 第1行,有非0的数据行是indices[indptr[1]:indptr[2]] = indices[2:3] = [2]
# 数据是data[indptr[1]:indptr[2] = data[2:3] = [3],所以在第1列第2行是3
# 第2行,有非0的数据行是indices[indptr[2]:indptr[3]] = indices[3:6] = [0,1,2]
# 数据是data[indptr[2]:indptr[3]] = data[3:6] = [4,5,6],所以在第2列第0行是4,第1行是5,第2行是6

scipy csr_matrix和csc_matrix函数详解相关推荐

  1. scipy csr_matrix和csc_matrix函数的用法(通俗易懂版)

    概述 scipy.sparse库中的函数 为了将稀疏的np.array数据进行压缩 两者一个是行一个是列,基本思想差不多 csr_matrix >>> indptr = np.arr ...

  2. C语言网络编程:accept函数详解

    文章目录 前言 函数描述 代码实例 如何得到客户端的IP 和 端口号 前言 当使用tcp服务器使用socket创建通信文件描述符,bind绑定了文件描述符,服务器ip和端口号,listen将服务器端的 ...

  3. 【FFmpeg】函数详解(三)

    FFmpeg函数详解 14.av_write_frame 15.av_interleaved_write_frame 16.av_write_trailer 17.avio_close 18.av_i ...

  4. 【FFmpeg】函数详解(二)

    FFmpeg函数详解 9.av_dump_format 10.avio_open 11.avformat_write_header 12.avcodec_send_frame 13.avcodec_r ...

  5. 【FFmpeg】函数详解(一)

    FFmpeg函数详解 一.错误码相关 1.AVERROR 2.av_strerror 3.其他错误码解释 二.编解码 1.获取编解码器 2.申请.释放上下文环境 3.打开编码器avcodec_open ...

  6. 【ES6】Generator函数详解

    [ES6]Generator函数详解 一.Generator函数简介 基本概念 函数写法 yield关键字介绍 二.next方法的参数 三.for...of循环 四.关于普通throw()与Gener ...

  7. mysql的聚合函数综合案例_MySQL常用聚合函数详解

    一.AVG AVG(col) 返回指定列的平均值 二.COUNT COUNT(col) 返回指定列中非NULL值的个数 三.MIN/MAX MIN(col):返回指定列的最小值 MAX(col):返回 ...

  8. python平方数迭代器_对python中的高效迭代器函数详解

    python中内置的库中有个itertools,可以满足我们在编程中绝大多数需要迭代的场合,当然也可以自己造轮子,但是有现成的好用的轮子不妨也学习一下,看哪个用的顺手~ 首先还是要先import一下: ...

  9. python基础知识~ 函数详解2

    python~函数详解2  1 生成器函数    定义 如果函数有yield这个关键字,就是生成器函数.生成器函数() 获取的是生成器,不执行函数   须知 yield和return一样,都可以返回数 ...

最新文章

  1. Linux进程通信中IPC对象——IPC_PRIVATE与ftok
  2. 【经验】提高github的下载(克隆)速度
  3. 多线程解决rospy.spin()语句之后,程序不再往下执行问题
  4. nagios 3.2安装详解(一)
  5. JSValidation 配置文件
  6. leetcode-7-整数翻转
  7. 如何阅读一本书~阅读的层次
  8. Vuex原来可以这样上手
  9. mysql - 索引_07
  10. tablediff同步
  11. java 发送邮件_SpringBoot 2.1.5发送验证码邮件
  12. 【网络基础】【TCP/IP】私有IP地址段
  13. SQL Server中的查询跟踪列值
  14. java通讯录管理系统答辩_java版通讯录管理系统
  15. SecureCRT 7.3.4 安装图解----破解图解
  16. 电子病历设计基本资料
  17. 一图学会配置微信云端店员监控收款回调
  18. 51单片机外设篇:LED点阵
  19. 中国计算机的科技成果,中国9大科技成就,每一个都是实力派
  20. DiskPart介绍

热门文章

  1. 计算机应用基础免费,一计算机应用基础
  2. JavaScript - 停止计时器
  3. 信奥赛和少儿编程的区别
  4. node.js如何模拟网页点击?
  5. 【怀旧】 “AutoCAD 经典”界面
  6. enternet.exe
  7. android 动画开源框架,图文简介非常炫酷的Android开源框架之UI框架
  8. WiFi无线网络显示红叉
  9. QPSK Matlab仿真
  10. 整人小代码(原创,纯属娱乐)