dia_matrix#
- class scipy.sparse.dia_matrix(arg1, shape=None, dtype=None, copy=False, *, maxprint=None)[source]#
使用 DIAgonal 儲存的稀疏矩陣。
- 這可以透過幾種方式實例化
- dia_matrix(D)
其中 D 是一個 2 維 ndarray
- dia_matrix(S)
與另一個稀疏陣列或矩陣 S (等同於 S.todia())
- dia_matrix((M, N), [dtype])
建構一個形狀為 (M, N) 的空矩陣,dtype 是可選的,預設為 dtype='d'。
- dia_matrix((data, offsets), shape=(M, N))
其中
data[k,:]
儲存對角線offsets[k]
的對角線項目 (請參閱以下範例)
註解
稀疏矩陣可用於算術運算:它們支援加法、減法、乘法、除法和矩陣冪。使用 DIAgonal 儲存的稀疏矩陣不支援切片。
範例
>>> import numpy as np >>> from scipy.sparse import dia_matrix >>> dia_matrix((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8)
>>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0) >>> offsets = np.array([0, -1, 2]) >>> dia_matrix((data, offsets), shape=(4, 4)).toarray() array([[1, 0, 3, 0], [1, 2, 0, 4], [0, 2, 3, 0], [0, 0, 3, 4]])
>>> from scipy.sparse import dia_matrix >>> n = 10 >>> ex = np.ones(n) >>> data = np.array([ex, 2 * ex, ex]) >>> offsets = np.array([-1, 0, 1]) >>> dia_matrix((data, offsets), shape=(n, n)).toarray() array([[2., 1., 0., ..., 0., 0., 0.], [1., 2., 1., ..., 0., 0., 0.], [0., 1., 2., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 2., 1., 0.], [0., 0., 0., ..., 1., 2., 1.], [0., 0., 0., ..., 0., 1., 2.]])
- 屬性:
方法
__len__
()__mul__
(other)arcsin
()元素級 arcsin。
arcsinh
()元素級 arcsinh。
arctan
()元素級 arctan。
arctanh
()元素級 arctanh。
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)普通點積
expm1
()元素級 expm1。
floor
()元素級 floor。
getH
()返回此矩陣的 Hermitian 轉置。
取得矩陣的形狀
getcol
(j)返回矩陣第 j 行的副本,作為 (m x 1) 稀疏矩陣(列向量)。
矩陣儲存格式
列印時要顯示的最大元素數量。
getnnz
([axis])儲存值的數量,包括顯式的零。
getrow
(i)返回矩陣第 i 列的副本,作為 (1 x n) 稀疏矩陣(行向量)。
log1p
()元素級 log1p。
maximum
(other)此陣列/矩陣與另一個陣列/矩陣之間的元素級最大值。
mean
([axis, dtype, out])沿指定軸計算算術平均值。
minimum
(other)此陣列/矩陣與另一個陣列/矩陣之間的元素級最小值。
multiply
(other)逐點乘以另一個陣列/矩陣。
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])將陣列/矩陣元素沿給定軸求和。
tan
()元素級 tan。
tanh
()元素級 tanh。
toarray
([order, out])返回此稀疏陣列/矩陣的密集 ndarray 表示。
tobsr
([blocksize, copy])將此陣列/矩陣轉換為區塊稀疏列格式。
tocoo
([copy])將此陣列/矩陣轉換為座標格式。
tocsc
([copy])將此陣列/矩陣轉換為壓縮稀疏行格式。
tocsr
([copy])將此陣列/矩陣轉換為壓縮稀疏列格式。
todense
([order, out])返回此稀疏矩陣的密集表示。
todia
([copy])將此陣列/矩陣轉換為稀疏 DIAgonal 格式。
todok
([copy])將此陣列/矩陣轉換為鍵字典格式。
tolil
([copy])將此陣列/矩陣轉換為列表的列表格式。
trace
([offset])返回稀疏陣列/矩陣對角線的總和。
transpose
([axes, copy])反轉稀疏陣列/矩陣的維度。
trunc
()元素級 trunc。