Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向csr_矩阵添加一列零_Python_Numpy_Scipy_Sparse Matrix - Fatal编程技术网

Python 向csr_矩阵添加一列零

Python 向csr_矩阵添加一列零,python,numpy,scipy,sparse-matrix,Python,Numpy,Scipy,Sparse Matrix,我有一个MxN稀疏csr_矩阵,我想在矩阵的右边添加几个只有零的列。原则上,数组indptr,索引和数据保持不变,因此我只想更改矩阵的维度。然而,这似乎没有得到实施 >>> 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,

我有一个MxN稀疏
csr_矩阵
,我想在矩阵的右边添加几个只有零的列。原则上,数组
indptr
索引
数据
保持不变,因此我只想更改矩阵的维度。然而,这似乎没有得到实施

>>> 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.
此外,水平堆叠零矩阵似乎不起作用

>>> 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([ <5x5 sparse matrix of type '<type 'numpy.int32'>'
    with 5 stored elements in Compressed Sparse Row format>,
       <5x2 sparse matrix of type '<type 'numpy.int32'>'
    with 0 stored elements in Compressed Sparse Row format>], dtype=object)

您可以使用
scipy.sparse.vstack
scipy.sparse.hstack
更快地执行此操作:

from scipy.sparse import csr_matrix, vstack, hstack

B = csr_matrix((5, 2), dtype=int)
C = csr_matrix((5, 2), dtype=int)
D = csr_matrix((10, 10), dtype=int)

B2 = vstack((B, C))
#<10x2 sparse matrix of type '<type 'numpy.int32'>'
#        with 0 stored elements in COOrdinate format>

hstack((B2, D))
#<10x12 sparse matrix of type '<type 'numpy.int32'>'
#        with 0 stored elements in COOrdinate format>
从scipy.sparse导入csr_矩阵,vstack,hstack
B=csr_矩阵((5,2),数据类型=int)
C=csr_矩阵((5,2),数据类型=int)
D=csr_矩阵((10,10),dtype=int)
B2=vstack((B,C))
#
hstack((B2,D))
#

请注意,输出是一个
coo_矩阵
,可以有效地转换为
CSR
CSC
格式。

您想要做的并不是numpy或scipy真正理解为重塑的事情。但对于您的特定情况,您可以创建一个新的CSR矩阵,从原始矩阵中重复使用
数据
索引
indptr
,而无需复制它们:

import scipy.sparse as sps

a = sps.rand(10000, 10000, density=0.01, format='csr')

In [19]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),
...                             shape=(10000, 10020), copy=True)
100 loops, best of 3: 6.26 ms per loop

In [20]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),
...                             shape=(10000, 10020), copy=False)
10000 loops, best of 3: 47.3 us per loop

In [21]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),
...                             shape=(10000, 10020))
10000 loops, best of 3: 48.2 us per loop
因此,如果您不再需要原始矩阵
a
,因为默认值为
copy=False
,只需执行以下操作:

a = sps.csr_matrix((a.data, a.indices, a.indptr), shape=(10000, 10020))
a = sps.csr_matrix((a.data, a.indices, a.indptr), shape=(10000, 10020))