scipy.sparse.

csr_array#

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

壓縮稀疏行陣列。

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

其中 D 是一個 2 維 ndarray

csr_array(S)

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

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

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

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

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

csr_array((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_array
>>> csr_array((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_array((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_array((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_array((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_array((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__()

arcsin()

逐元素 arcsin。

arcsinh()

逐元素 arcsinh。

arctan()

逐元素 arctan。

arctanh()

逐元素 arctanh。

argmax([axis, out, explicit])

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

argmin([axis, out, explicit])

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

asformat(format[, copy])

以傳遞的格式返回此陣列/矩陣。

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。

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。

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__

__mul__