scipy.sparse.

coo_matrix#

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

COO 格式的稀疏矩陣。

也稱為 ‘ijv’ 或 ‘三元組’ 格式。

這可以透過幾種方式實例化
coo_matrix(D)

其中 D 是 2 維 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]。當未指定形狀時,會從索引陣列推斷

Notes

稀疏矩陣可以用於算術運算:它們支持加法、減法、乘法、除法和矩陣冪。

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]])
屬性:
dtypedtype

矩陣的資料類型

shape二元組

矩陣的形狀

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

返回此矩陣的 Hermitian 轉置。

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

將此陣列/矩陣轉換為 COO 格式。

tocsc([copy])

將此陣列/矩陣轉換為壓縮稀疏列格式

tocsr([copy])

將此陣列/矩陣轉換為壓縮稀疏行格式

todense([order, out])

返回此稀疏矩陣的密集表示形式。

todia([copy])

將此陣列/矩陣轉換為稀疏 DIAgonal 格式。

todok([copy])

將此陣列/矩陣轉換為字典鍵格式。

tolil([copy])

將此陣列/矩陣轉換為列表格式。

trace([offset])

返回沿稀疏陣列/矩陣對角線的總和。

transpose([axes, copy])

反轉稀疏陣列/矩陣的維度。

trunc()

元素級 trunc。