scipy.sparse.

coo_matrix#

class scipy.sparse.coo_matrix(arg1, shape=None, dtype=None, copy=False, *, maxprint=None)[源代码]#

一个 COOrdinate 格式的稀疏矩阵。

也称为“ijv”或“三元组”格式。

可以通过多种方式实例化
coo_matrix(D)

其中 D 是一个二维 ndarray

coo_matrix(S)

与另一个稀疏数组或矩阵 S (等同于 S.tocoo())

coo_matrix((M, N), [dtype])

构造一个形状为 (M, N) 的空矩阵。dtype 是可选的,默认为 dtype='d'。

coo_matrix((data, (i, j)), [shape=(M, N)])
从三个数组构造
  1. data[:] 矩阵的条目,顺序不限

  2. i[:] 矩阵条目的行索引

  3. j[:] 矩阵条目的列索引

其中 A[i[k], j[k]] = data[k]。当未指定 shape 时,它会从索引数组中推断出来。

属性:
dtypedtype

矩阵的数据类型

shape2-tuple

矩阵的形状

ndimint

维度数(这始终为 2)

nnz

存储值的数量,包括显式零。

size

存储值的数量。

data

矩阵的 COO 格式数据数组

row

矩阵的 COO 格式行索引数组

col

矩阵的 COO 格式列索引数组

has_canonical_formatbool

矩阵是否具有排序索引且没有重复项

format

矩阵的格式字符串。

T

转置。

方法

__len__()

__mul__(other)

arcsin()

逐元素 arcsin。

arcsinh()

逐元素 arcsinh。

arctan()

逐元素 arctan。

arctanh()

逐元素 arctanh。

argmax([axis, out, explicit])

返回沿轴的最大元素的索引。

argmin([axis, out, explicit])

返回沿轴的最小元素的索引。

asformat(format[, copy])

将此数组/矩阵以指定格式返回。

asfptype()

将矩阵向上转换为浮点格式(如果需要)

astype(dtype[, casting, copy])

将数组/矩阵元素转换为指定类型。

ceil()

逐元素 ceil。

conj([copy])

逐元素复数共轭。

conjugate([copy])

逐元素复数共轭。

copy()

返回此数组/矩阵的副本。

count_nonzero([axis])

非零条目数量,等同于

deg2rad()

逐元素 deg2rad。

diagonal([k])

返回数组/矩阵的第 k 个对角线。

dot(other)

返回两个数组的点积。

eliminate_zeros()

从数组/矩阵中移除零条目

expm1()

逐元素 expm1。

floor()

逐元素 floor。

getH()

返回此矩阵的厄米转置。

get_shape()

获取矩阵的形状

getcol(j)

返回矩阵第 j 列的副本,作为一个 (m x 1) 稀疏矩阵(列向量)。

getformat()

矩阵存储格式

getmaxprint()

打印时显示的最大元素数量。

getnnz([axis])

存储值的数量,包括显式零。

getrow(i)

返回矩阵第 i 行的副本,作为一个 (1 x n) 稀疏矩阵(行向量)。

log1p()

逐元素 log1p。

max([axis, out, explicit])

返回数组/矩阵的最大值或沿轴的最大值。

maximum(other)

此数组/矩阵与另一个数组/矩阵之间的逐元素最大值。

mean([axis, dtype, out])

计算沿指定轴的算术平均值。

min([axis, out, explicit])

返回数组/矩阵的最小值或沿轴的最小值。

minimum(other)

此数组/矩阵与另一个数组/矩阵之间的逐元素最小值。

multiply(other)

与另一个数组/矩阵的逐元素乘法。

nanmax([axis, out, explicit])

返回沿轴的最大值,忽略任何 NaN。

nanmin([axis, out, explicit])

返回沿轴的最小值,忽略任何 NaN。

nonzero()

数组/矩阵的非零索引。

power(n[, dtype])

此函数执行逐元素幂运算。

rad2deg()

逐元素 rad2deg。

reshape(self, shape[, order, copy])

为稀疏数组/矩阵赋予新形状,而不改变其数据。

resize(*shape)

将数组/矩阵就地调整为 shape 给定的维度

rint()

逐元素 rint。

set_shape(shape)

就地设置矩阵的形状

setdiag(values[, k])

设置数组/矩阵的对角线或非对角线元素。

sign()

逐元素 sign。

sin()

逐元素 sin。

sinh()

逐元素 sinh。

sqrt()

逐元素 sqrt。

sum([axis, dtype, out])

沿着给定轴对数组/矩阵元素求和。

sum_duplicates()

通过将重复条目相加来消除它们

tan()

逐元素 tan。

tanh()

逐元素 tanh。

tensordot(other[, axes])

返回与另一个数组沿给定轴的张量积。

toarray([order, out])

返回此稀疏数组/矩阵的密集 ndarray 表示。

tobsr([blocksize, copy])

将此数组/矩阵转换为块稀疏行格式。

tocoo([copy])

将此数组/矩阵转换为 COOrdinate 格式。

tocsc([copy])

将此数组/矩阵转换为压缩稀疏列格式

tocsr([copy])

将此数组/矩阵转换为压缩稀疏行格式

todense([order, out])

返回此稀疏矩阵的密集表示。

todia([copy])

将此数组/矩阵转换为稀疏对角线格式。

todok([copy])

将此数组/矩阵转换为键字典格式。

tolil([copy])

将此数组/矩阵转换为列表的列表格式。

trace([offset])

返回稀疏数组/矩阵沿对角线的和。

transpose([axes, copy])

反转稀疏数组/矩阵的维度。

trunc()

逐元素 trunc。

备注

稀疏矩阵可用于算术运算:它们支持加法、减法、乘法、除法和矩阵幂。

COO 格式的优点
  • 便于稀疏格式之间快速转换

  • 允许重复条目(参见示例)

  • 与 CSR/CSC 格式之间转换速度非常快

COO 格式的缺点
  • 不直接支持
    • 算术运算

    • 切片

预期用途
  • COO 是一种用于构造稀疏矩阵的快速格式

  • 一旦 COO 矩阵被构造,将其转换为 CSR 或 CSC 格式以便进行快速算术和矩阵向量运算

  • 默认情况下,当转换为 CSR 或 CSC 格式时,重复的 (i,j) 条目将被求和。这有助于高效构建有限元矩阵等。(参见示例)

规范格式
  • 条目和坐标按行然后按列排序。

  • 没有重复条目(即重复的 (i,j) 位置)

  • 数据数组可以包含显式零。

示例

>>> # Constructing an empty matrix
>>> 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)
>>> # Constructing a matrix using ijv format
>>> 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]])
>>> # Constructing a matrix with duplicate coordinates
>>> 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 = coo_matrix((data, (row, col)), shape=(4, 4))
>>> # Duplicate coordinates are maintained until implicitly or explicitly summed
>>> np.max(coo.data)
1
>>> coo.toarray()
array([[3, 0, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 1]])