稀疏矩阵的存储格式(Sparse Matrix Formats)

2023-11-11

稀疏矩阵的存储格式(Sparse Matrix Storage Formats)

1.Coordinate Format(COO)

这里写链接内容
这种存储方式的主要优点是灵活、简单。仅存储非零元素以及每个非零元素的坐标。
使用3个数组进行存储:values, rows, andcolumn
values: 实数或复数数据,包括矩阵中的非零元素, 顺序任意。

rows: 数据所处的行。

columns: 数据所处的列.
参数:矩阵中非零元素的数量 nnz,3个数组的长度均为nnz.

import numpy as np
from scipy.sparse import coo_matrix
coo_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)

row  = np.array([0, 3, 1, 0])
col  = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
       [0, 7, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 5]])
# example with duplicates
row  = np.array([0, 0, 1, 3, 1, 0, 0])
col  = np.array([0, 2, 1, 3, 1, 0, 0])
data = np.array([1, 1, 1, 1, 1, 1, 1])
coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[3, 0, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 1]])

2.Diagonal Storage Format (DIA,对角线存储格式?不太懂)

这里写图片描述
The Intel MKL diagonal storage format is specified by two arrays:values and distance, and two parameters:ndiag, which is the number of non-empty diagonals(非零对角线), and lval, which is the declared leading dimension in the calling (sub)programs.

from scipy.sparse import dia_matrix
dia_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)

data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
offsets = np.array([0, -1, 2])
dia_matrix((data, offsets), shape=(4, 4)).toarray()
#where the data[k,:] stores the diagonal entries for diagonal offsets[k]
array([[1, 0, 3, 0],
       [1, 2, 0, 4],
       [0, 2, 3, 0],
       [0, 0, 3, 4]])

3.Compressed Sparse Row Format (CSR)

这里写图片描述

from scipy.sparse import csr_matrix
csr_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)

 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])
 csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])

 indptr = np.array([0, 2, 3, 6])# 为什么要有4个元素??每个元素代表一行,其数值代表该行从indices中哪个元素开始.indices是列号
 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]])
As an example of how to construct a CSR matrix incrementally, the following snippet builds a term-document matrix from texts:


docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]]
 indptr = [0]
 indices = []
 data = []
 vocabulary = {}
 for d in docs:
     for term in d:
         index = vocabulary.setdefault(term, len(vocabulary))
         indices.append(index)
         data.append(1)
    indptr.append(len(indices))

csr_matrix((data, indices, indptr), dtype=int).toarray()
array([[2, 1, 0, 0],
       [0, 1, 1, 1]])

4.Compressed Sparse Column Format (CSC)

和CSR差不多。
The compressed sparse column format (CSC) is similar to the CSR format, but the columns are used instead the rows. In other words, the CSC format is identical to the CSR format for the transposed matrix. The CSR format is specified by four arrays: values, columns, pointerB, and pointerE. The following table describes the arrays in terms of the values, row, and column positions of the non-zero elements in a sparse matrixA.
values
A real or complex array that contains the non-zero elements ofA. Values of the non-zero elements ofA are mapped into thevalues array using the column-major storage mapping.
rows
Element i of the integer array rows is the number of the row inA that contains thei-th value in thevalues array.
pointerB
Element j of this integer array gives the index of the element in thevalues array that is first non-zero element in a columnj ofA. Note that this index is equal topointerB(j) -pointerB(1)+1 .
pointerE
An integer array that contains column indices, such thatpointerE(j)-pointerB(1) is the index of the element in thevalues array that is last non-zero element in a column j ofA.

5. Skyline Storage Format

6. Block Compressed Sparse Row Format (BSR)

from scipy.sparse import bsr_matrix
bsr_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)

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])
bsr_matrix((data, (row, col)), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])

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]).repeat(4).reshape(6, 2, 2)
bsr_matrix((data,indices,indptr), shape=(6, 6)).toarray()
array([[1, 1, 0, 0, 2, 2],
       [1, 1, 0, 0, 2, 2],
       [0, 0, 0, 0, 3, 3],
       [0, 0, 0, 0, 3, 3],
       [4, 4, 5, 5, 6, 6],
       [4, 4, 5, 5, 6, 6]])

