python 2.7 - sparse matrix calculations in scipy -
sorry rookie question. i'm learning work scipy.sparse , i'm out of ideas why code not work. dimensions correct subtraction can not computed:
c=(count_mat[i]) # c ith row of sparse csr matrix count_mat, has d # elements l=c.sum() second_mnt+=(1/(l*(l-1)))*((c.t*c)-spdiags(c,0,d,d)) the subtraction between 2 d*d sparse matrices can not computed.
edit: error in line calculating second_mnt. hard think it's related other subtraction between 2 matrices. complete traceback:
traceback (most recent call last): file "c:/users/hfjrk/pycharmprojects/project/my_code_1.py", line 28, in <module> second_mnt+=(1/(l*(l-1)))*((c.t*c)-sparse.spdiags(c,0,d,d)) file "c:\python27\lib\site-packages\scipy\sparse\compressed.py", line 366, in __sub__ return self._binopt(other,'_minus_') file "c:\python27\lib\site-packages\scipy\sparse\compressed.py", line 1039, in _binopt other = self.__class__(other) file "c:\python27\lib\site-packages\scipy\sparse\compressed.py", line 31, in __init__ arg1 = arg1.asformat(self.format) file "c:\python27\lib\site-packages\scipy\sparse\base.py", line 213, in asformat return getattr(self,'to' + format)() file "c:\python27\lib\site-packages\scipy\sparse\dia.py", line 237, in tocsc return self.tocoo().tocsc() file "c:\python27\lib\site-packages\scipy\sparse\coo.py", line 313, in tocsc indptr, indices, data) typeerror: cannot cast array data dtype('o') dtype('bool') according rule 'safe'
i'm having problems the
spdiags(c,0,d,d) term. produces matrix, converts coo fine. throws error when .todense or tocsr. data object, not number or array.
try this:
sparse.spdiags(c.a,0,4,4) in other words, data argument spdiags should list or numpy array, not sparse matrix.
Comments
Post a Comment