sparse-matrix

Recommended direct solver for sparse positive definite linear system in scipy?

自闭症网瘾萝莉.ら 提交于 2019-12-07 05:55:48
问题 I'm sorry if this is explained clearly in the scipy.sparse documentation. When using scipy, what function would you recommend using to solve a sparse positive definite linear system of equations? I want to use a direct method, and I want the columns to be reordered so as to preserve sparsity as much as possible in the Cholesky factorization of the coefficient matrix. Ideally I'd be able to experiment with various options for reordering. Does a direct solver for sparse positive definite

scipy.sparse.hstack(([1], [2])) -> “ValueError: blocks must be 2-D”. Why?

那年仲夏 提交于 2019-12-07 05:02:31
问题 scipy.sparse.hstack((1, [2])) and scipy.sparse.hstack((1, [2])) work well, but not scipy.sparse.hstack(([1], [2])) . Why is this the case? Here is a trace of what's happening on my system: C:\Anaconda>python Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v. 1500 64 bit (AMD64)] on win32 >>> import scipy.sparse >>> scipy.sparse.hstack((1, [2])) <1x2 sparse matrix of type '<type 'numpy.int32'>' with 2 stored elements in COOrdinate format> >>> scipy.sparse.hstack(

Converting python sparse matrix dict to scipy sparse matrix

假装没事ソ 提交于 2019-12-07 04:23:21
问题 I am using python scikit-learn for document clustering and I have a sparse matrix stored in a dict object: For example: doc_term_dict = { ('d1','t1'): 12, \ ('d2','t3'): 10, \ ('d3','t2'): 5 \ } # from mysql data table <type 'dict'> I want to use scikit-learn to do the clustering where the input matrix type is scipy.sparse.csr.csr_matrix Example: (0, 2164) 0.245793088885 (0, 2076) 0.205702177467 (0, 2037) 0.193810934784 (0, 2005) 0.14547028437 (0, 1953) 0.153720023365 ... <class 'scipy.sparse

Overloading operator [] for a sparse vector

佐手、 提交于 2019-12-07 03:39:54
问题 I'm trying to create a "sparse" vector class in C++, like so: template<typename V, V Default> class SparseVector { ... } Internally, it will be represented by an std::map<int, V> (where V is the type of value stored). If an element is not present in the map, we will pretend that it is equal to the value Default from the template argument. However, I'm having trouble overloading the subscript operator, [] . I must overload the [] operator, because I'm passing objects from this class into a

Using scipy sparse matrices to solve system of equations

本小妞迷上赌 提交于 2019-12-07 03:34:04
问题 This is a follow up to How to set up and solve simultaneous equations in python but I feel deserves its own reputation points for any answer. For a fixed integer n , I have a set of 2(n-1) simultaneous equations as follows. M(p) = 1+((n-p-1)/n)*M(n-1) + (2/n)*N(p-1) + ((p-1)/n)*M(p-1) N(p) = 1+((n-p-1)/n)*M(n-1) + (p/n)*N(p-1) M(1) = 1+((n-2)/n)*M(n-1) + (2/n)*N(0) N(0) = 1+((n-1)/n)*M(n-1) M(p) is defined for 1 <= p <= n-1 . N(p) is defined for 0 <= p <= n-2 . Notice also that p is just a

How to get a big sparse matrix in R? (> 2^31-1)

南楼画角 提交于 2019-12-07 03:30:38
问题 I use some C++ code to take a text file from a database and create a dgcMatrix type sparse matrix from the Matrix package. For the first time, I'm trying to build a matrix that has more than 2^31-1 non-sparse members, which means that the index vector in the sparse matrix object must also be longer than that limit. Unfortunately, vectors seem to use 32-bit integer indices, as do NumericVectors in Rcpp. Short of writing an entire new data type from the ground up, does R provide any facility

Which is the best way to multiply a large and sparse matrix with its transpose?

别来无恙 提交于 2019-12-07 02:23:25
问题 I currently want to multiply a large sparse matrix(~1M x 200k) with its transpose. The values of the resulting matrix would be in float. I tried loading the matrix in scipy's sparse matrix and by multiplying each row of first matrix with the second matrix. The multiplication took ~2hrs to complete. What is the efficient way to achieve this multiplication? Because I see a pattern in the computation. The matrix being large and sparse . The multiplication of a matrix with its transpose. So, the

Data Structure for Storing Sparse Matrices

ε祈祈猫儿з 提交于 2019-12-07 00:55:30
问题 I need to do some mathematics operations on sparse matrices. I noticed that using arrays may not be the most efficient way to utilize my memory, especially since the matrices may have over 200 rows. I have considered using a linked list too, but I'm not sure if that'll be better. Is there any suitable data structure [approach] to this situation. 回答1: How many "over 200 rows"? How sparse? A 1000x1000 matrix of doubles is still less than 8MB, which is not something I'd worry about unless you

Set all NaN elements in sparse matrix to zero

放肆的年华 提交于 2019-12-06 23:07:24
What's the equivalent of the Matlab statement X(isnan(X))=0 in R? Note X is of type of matrix.csr in R. (This is from pkg:SparseM.) Are you sure you want to use the matrix.csr class? It is from the SparseM package and as far as I can tell, at least from the package documentation, there are no is.na<- or is.na[ methods. The Matrix-package does document is.na-methods: > library(Matrix);M <- Matrix(1:6, nrow=4, ncol=3, + dimnames = list(c("a", "b", "c", "d"), c("A", "B", "C"))) > stopifnot(all(!is.na(M))) > M[2:3,2] <- NA > M[is.na(M)] <- 0 > M 4 x 3 Matrix of class "dgeMatrix" A B C a 1 5 3 b 2

How do I transform a “SciPy sparse matrix” to a “NumPy matrix”?

本小妞迷上赌 提交于 2019-12-06 17:24:00
问题 I am using a python function called "incidence_matrix(G)", which returns the incident matrix of graph. It is from Networkx package. The problem that I am facing is the return type of this function is "Scipy Sparse Matrix". I need to have the Incident matrix in the format of numpy matrix or array. I was wondering if there is any easy way of doing that or not? Or is there any built-in function that can do this transformation for me or not? Thanks 回答1: The scipy.sparse.*_matrix has several