bsr_matrix((data, indices, indptr), [shape=(M, N)])
is the standard BSR representation where the block column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding block 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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

稀疏矩阵的存储格式(Sparse Matrix Formats) 的相关文章

  • 如何获取 Pandas df.merge() 不匹配的列名称

    给出以下数据 data df pd DataFrame Reference A A A B C C D E Value1 U U U V W W X Y Value2 u u u v w w x y index 1 2 3 4 5 6 7
  • Django 1.6:清除一张表中的数据

    我有一个名为 UGC 的表 想要清除该表中的所有数据 我不想重置整个应用程序 这也会删除所有其他模型中的所有数据 是否可以只清除一个模型 我还为我的应用程序配置了 South 如果这有帮助的话 你可以使用原始 SQL https docs
  • 查找数据集中的异常值

    我有一个 python 脚本 它创建服务器正常运行时间和性能数据列表的列表 其中每个子列表 或 行 包含特定集群的统计信息 例如 格式良好的它看起来像这样 Cluster Availability Requests Sec Errors S
  • Python:访问另一个类中一个类的属性和方法

    假设我有两个 A 类和 B 类 Class A A s attributes and methods here Class B B s attributes and methods here 现在我可以评估 B 类对象中 A 的属性 如下所
  • 搜索/替换 xml 内容

    我已经成功地使用 xml etree ElementTree 解析 xml 搜索内容 然后将其写入不同的 xml 然而 我只是处理单个标签内的文本 import os sys glob xml etree ElementTree as ET
  • Python TypeError:不支持的操作数类型 -:“int”和“function”

    我是 Python 初学者 正在做一项作业 我不断得到TypeError unsupported operand type s for int and function 即使在研究了错误并应用了建议的修复之后 我并不是在寻找任何人给我一个解
  • 在 PyCharm 中启用终端模拟

    很多人告诉过我和PyCharm 2 7 的 PyCharm 发行说明 https www jetbrains com pycharm whatsnew whatsnew 27 html吹捧那个PyCharm包括完整的终端仿真 我认为这是关于
  • 并行执行按位运算的代码

    我有这段代码 通过将该 AU 矩阵的每个字节 8 个元素打包到 A 中来减少内存消耗 从而使 100k 200k 矩阵占用更少的空间 正如您所期望的 这段代码需要永远运行 我也计划将行数增加到 200k 我正在一个非常强大的实例 CPU 和
  • 映射 2 个数据帧并替换目标数据帧中匹配值的标头

    我有一个数据框 df1 SAP Name SAP Class SAP Sec Avi 5 C Rison 6 A Slesh 7 B San 8 C Sud 7 B df2 Name Fi Class Avi 5 Rison 6 Slesh
  • Python Jinja2 调用宏会导致(不需要的)换行符

    我的 JINJA2 模板如下所示 macro print if john name if name John Hi John endif endmacro Hello World print if john Foo print if joh
  • Plotly:如何在堆叠条形图顶部显示值的总和以及各个条形值?

    我正在尝试在 Python 中的 Plotly Express 中添加每个堆叠条形顶部的总计以及各个条形值 import plotly express as px df px data medals long fig px bar df x
  • 如何为 PyYAML 编写代表程序?

    我想要一个自定义函数来序列化任意 python 对象 就像 json dump 函数有一个名为 default 的可选参数 如果对象不是 json 可序列化的 它应该是 json 转储器将调用的函数 我只是想从 json 包中执行相当于此操
  • python 函数中的对象不可迭代错误

    我有一个简单的功能如下 comdList range 0 27 for t in comdList print t 但是它返回一个 in object not iterable 错误 在函数之外它工作正常 这是怎么回事 尝试这个 for t
  • 折叠 numpy 数组除前两个维度之外的所有维度

    我有一个可变维度的 numpy 数组 例如它可以具有以下形状 64 64 64 64 2 5 64 64 40 64 64 10 20 4 我想要做的是 如果维数大于 3 我想将其他所有内容折叠 堆叠到第三维中 同时保留顺序 因此 在我上面
  • Python for 循环前瞻

    我有一个 python for 循环 其中我需要向前查看一项以查看在处理之前是否需要执行某项操作 for line in file if the start of the next line 0 perform pre processing
  • 如何使用 opencv python 根据检测到的物体的位置生成其热图

    我需要根据对象的位置生成其热图 示例 视频帧中检测到的绿色球 如果它长时间停留在某个位置 那么该位置应该是红色的 并且球在短时间内经过的帧中的位置必须是蓝色的 这样我就需要生成热图 提前致谢 那么你在这里可以做的是 1 首先定义一个热图作为
  • ValueError:序列太大;不能大于 32

    我写了这段代码 from Crypto Cipher import AES import numpy as np import cv2 base64 BLOCK SIZE 16 PADDING pad lambda s s BLOCK SI
  • 导入错误:无法导入名称

    我有一个名为 google translate python 的库 https github com terryyin google translate python https github com terryyin google tra
  • print() 函数的有趣/奇怪的机制

    我正在学习Python 我目前正在学习如何定义自己的函数 并且在尝试理解返回值和打印它之间的区别时遇到了一些困难 我读到的关于这个主题的描述对我来说不太清楚 所以我开始自己尝试 我想我现在已经明白了 如果我没记错的话 区别在于你可以传递 a
  • 如何限制单元测试的最大运行时间?

    我目前正在运行一些单元测试 这些测试可能需要很长时间才能失败或无限期地运行 在成功的测试运行中 它们总是会在一定的时间内完成 是否可以创建一个 pytest 单元测试 如果在一定时间内未完成 该测试就会失败 您可以安装 pytest tim

