Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 3.x Scipy sparse.kron给出了非稀疏矩阵_Python 3.x_Scipy_Sparse Matrix - Fatal编程技术网

Python 3.x Scipy sparse.kron给出了非稀疏矩阵

Python 3.x Scipy sparse.kron给出了非稀疏矩阵,python-3.x,scipy,sparse-matrix,Python 3.x,Scipy,Sparse Matrix,当使用Scipy的稀疏模块的kron方法时,我得到了意想不到的非稀疏结果。具体来说,执行kronecker乘积后等于零的矩阵元素将保留在结果中,我想了解我应该如何确保输出仍然是完全稀疏的 以下是我的意思的一个示例,以两份身份副本的kronecker产品为例: import scipy.sparse as sp s = sp.eye(2) S = sp.kron(s,s) S <4x4 sparse matrix of type '<class 'numpy.float64'&

当使用Scipy的稀疏模块的kron方法时,我得到了意想不到的非稀疏结果。具体来说,执行kronecker乘积后等于零的矩阵元素将保留在结果中,我想了解我应该如何确保输出仍然是完全稀疏的

以下是我的意思的一个示例,以两份身份副本的kronecker产品为例:

import scipy.sparse as sp

s = sp.eye(2)

S = sp.kron(s,s)

S 
<4x4 sparse matrix of type '<class 'numpy.float64'>'
with 8 stored elements (blocksize = 2x2) in Block Sparse Row format>

print(S)

(0, 0)  1.0
(0, 1)  0.0
(1, 0)  0.0
(1, 1)  1.0
(2, 2)  1.0
(2, 3)  0.0
(3, 2)  0.0
(3, 3)  1.0
将scipy.sparse导入为sp
s=sp.eye(2)
S=标准克朗(S,S)
s
印刷品
(0, 0)  1.0
(0, 1)  0.0
(1, 0)  0.0
(1, 1)  1.0
(2, 2)  1.0
(2, 3)  0.0
(3, 2)  0.0
(3, 3)  1.0
稀疏矩阵S应该只包含4个(对角)非零项,但这里还有其他等于零的项。任何关于我做错了什么的指点都将不胜感激。

我指出,
sparse.kron
默认生成一个
BSR
格式矩阵。这就是你的显示器所显示的。这些额外的零是密集块的一部分

如果指定另一种格式,
kron
将不会产生这些零:

In [672]: sparse.kron(s,s,format='csr')                                         
Out[672]: 
<4x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>
In [673]: _.A                                                                   
Out[673]: 
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
[672]中的
:sparse.kron(s,s,format='csr')
出[672]:
在[673]中:
出[673]:
数组([[1,0,0,0.]),
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

非常感谢@hpaulj,这很有意义。