gpt4 book ai didi

matrix - 稀疏矩阵 : ValueError: matrix type must be 'f' , 'd' 、 'F' 或 'D'

转载 作者:行者123 更新时间:2023-12-04 10:42:47 26 4
gpt4 key购买 nike

我想通过使用 scipy 对稀疏矩阵进行 SVD:

from svd import compute_svd
print("The size of raw matrix: "+str(len(raw_matrix))+" * "+str(len(raw_matrix[0])))

from scipy.sparse import dok_matrix
dok = dok_matrix(raw_matrix)

matrix = compute_svd( dok )

函数 compute_svd 是我的自定义模块,如下所示:
def compute_svd( matrix ):
from scipy.sparse import linalg
from scipy import dot, mat
# e.g., matrix = [[2,1,0,0], [4,3,0,0]]
# matrix = mat( matrix );
# print "Original matrix:"
# print matrix
U, s, V = linalg.svds( matrix )
print "U:"
print U
print "sigma:"
print s
print "VT:"
print V
dimensions = 1
rows,cols = matrix.shape
#Dimension reduction, build SIGMA'
for index in xrange(dimensions, rows):
s[index]=0
print "reduced sigma:"
print s
#Reconstruct MATRIX'
# from scipy import dot
reconstructedMatrix= dot(dot(U,linalg.diagsvd(s,len(matrix),len(V))),V)
#Print transform
print "reconstructed:"
print reconstructedMatrix

return reconstructedMatrix

我得到一个异常(exception):
Traceback (most recent call last):
File "D:\workspace\PyQuEST\src\Practice\baseline_lsi.py", line 96, in <module>
matrix = compute_svd( dok )
File "D:\workspace\PyQuEST\src\Practice\svd.py", line 13, in compute_svd
U, s, V = linalg.svds( matrix )
File "D:\Program\Python26\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 1596, in svds
eigvals, eigvec = eigensolver(XH_X, k=k, tol=tol ** 2)
File "D:\Program\Python26\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 1541, in eigsh
ncv, v0, maxiter, which, tol)
File "D:\Program\Python26\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 519, in __init__
ncv, v0, maxiter, which, tol)
File "D:\Program\Python26\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 326, in __init__
raise ValueError("matrix type must be 'f', 'd', 'F', or 'D'")
ValueError: matrix type must be 'f', 'd', 'F', or 'D'

这是我第一次这样做。我应该如何解决它?有任何想法吗?谢谢!

最佳答案

你必须使用浮点数或 double 。你似乎在使用不受支持的矩阵类型 DOK 的整数?

稀疏 svd:http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.svds.html

关于matrix - 稀疏矩阵 : ValueError: matrix type must be 'f' , 'd' 、 'F' 或 'D',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com