随机推荐

  • 光条中心提取方法总结(二)

    传统算法见之前的文章 光条中心提取方法总结 一 视觉菜鸟Leonardo的博客 CSDN博客e 二 深度学习方法 利用深度学习来进行光条中心提取是这几年刚兴起的方法 目前可供参考的论文屈指可数 方法从两个途径切入 1 利用深度学习进行光条图
  • 研一Python基础课程第二周课后习题分享(含代码)

    一 问题描述 共计18道 1 问题1 你买了n个苹果 但是很不幸里面混进了一条虫子 如果虫子每x小时吃完一只苹果 然后开始吃下一个 经过y小时后 你还有几个完整的苹果 分别输入n x y三个整型数值 输出结果 2 问题2 分别输入两个时间
  • javascript 实现Base64加密

    想必大家对base64并不陌生吧 在本文将为大家介绍下Js中的base64加密解密过程 感兴趣的朋友不要错过 html view plain copy
  • 关于存储那些事1-----基础篇

    目录 一 SSD 1 简介 1 1 分类 1 1 1 易失性存储器 1 1 2 非易失性存储器 2 SSD接口 2 1 SATA接口 2 2 SATA Express接口 2 3 SAS接口 2 4 U 2接口 2 5 mSATA接口 2
  • 【解决方案】LaTeX插入svg图片

    LaTeX插入svg图片的解决方案 今天在写论文时 想在论文里插入svg图片 遇到了问题 百度了一下方法 发现LaTeX不支持插入svg图片 在捣鼓了一下之后 发现基本的方法不是失效就是比较麻烦 本文简单总结了两个解决方案 发现都不太行 研
  • 系统及服务器巡检流程图,巡检日常工作流程图

    巡检日常工作流程图 由会员分享 可在线阅读 更多相关 巡检日常工作流程图 1页珍藏版 请在人人文库网上搜索 1 质质检检日日常常巡巡检检流流程程图图 查查看看生生产产交交接接半半成成品品或或成成品品 初初步步确确定定生生产产零零件件 准准备
  • Win10下安装mujuco

    1 背景 安装mujuco之前玩的环境都是些简单的 易处理的环境 就是下面这种 第一张图是移动下面的方块保持杆子立起来环境 第二张图是小车爬山环境 第三张图是给杆子施加力使得杆子保持立起来环境 从图也可以看出 是比较简单的环境 而mujuc
  • 批量文本文件内容替换之Linux sed命令

    文章目录 sed命令简介 需求 sed实现批量替换 sed命令简介 Linux sed命令可以使用shell脚本进行文件的批量处理 如批量替换 修改等等 尤其是在需要对大量文本文件进行批量操作时 使用sed命令会起到事半功倍的效果 关于详细
  • 其他-08-idea配置查询字节码

    1 字节码查询 查看一下idea是否安装了 一般都安装了 编译一下 生成target 点击View下面的Show ByteCode即可 其实你看到的字节码是java加工多的 可以看下这个类 原生都是数字 以 helloWql方法字节码解释
  • 为何程序员完成最后20%的工作需要的时间跟之前的80%一样多?

    听过行百里者半九十吧 这句话在程序员的工作中同样适用 到底是为何呢 Matija用一个精巧的比喻揭示了个中道理 其实这就好比在高峰期从郊外开车回市中心 前 80 的路程很顺 高速嘛 可能两小时就走完了 但是到了城里 就走不动了 红绿灯 人行
  • MATLAB点云处理函数整理

    pcbin 空间bin点云点 bins pcbin ptCloud numBins bins pcbin ptCloud numBins spatialLimits bins binLocations pcbin pcdenoise 去噪
  • 数据结构与算法之二叉树的建立

    文章目录 一 已知二叉树的先序和中序数列 创建二叉树 1 算法思想 2 代码实现 二 已知二叉树的先序和后序数列 创建二叉树 1 算法思想 2 代码实现 三 二叉树的顺序存放 打印先 中 后序遍历 一 已知二叉树的先序和中序数列 创建二叉树
  • Java图形化界面编程一

    目录 一 介绍 二 AWT编程 2 1AWT介绍 2 2 AWT继承体系 2 3 Container容器 2 3 1 Container继承体系 2 3 2 常见API 2 3 3 容器演示 2 4 LayoutManager布局管理器 2
  • Keras使用VGG16模型预测自己的图片

    Keras使用VGG16模型预测自己的图片 环境 Win10 Miniconda3 Pycharm2018 02 代码如下 from keras applications vgg16 import VGG16 from keras prep
  • 计算机视觉课程设计:基于Mediapipe的体感游戏设计

    演示视频 计算机视觉课程设计 基于Mediapipe的体感游戏设计 哔哩哔哩 bilibili
  • SpringCloud微服务架构标准版本拓扑图

    本图是公司需要 自己整理的SpringCloud微服务架构标准版本拓扑图 有 eddx格式 需要请私信 为了方便截了个jpg 希望对你有所帮助 喜欢的朋友点赞收藏转发
  • Cow Marathon(树的直径)(最长路)

    Cow Marathon Time Limit 2000MS Memory Limit 30000KB 64bit IO Format lld llu Submit Status Description After hearing abou
  • 【树莓派】利用tesseract进行汉字识别

    树莓派 利用tesseract进行汉字识别 安装tesseract库 识别图像中的汉字 安装tesseract库 安装tesseract库和它的python封装 sudo apt install tesseract ocr fix miss
  • Python学习-----文件操作(读写定位篇)

    目录 前言 1 打开文件 open 关闭文件 close 2 文件的读取 文件变量名 f 1 整体读取 read 2 读取一行 readline 3 读取多行 readlines 3 文件的写入 文件变量名f write 4 判断文件的可读
  • 稀疏矩阵的存储格式(Sparse Matrix Formats)

    稀疏矩阵的存储格式 Sparse Matrix Storage Formats 1 Coordinate Format COO 这种存储方式的主要优点是灵活 简单 仅存储非零元素以及每个非零元素的坐标 使用3个数组进行存储 values r