对scipy.sparse.csr_matrix的理解

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

Compressed Sparse Row matrix

  • csr_matrix((data, indices, indptr), [shape=(M, N)])
    • is the standard CSR representation where the column indices for row 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.
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]])

  • 理解:
    • 默认是从0,1,2行依次存放数据。
    • 第1行存放data_array中indices为[0,2)的值,第2行存放data_array中indices为[2,3)的值,第3行存放data_array中indices为[3,6)的值
    • 第1行存放的数的列下表是indices_array中indices为[0,2)的值,第2行存放的数的列下表是indices_array中indices为[2,3)的值,第3行存放的数的列下表是indices_array中indices为[3,6)的值。即:存放数的位置依次为(0, 0), (0, 2), (1, 2), (2, 0), (2, 1), (2, 2)

你可能感兴趣的:(Python)