<分区>
我有一个函数接受 csr_matrix
并对其进行一些计算。
这些计算的行为要求这个矩阵的形状是特定的(比如 NxM
)。
我发送的输入有较少的列和确切的行数。
(例如它有 shape=(A,B) 其中 A < N 和 B == M)
例如:我有对象x
>>>x = csr_matrix([[1,2],[1,2]])
>>>x
(0, 0) 1
(0, 1) 2
(1, 0) 1
(1, 1) 2
>>>x.shape
(2, 2)
还有一个函数f
:
def f(csr_mat):
"""csr_mat.shape should be (2,3)"""
然后我想在x
上做点什么,所以它会变成y
:
>>>y = csr_matrix([[1,2,0],[1,2,0]])
>>>y
(0, 0) 1
(0, 1) 2
(1, 0) 1
(1, 1) 2
>>>y.shape
(2, 3)
在此示例中,x
和y
具有相同的非零值,但y
具有不同的形状。我想要的是有效地“扩展”x
到一个新维度,用零填充新列。即,给定 x
和 new_shape=(2,3)
,它应该返回 y
。
我已经尝试过 reshape
:
x.reshape((2,3))
但后来我得到了:
NotImplementedError
我的第二个选择是创建具有不同形状的新 csr_matrix
:
z = csr_matrix(x,shape=(3,3))
但这也失败了:
NotImplementedError: Reshaping not implemented for csr_matrix.
编辑:使用 csc_matrix 带来了同样的错误。
有什么想法吗?
谢谢