官方文档可见:https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.html#scipy.sparse.csc_matrix
官方文档中关于CSC矩阵原文如下:
csc_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSC representation where the row indices for column i are stored in indices[indptr[i]:indptr[i+1]]
and their corresponding values are stored in data[indptr[i]:indptr[i+1]]
. If the shape parameter is not supplied, the matrix dimensions are inferred from the index arrays.
原文翻译:第i列的相对应的行信息,存储在indices[indptr[i]:indptr[i+1]]中,且与行列想对应的值存在于 data[indptr[i]:indptr[i+1]]。
相对应的测试代码如下:
>>> 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_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 4],
[0, 0, 5],
[2, 3, 6]])
我的解读:
1. 首先,indptr中列表的长度len决定矩阵的列数num_column = len - 1,即原文档中的 column i
2. i = 0, indices [indptr[0]] : indptr[1]] = indices[0:2] = [0, 2], 即第0列对应于第0行和第2行,再看data中的值,即矩阵中(0,0)= 1, (2,0)=2;
i = 1, indices [indptr[1]] : indptr[2]] = indices[2:3] = [2], 即第1列对应于第2行,再看data中的值,即矩阵中(2,1)= 3;
i = 2, indices [indptr[2]] : indptr[3]] = indices[3:6] = [0,1,2], 即第2列对应于第0行、第1行和第2行,再看data中的值,即矩阵中(0,2)= 4, (1,2)=5, (2,2)=6;
以上(a,b)表示a行b列