scipy.sparse.

csr_matrix#

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

壓縮稀疏行矩陣。

這可以通過幾種方式實例化
csr_matrix(D)

其中 D 是一個 2 維 ndarray

csr_matrix(S)

與另一個稀疏陣列或矩陣 S(等效於 S.tocsr())

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

構造一個形狀為 (M, N) 的空矩陣,dtype 是可選的,預設為 dtype=’d’。

csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])

其中 datarow_indcol_ind 滿足關係式 a[row_ind[k], col_ind[k]] = data[k]

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

是標準 CSR 表示法,其中行 i 的列索引儲存在 indices[indptr[i]:indptr[i+1]] 中,它們的對應值儲存在 data[indptr[i]:indptr[i+1]] 中。如果未提供 shape 參數,則矩陣維度從索引陣列推斷。

註解

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

CSR 格式的優點
  • 高效的算術運算 CSR + CSR、CSR * CSR 等。

  • 高效的行切片

  • 快速矩陣向量乘積

CSR 格式的缺點
  • 慢速的列切片運算(考慮 CSC)

  • 對稀疏結構的更改代價很高(考慮 LIL 或 DOK)

標準格式
  • 在每一行中,索引按列排序。

  • 沒有重複的條目。

範例

>>> import numpy as np
>>> 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])
>>> 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 = np.array([0, 1, 2, 0])
>>> col = np.array([0, 1, 1, 0])
>>> data = np.array([1, 2, 4, 8])
>>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
array([[9, 0, 0],
       [0, 2, 0],
       [0, 4, 0]])

作為如何逐步建構 CSR 矩陣的範例,以下程式碼片段從文字建構詞彙-文件矩陣

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

矩陣的資料類型

shape2-tuple

矩陣的形狀

ndimint

維度數量(始終為 2)

nnz

儲存值的數量,包括顯式零值。

size

儲存值的數量。

data

矩陣的 CSR 格式資料陣列

indices

矩陣的 CSR 格式索引陣列

indptr

矩陣的 CSR 格式索引指標陣列

has_sorted_indices

索引是否已排序

has_canonical_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。

check_format([full_check])

檢查陣列/矩陣是否符合 CSR 或 CSC 格式。

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

此函數執行元素級冪運算。

prune()

移除所有非零元素後的空白空間。

rad2deg()

元素級 rad2deg。

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

給予稀疏陣列/矩陣新的形狀,而不更改其資料。

resize(*shape)

將陣列/矩陣就地調整大小為 shape 給定的維度

rint()

元素級 rint。

set_shape(shape)

就地設定矩陣的形狀

setdiag(values[, k])

設定陣列/矩陣的對角線或非對角線元素。

sign()

元素級 sign。

sin()

元素級 sin。

sinh()

元素級 sinh。

sort_indices()

就地排序此陣列/矩陣的索引

sorted_indices()

返回此陣列/矩陣的副本,其中索引已排序

sqrt()

元素級 sqrt。

sum([axis, dtype, out])

將陣列/矩陣元素沿給定軸求和。

sum_duplicates()

通過將重複的條目相加來消除它們

tan()

元素級 tan。

tanh()

元素級 tanh。

toarray([order, out])

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

tobsr([blocksize, copy])

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

tocoo([copy])

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

tocsc([copy])

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

tocsr([copy])

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

todense([order, out])

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

todia([copy])

將此陣列/矩陣轉換為稀疏對角線格式。

todok([copy])

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

tolil([copy])

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

trace([offset])

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

transpose([axes, copy])

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

trunc()

元素級 trunc。

__getitem__