csc_array#
- class scipy.sparse.csc_array(arg1, shape=None, dtype=None, copy=False, *, maxprint=None)[源代码]#
压缩稀疏列数组。
- 这可以通过以下几种方式实例化
- csc_array(D)
其中 D 是一个二维 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)])
其中
data、row_ind和col_ind满足关系a[row_ind[k], col_ind[k]] = data[k]。- csc_array((data, indices, indptr), [shape=(M, N)])
是标准的 CSC 表示,其中列 i 的行索引存储在
indices[indptr[i]:indptr[i+1]]中,其对应值存储在data[indptr[i]:indptr[i+1]]中。如果未提供 shape 参数,则数组维度从索引数组中推断。
- 属性:
- 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)普通点积
从数组/矩阵中移除零项
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。
就地排序此数组/矩阵的索引
返回此数组/矩阵的已排序索引副本
sqrt()逐元素的 sqrt。
sum([axis, dtype, out])对给定轴上的数组/矩阵元素求和。
通过相加消除重复项
tan()逐元素的 tan。
tanh()逐元素的 tanh。
toarray([order, out])返回此稀疏数组/矩阵的密集 ndarray 表示。
tobsr([blocksize, copy])将此数组/矩阵转换为块稀疏行格式。
tocoo([copy])将此数组/矩阵转换为 COOrdinate 格式。
tocsc([copy])将此数组/矩阵转换为压缩稀疏列格式。
tocsr([copy])将此数组/矩阵转换为压缩稀疏行格式。
todense([order, out])返回此稀疏数组的密集表示。
todia([copy])将此数组/矩阵转换为稀疏 DIAgonal 格式。
todok([copy])将此数组/矩阵转换为键字典格式。
tolil([copy])将此数组/矩阵转换为列表的列表格式。
trace([offset])返回稀疏数组/矩阵沿对角线的和。
transpose([axes, copy])反转稀疏数组/矩阵的维度。
trunc()逐元素的 trunc。
__getitem__
__mul__
注释
稀疏数组可用于算术运算:它们支持加法、减法、乘法、除法和矩阵幂。
- CSC 格式的优点
高效的算术运算,如 CSC + CSC、CSC * CSC 等。
高效的列切片
快速的矩阵向量乘积 (CSR、BSR 可能更快)
- CSC 格式的缺点
慢速的行切片操作 (考虑使用 CSR)
稀疏结构的变化开销大 (考虑使用 LIL 或 DOK)
- 规范格式
在每列中,索引按行排序。
没有重复项。
示例
>>> 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]])