python - scipy sparse matrix -- accessing multiple elements of a path -
i have scipy sparse matrix , (long) list of coordinates
myrows=[i1,i2,...] mycols=[j1,j2,...]
. need list of values [a[i1,j2],a[i2,j2],...]
. how can quickly. loop slow.
i've thought cython.inline()
(which use in other places in code) or weave, don't see how use sparse type efficiently in cython or c++. missing simple?
currently i'm using hack seems inefficient , possibly wrong -- flag error message. here badly written code. note relies on ordering of elements preserved under addition , assumes elements in myrows,mycols in a.
import scipy.sparse sps def getmatvals(a,myrows,mycols) #a coo_matrix b = sps.coo_matrix((range(1,1+a.nnz),(a.row,a.col)),shape=a.shape) t = sps.coo_matrix(([a.nnz+1]*len(myrows),(myrows,mycols)),shape=a.shape) g = b-t #signify myelements in g negatives , others 0's h = np.minimum([0]*a.nnz,g.data) #remove elements h = h[np.nonzero(h)] h = h + a.nnz return a.data[h]
Comments
Post a Comment