sparse-matrix

Scipy sparse matrix multiplication

青春壹個敷衍的年華 提交于 2019-12-04 21:43:00
问题 I have this example of matrix by matrix multiplication using numpy arrays: import numpy as np m = np.array([[1,2,3],[4,5,6],[7,8,9]]) c = np.array([0,1,2]) m * c array([[ 0, 2, 6], [ 0, 5, 12], [ 0, 8, 18]]) How can i do the same thing if m is scipy sparse CSR matrix? This gives dimension mismatch: sp.sparse.csr_matrix(m)*sp.sparse.csr_matrix(c) 回答1: You can call the multiply method of csr_matrix to do pointwise multiplication. sparse.csr_matrix(m).multiply(sparse.csr_matrix(c)).todense() #

pseudo inverse of sparse matrix in python

时光毁灭记忆、已成空白 提交于 2019-12-04 21:10:41
问题 I am working with data from neuroimaging and because of the large amount of data, I would like to use sparse matrices for my code (scipy.sparse.lil_matrix or csr_matrix). In particular, I will need to compute the pseudo-inverse of my matrix to solve a least-square problem. I have found the method sparse.lsqr, but it is not very efficient. Is there a method to compute the pseudo-inverse of Moore-Penrose (correspondent to pinv for normal matrices). The size of my matrix A is about 600'000x2000

R: sparse matrix multiplication with data.table and quanteda package?

跟風遠走 提交于 2019-12-04 21:02:33
I am trying to create a matrix mulptiplication with sparse matrix and with the package called quanteda, utilising data.table package, related to this thread here . So require(quanteda) mytext <- c("Let the big dogs hunt", "No holds barred", "My child is an honor student") myMatrix <-dfm(mytext, ignoredFeatures = stopwords("english"), stem = TRUE) #a data.table as.matrix(myMatrix) %*% transpose(as.matrix(myMatrix)) how can you get the matrix multiplication working here with quanteda package and sparse matrices? This works just fine: mytext <- c("Let the big dogs hunt", "No holds barred", "My

Matlab allocates a sparse matrix more memory than is required

萝らか妹 提交于 2019-12-04 18:34:58
Suppose I create this sparse matrix, where the non-zero elements consist of booleans 'true': s = sparse([3 2 3 3 3 3 2 34 3 6 3 2 3 3 3 3 2 3 3 6], [10235 11470 21211 33322 49297 88361 91470 127422 152383 158751 166485 171471 181211 193321 205548 244609 251470 283673 312384 318752], true); which contains 20 elements. Matlab ought to allocates no more than (4+4+1)*20 = 180 bytes of memory (it looks like the indices are 4 bytes long). Yet whos s says that the matrix takes up 1275112 bytes in memory, which is a problem as I need to store many thousands of these. Any idea why this happens? Cheers!

Best way to calculate the fundamental matrix of an absorbing Markov Chain?

寵の児 提交于 2019-12-04 17:56:11
问题 I have a very large absorbing Markov chain (scales to problem size -- from 10 states to millions) that is very sparse (most states can react to only 4 or 5 other states). I need to calculate one row of the fundamental matrix of this chain (the average frequency of each state given one starting state). Normally, I'd do this by calculating (I - Q)^(-1) , but I haven't been able to find a good library that implements a sparse matrix inverse algorithm! I've seen a few papers on it, most of them P

Adding two `csc` sparse matrices of different shapes in python

夙愿已清 提交于 2019-12-04 16:41:17
So I have two csc matrices of different shapes that I need to add together. The matrices look like this: current_flows = (7005, 1001) 50.0 (8259, 1001) 65.0 (14007, 1001) 45.0 (9971, 1002) 80.0 : : (69003, 211148) 0.0 result_flows = (7005, 1001) 40 (14007, 1001) 20 (9971, 1002) 35 : : (71136, 71137) 90 final_flows = current_flows + result_flows As illustrated by some of the row and column ID's shown: (7005, 1001), (14007, 1001), (9971, 1002) the matrices do have elements in common. Based on their final row and column ID's though they are of different shape. I would like to add the two matrices

Numpy re-index to first N natural numbers

☆樱花仙子☆ 提交于 2019-12-04 13:47:57
I have a matrix that has a quite sparse index (the largest values in both rows and columns are beyond 130000), but only a few of those rows/columns actually have non-zero values. Thus, I want to have the row and column indices shifted to only represent the non-zero ones, by the first N natural numbers. Visually, I want a example matrix like this 1 0 1 0 0 0 0 0 1 to look like this 1 1 0 1 but only if all values in the row/column are zero. Since I do have the matrix in a sparse format, I could simply create a dictionary, store every value by an increasing counter (for row and matrix separately)

Using pytables, which is more efficient: scipy.sparse or numpy dense matrix?

混江龙づ霸主 提交于 2019-12-04 11:55:47
问题 When using pytables , there's no support (as far as I can tell) for the scipy.sparse matrix formats, so to store a matrix I have to do some conversion, e.g. def store_sparse_matrix(self): grp1 = self.getFileHandle().createGroup(self.getGroup(), 'M') self.getFileHandle().createArray(grp1, 'data', M.tocsr().data) self.getFileHandle().createArray(grp1, 'indptr', M.tocsr().indptr) self.getFileHandle().createArray(grp1, 'indices', M.tocsr().indices) def get_sparse_matrix(self): return sparse.csr

R constructing sparse Matrix

心已入冬 提交于 2019-12-04 10:59:07
I'm reading through instructions of Matrix package in R. But I couldn't understand the p argument in function: sparseMatrix(i = ep, j = ep, p, x, dims, dimnames, symmetric = FALSE, index1 = TRUE, giveCsparse = TRUE, check = TRUE) According to http://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/sparseMatrix.html p: numeric (integer valued) vector of pointers, one for each column (or row), to the initial (zero-based) index of elements in the column (or row). Exactly one of i, j or p must be missing. I figured p is for compressed representation of either the row or column indices because it

Python: how do you store a sparse matrix using python?

我的未来我决定 提交于 2019-12-04 09:45:12
问题 I have got an output using sparse matrix in python, i need to store this sparse matrix in my hard disk, how can i do it? if i should create a database then how should i do?? this is my code: import nltk import cPickle import numpy from scipy.sparse import lil_matrix from nltk.corpus import wordnet as wn from nltk.corpus import brown f = open('spmatrix.pkl','wb') def markov(L): count=0 c=len(text1) for i in range(0,c-2): h=L.index(text1[i]) k=L.index(text1[i+1]) mat[h,k]=mat[h,k]+1//matrix