相关疑难解决方法(0)

将一列零添加到csr_matrix

我有一个MxN稀疏csr_matrix,我想在矩阵的右边添加一些只有零的列.原则上,阵列indptr,indicesdata保持相同的,所以我只是想改变矩阵的尺寸.但是,这似乎没有实现.

>>> A = csr_matrix(np.identity(5), dtype = int)
>>> A.toarray()
array([[1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0],
       [0, 0, 0, 0, 1]])
>>> A.shape
(5, 5)
>>> A.shape = ((5,7))
NotImplementedError: Reshaping not implemented for csr_matrix.
Run Code Online (Sandbox Code Playgroud)

水平堆叠零矩阵似乎也不起作用.

>>> B = csr_matrix(np.zeros([5,2]), dtype = int)
>>> B.toarray()
array([[0, 0],
       [0, 0],
       [0, 0],
       [0, 0],
       [0, 0]])
>>> np.hstack((A,B))
array([ …
Run Code Online (Sandbox Code Playgroud)

python numpy scipy sparse-matrix

7
推荐指数
2
解决办法
3507
查看次数

标签 统计

numpy ×1

python ×1

scipy ×1

sparse-matrix ×1