scipy.sparse.csr_matrix

class scipy.sparse.csr_matrix(arg1, shape=None, dtype=None, copy=False)[源代码]

压缩稀疏行矩阵

这可以通过几种方式实例化:
CSR_矩阵(D)

具有稠密矩阵或秩为2的ndarray D

CSR_矩阵(S)

具有另一个稀疏矩阵S(等价于S.tocsr())

CSR_矩阵((M,N), [数据类型] )

要构造形状为(M,N)的空矩阵,dtype是可选的,默认为dtype=‘d’。

csr_Matrix((data,(row_ind,colind), [shape=(M, N)] )

哪里 datarow_indcol_ind 满足关系 a[row_ind[k], col_ind[k]] = data[k]

CSR_矩阵((data,index,indptr), [shape=(M, N)] )

是存储第i行的列索引的标准CSR表示形式 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]])
属性
dtype数据类型

矩阵的数据类型

shape2元组

获得矩阵的形状。

ndim集成

维数(该值始终为2)

nnz

存储值的数量,包括显式零。

data

CSR格式矩阵的数据数组

indices

CSR格式矩阵的索引数组

indptr

CSR格式的矩阵的索引指针数组

has_sorted_indices

确定矩阵是否具有排序索引

方法:

__len__ \()

__mul__ \(其他)

解释其他并调用以下任一

arcsin \()

基于元素的反正弦。

arcsinh \()

以元素为单位的反正弦。

arctan \()

基于元素的弧线。

arctanh \()

以元素为单位的弧度。

argmax \([axis, out] )

返回轴上最大元素的索引。

argmin \([axis, out] )

返回轴上最小元素的索引。

asformat \(格式[, copy] )

以传递的格式返回此矩阵。

asfptype \()

将矩阵向上转换为浮点格式(如有必要)

astype \(dtype[, casting, copy] )

将矩阵元素强制转换为指定类型。

ceil \()

按元素排列的单元格。

check_format \([full_check] )

检查矩阵格式是否有效

conj \([copy] )

元素复数共轭。

conjugate \([copy] )

元素复数共轭。

copy \()

返回此矩阵的副本。

count_nonzero \()

非零条目数,相当于

deg2rad \()

元素方向的de2rad。

diagonal \([k] )

返回矩阵的第k条对角线。

dot \(其他)

普通点积

eliminate_zeros \()

从矩阵中删除零个条目

expm1 \()

基于元素的表达式1。

floor \()

基于图元的楼板。

getH \()

返回此矩阵的厄米转置。

get_shape \()

获得矩阵的形状。

getcol \(i)

以(M X 1)CSR矩阵(列向量)的形式返回矩阵第i列的副本。

getformat \()

矩阵表示形式为字符串的格式。

getmaxprint \()

打印时显示的最大元素数。

getnnz \([axis] )

存储值的数量,包括显式零。

getrow \(i)

以(1 X N)CSR矩阵(行向量)的形式返回矩阵第i行的副本。

log1p \()

基于元素的log1p。

max \([axis, out] )

返回矩阵的最大值或沿轴的最大值。

maximum \(其他)

此矩阵与另一个矩阵之间的元素最大值。

mean \([axis, dtype, out] )

计算沿指定轴的算术平均值。

min \([axis, out] )

返回轴上矩阵的最小值或最大值。

minimum \(其他)

此矩阵与另一个矩阵之间的元素最小值。

multiply \(其他)

按点乘以另一个矩阵、向量或标量。

nonzero \()

非零折射率

power \(n[, dtype] )

此函数执行元素级电源。

prune \()

删除所有非零元素后的空格。

rad2deg \()

基于元素的rad2deg。

reshape \(自身,形状[, order, copy] )

在不更改稀疏矩阵数据的情况下为其赋予新形状。

resize \(*形状)

就地调整矩阵大小至给定的尺寸 shape

rint \()

基于元素的Rint。

set_shape \(形状)

看见 reshape

setdiag \(值[, k] )

设置阵列的对角或非对角元素。

sign \()

以元素为基础的符号。

sin \()

基于元素的罪过。

sinh \()

基于元素的SINH。

sort_indices \()

对此矩阵的索引进行排序 就位

sorted_indices \()

返回带有排序索引的此矩阵的副本

sqrt \()

基于元素的sqrt。

sum \([axis, dtype, out] )

对给定轴上的矩阵元素求和。

sum_duplicates \()

通过将重复的矩阵条目添加到一起来消除重复的矩阵条目

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 \()

基于元素的主干。

__getitem__