sparse-matrix

Get unique rows from a Scipy sparse matrix

霸气de小男生 提交于 2020-06-11 05:47:17
问题 I'm working with sparse matrices in python, I wonder if there is an efficient way to remove duplicate rows in a sparse matrix, and have only the unique rows remain. I did not find a function associated with it and not sure how to do it without converting the sparse matrix to dense and use numpy.unique. 回答1: There is no quick way to do it, so I had to write a function. It returns a sparse matrix with the unique rows (axis=0) or columns (axis=1) of an input sparse matrix. Note that the unique

How to use a sparse matrix in numpy.linalg.solve

送分小仙女□ 提交于 2020-05-29 16:15:10
问题 I want to solve the following linear system for x Ax = b Where A is sparse and b is just regular column matrix. However when I plug into the usual np.linalg.solve(A,b) routine it gives me an error. However when I do np.linalg.solve(A.todense(),b) it works fine. Question. How can I use this linear solve still preserving the sparseness of A ?. The reason is A is quite large about 150 x 150 and there are about 50 such matrices and so keeping it sparse for as long as possible is the way I'd

How to use a sparse matrix in numpy.linalg.solve

自作多情 提交于 2020-05-29 16:13:43
问题 I want to solve the following linear system for x Ax = b Where A is sparse and b is just regular column matrix. However when I plug into the usual np.linalg.solve(A,b) routine it gives me an error. However when I do np.linalg.solve(A.todense(),b) it works fine. Question. How can I use this linear solve still preserving the sparseness of A ?. The reason is A is quite large about 150 x 150 and there are about 50 such matrices and so keeping it sparse for as long as possible is the way I'd

Sparse matrix support for long vectors (over 2^31 elements)

ε祈祈猫儿з 提交于 2020-05-29 07:37:26
问题 I know this question has been asked in the past (here and here, for example), but those questions are years old and unresolved. I am wondering if any solutions have been created since then. The issue is that the Matrix package in R cannot handle long vectors (length greater than 2^31 - 1). In my case, a sparse matrix is necessary for running an XGBoost model because of memory and time constraints. The XGBoost xgb.DMatrix supports using a dgCMatrix object. However, due to the size of my data,

What is a strided array?

旧时模样 提交于 2020-05-24 21:26:09
问题 There is also a counterpart which is called density array. What does this mean? I have done some search, but didn't get accurate information. 回答1: To stride is to "take long steps" thefreedictionary.com/stride For an array this would mean that only some of the elements are present, like just every 10th element. You can then save space by not storing the empty elements in between. A dense array would be one where many, if not all, elements are present so there is no empty space between the

hstack out of memory

狂风中的少年 提交于 2020-05-17 07:42:13
问题 I got scipy.sparse.csr_matrix object wich size is full_sites_sparse.shape (336358, 48371) . Also I have dataframe wich size is union_df[hour].shape (336358, 17) . I want to union there and get another one with size (336358, 48388) I try union there with: full_sites_hour_sparse = np.hstack((full_sites_sparse.A, union_df[hour].values)) and full_sites_hour_sparse = scipy.sparse.hstack(full_sites_sparse.A, union_df[hour].values) But both raise out of memory exception. Any other way to do this? 来源

How to compute scipy sparse matrix determinant without turning it to dense?

冷暖自知 提交于 2020-05-09 19:03:06
问题 I am trying to figure out the fastest method to find the determinant of sparse symmetric and real matrices in python. using scipy sparse module but really surprised that there is no determinant function. I am aware I could use LU factorization to compute determinant but don't see a easy way to do it because the return of scipy.sparse.linalg.splu is an object and instantiating a dense L and U matrix is not worth it - I may as well do sp.linalg.det(A.todense()) where A is my scipy sparse matrix

How to implement ILU precondioner in scipy?

强颜欢笑 提交于 2020-04-30 06:58:05
问题 For the iterative solvers in the scipy.sparse.linalg such as bicg , gmres , etc, there is an option to add the precondioner for the matrix A . However, the documentation is not very clear about what I should give as the preconditioner. If I use ilu = sp.sparse.linalg.spilu(A) , ilu is not any matrices but an object that encompasses many things. Someone asked about a similar question here for Python 2.7, but I doesn't work for me (Python 3.7, scipy version 1.1.0) So my question is how to

Sparse matrix solver with preconditioner

依然范特西╮ 提交于 2020-04-10 03:45:28
问题 I have a scipy.sparse.csc_matrix sparse matrix A of shape (N, N) where N is about 15000 . A has less than 1 % non-zero elements. I need to solve for Ax=b as time efficient as possible. Using scipy.sparse.linalg.spsolve takes about 350 ms using scikit-umfpack . scipy.sparse.linalg.gmres is with 50 ms significantly faster when using an ILU preconditioner. Without preconditioner it takes more than a minute. However, creating the preconditioner takes about 1.5 s . Given that, it would be more

Sparse matrix solver with preconditioner

风流意气都作罢 提交于 2020-04-10 03:44:07
问题 I have a scipy.sparse.csc_matrix sparse matrix A of shape (N, N) where N is about 15000 . A has less than 1 % non-zero elements. I need to solve for Ax=b as time efficient as possible. Using scipy.sparse.linalg.spsolve takes about 350 ms using scikit-umfpack . scipy.sparse.linalg.gmres is with 50 ms significantly faster when using an ILU preconditioner. Without preconditioner it takes more than a minute. However, creating the preconditioner takes about 1.5 s . Given that, it would be more