python - Python 的稀疏矩阵乘法问题

标签 python scipy sparse-matrix

我正在尝试获取稀疏矩阵及其转置的点积。我正在使用 scipy.sparse 库并发现结果不正确。见下文:

import numpy as np
import scipy.sparse 

#Define the dense matrix
matrix_dense = np.zeros([100000,10])
for i in range(10):
    i_0 = i*10000
    i_1 = (i+1)*10000
    matrix_dense[i_0:i_1,i] = 1

#Define the sparse matrix
cols = []
for i in range(10):
    cols+=[i]*10000

dtype = np.uint8 
rows = range(len(cols)) 
data_csc = np.ones(len(cols), dtype=dtype)
matrix_sparse = scipy.sparse.csc_matrix((data_csc, (rows, cols)), shape=(len(cols), 10), dtype=dtype)

#Check that the two matrices are identical
assert np.abs(matrix_sparse.todense() - matrix_dense).max() == 0 

#Dot product of the dense matrix
dense_product = np.dot(matrix_dense.T,matrix_dense)

#Dot product of the sparse matrix
sparse_product = (matrix_sparse.T)*(matrix_sparse)

正确答案(由dense_product给出)应该是对角矩阵,其中对角项等于10,000。

print dense_product
[[ 10000.      0.      0.      0.      0.      0.      0.      0.      0.
   0.]
 [     0.  10000.      0.      0.      0.      0.      0.      0.      0.
   0.]
 [     0.      0.  10000.      0.      0.      0.      0.      0.      0.
   0.]
 [     0.      0.      0.  10000.      0.      0.      0.      0.      0.
   0.]
 [     0.      0.      0.      0.  10000.      0.      0.      0.      0.
   0.]
 [     0.      0.      0.      0.      0.  10000.      0.      0.      0.
   0.]
 [     0.      0.      0.      0.      0.      0.  10000.      0.      0.
   0.]
 [     0.      0.      0.      0.      0.      0.      0.  10000.      0.
   0.]
 [     0.      0.      0.      0.      0.      0.      0.      0.  10000.
   0.]
 [     0.      0.      0.      0.      0.      0.      0.      0.      0.
   10000.]]

但是,无论我如何计算稀疏矩阵,结果都是错误的:

print sparse_product.todense()
[[16  0  0  0  0  0  0  0  0  0]
 [ 0 16  0  0  0  0  0  0  0  0]
 [ 0  0 16  0  0  0  0  0  0  0]
 [ 0  0  0 16  0  0  0  0  0  0]
 [ 0  0  0  0 16  0  0  0  0  0]
 [ 0  0  0  0  0 16  0  0  0  0]
 [ 0  0  0  0  0  0 16  0  0  0]
 [ 0  0  0  0  0  0  0 16  0  0]
 [ 0  0  0  0  0  0  0  0 16  0]
 [ 0  0  0  0  0  0  0  0  0 16]]

我尝试了不同的方法来执行稀疏点积并得到完全相同的答案:

sparse_product_1 = np.dot(matrix_sparse.T,matrix_sparse)
sparse_product_2 = (matrix_sparse.T).dot(matrix_sparse)
sparse_product_3 = scipy.sparse.csr_matrix.dot((matrix_sparse.T), 
matrix_sparse)

知道发生了什么吗?

最佳答案

看起来您正在使用 uint8 数据类型,其最大值为 256,并且可能发生溢出,最终得到 10000%256这给你 16。

以下是正在发生的情况的示例:

x = np.array(10000, dtype = np.uint8)
x
array(16, dtype=uint8)

将您的 dtype 更改为 np.int64 对我有用:

dtype = np.int64

关于python - Python 的稀疏矩阵乘法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45825786/

相关文章:

java - 使用 java Runtime.exec 在 Windows 上执行 python 脚本

scipy - tf idf 上截断的 svd 给出值错误数组太大

Python:[Errno 2]没有这样的文件或目录:...site-packages/testpath-0.3.1.dist-info/top_level.txt

python 绘制填充颜色的饼图

python - Python 中 NumPy 或 SciPy 中 Wolfram Mathematica 的模拟函数

python - 为什么 scipy 为结果应该为零的积分提供非零结果?

c++ - 如何访问 C++ Eigen 稀疏矩阵中的特定(行,列)索引?

arrays - 两个稀疏矩阵相乘的算法

c++ - 如何使用Rcpp初始化mappedsparsematrix

python - 使用 Pillow + WebDriver 上传带有内存流的图像以进行输入?