scipy.sparse.

csc_array#

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

壓縮稀疏行陣列。

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

其中 D 是一個 2 維 ndarray

csc_array(S)

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

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

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

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

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

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

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

註解

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

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

  • 高效的列切片

  • 快速的矩陣向量乘積(CSR、BSR 可能更快)

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

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

標準格式
  • 在每個 column 內,索引依行排序。

  • 沒有重複的條目。

範例

>>> import numpy as np
>>> from scipy.sparse import csc_array
>>> csc_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, 2, 2, 0, 1, 2])
>>> col = np.array([0, 0, 1, 2, 2, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csc_array((data, (row, col)), shape=(3, 3)).toarray()
array([[1, 0, 4],
       [0, 0, 5],
       [2, 3, 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])
>>> csc_array((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 4],
       [0, 0, 5],
       [2, 3, 6]])
屬性:
dtypedtype

陣列的資料類型

shape2-tuple

陣列的形狀

ndimint

維度數量(這始終為 2)

nnz

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

size

儲存值的數量。

data

陣列的 CSC 格式資料陣列

indices

陣列的 CSC 格式索引陣列

indptr

陣列的 CSC 格式索引指標陣列

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

將此陣列/矩陣轉換為 Block Sparse Row 格式。

tocoo([copy])

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

tocsc([copy])

將此陣列/矩陣轉換為 Compressed Sparse Column 格式。

tocsr([copy])

將此陣列/矩陣轉換為 Compressed Sparse Row 格式。

todense([order, out])

傳回此稀疏陣列的密集表示形式。

todia([copy])

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

todok([copy])

將此陣列/矩陣轉換為 Dictionary Of Keys 格式。

tolil([copy])

將此陣列/矩陣轉換為 List of Lists 格式。

trace([offset])

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

transpose([axes, copy])

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

trunc()

元素級 trunc。

__getitem__

__mul__