sparse-matrix

How to convert a sparse matrix into a matrix of index and value of non-zero element

被刻印的时光 ゝ 提交于 2019-11-28 11:58:44
We can construct a sparse matrix from an index and value of non-zero element with the sparseMatrix or spMatrix . Is there any function convert a sparse matrix back to an index and value of all non-zero element? For example i <- c(1,3,5); j <- c(1,3,4); x <- 1:3 A <- sparseMatrix(i, j, x = x) B <- sparseToVector(A) ## test case: identical(B,cbind(i,j,x)) Is there any function do a similar job as sparseToVector ? summary(A) # 5 x 4 sparse Matrix of class "dgCMatrix", with 3 entries # i j x # 1 1 1 1 # 2 3 3 2 # 3 5 4 3 which you can easily pass to as.data.frame or as.matrix : sparseToVector <-

Sum over rows in scipy.sparse.csr_matrix

时间秒杀一切 提交于 2019-11-28 11:44:19
I have a big csr_matrix and I want to add over rows and obtain a new csr_matrix with the same number of columns but reduced number of rows. (Context: The matrix is a document-term matrix obtained from sklearn CountVectorizer and I want to be able to quickly combine documents according to codes associated with these documents) For a minimal example, this is my matrix: import numpy as np from scipy.sparse import csr_matrix from scipy.sparse import vstack row = np.array([0, 4, 1, 3, 2]) col = np.array([0, 2, 2, 0, 1]) dat = np.array([1, 2, 3, 4, 5]) A = csr_matrix((dat, (row, col)), shape=(5, 5))

Efficient way to solve for X in AX=B in MATLAB when both A and B are big matrices

梦想的初衷 提交于 2019-11-28 10:19:24
I have this problem which requires solving for X in AX=B . A is of the order 15000 x 15000 and is sparse and symmetric. B is 15000 X 7500 and is NOT sparse. What is the fastest way to solve for X? I can think of 2 ways. Simplest possible way, X = A\B Using for loop, invA = A\speye(size(A)) for i = 1:size(B,2) X(:,i) = invA*B(:,i); end Is there a better way than the above two? If not, which one is best between the two I mentioned? angainor First things first - never, ever compute inverse of A. That is never sparse except when A is a diagonal matrix. Try it for a simple tridiagonal matrix. That

How can I create a series of months to join sparse data to?

本秂侑毒 提交于 2019-11-28 09:33:44
I think this is a pretty common issue, but I don't know what the process is called, so I'll describe it with an example. The concept is that I want to join a sparse dataset to a complete series, such as the days of the week, months of the year, or any ordered set (for example, for ranking). Empty positions in the sparse data will show as NULL alongside the complete series. Let's say I run the following query in SQL Server to find out monthly sales. SELECT YEAR([timestamp]), MONTH([timestamp]), COUNT(*) FROM table1 WHERE YEAR([timestamp]) = YEAR(GETDATE()) GROUP BY YEAR([timestamp]), MONTH(

Using SparseTensor as a trainable variable?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 09:31:52
I'm trying to use SparseTensor to represent weight variables in a fully-connected layer. However, it seems that TensorFlow 0.8 doesn't allow to use SparseTensor as tf.Variable. Is there any way to go around this? I've tried import tensorflow as tf a = tf.constant(1) b = tf.SparseTensor([[0,0]],[1],[1,1]) print a.__class__ # shows <class 'tensorflow.python.framework.ops.Tensor'> print b.__class__ # shows <class 'tensorflow.python.framework.ops.SparseTensor'> tf.Variable(a) # Variable is declared correctly tf.Variable(b) # Fail By the way, my ultimate goal of using SparseTensor is to permanently

Sparse vs Normal Array Matlab

℡╲_俬逩灬. 提交于 2019-11-28 09:14:25
In Matlab, at what point is having a sparse array better than a normal array if I still have a lot of calculations to do on it, and about 25% of the array are non-zeros? Personally, I'd rarely bother with sparse for an array that is only 25% non-zeros. If you don't believe me, try it yourself. A = sprand(2000,2000,0.25); tic,B = A*A;toc Elapsed time is 1.771668 seconds. Af = full(A); tic,B = Af*Af;toc Elapsed time is 0.499045 seconds. The extra work involved with this as a sparse matrix costs too much to be worth the bother. Now try it with a really sparse matrix. A = sprand(2000,2000,0.005);

k-means clustering in R on very large, sparse matrix?

一个人想着一个人 提交于 2019-11-28 07:46:10
I am trying to do some k-means clustering on a very large matrix. The matrix is approximately 500000 rows x 4000 cols yet very sparse (only a couple of "1" values per row). The whole thing does not fit into memory, so I converted it into a sparse ARFF file. But R obviously can't read the sparse ARFF file format. I also have the data as a plain CSV file. Is there any package available in R for loading such sparse matrices efficiently? I'd then use the regular k-means algorithm from the cluster package to proceed. Many thanks The bigmemory package (or now family of packages -- see their website

csv to sparse matrix in python

时光总嘲笑我的痴心妄想 提交于 2019-11-28 07:45:30
I have a big csv file which lists connections between nodes in a graph. example: 0001,95784 0001,98743 0002,00082 0002,00091 So this means that node id 0001 is connected to node 95784 and 98743 and so on. I need to read this into a sparse matrix in numpy. How can i do this? I am new to python so tutorials on this would also help. Example using lil_matrix (list of list matrix) of scipy. Row-based linked list matrix. This contains a list ( self.rows ) of rows, each of which is a sorted list of column indices of non-zero elements. It also contains a list ( self.data ) of lists of these elements.

How to get sparse matrices into H2O?

我的未来我决定 提交于 2019-11-28 07:39:34
问题 I am trying to get a sparse matrix into H2O and I was wondering whether that was possible. Suppose we have the following: test <- Matrix(c(1,0,0,1,1,1,1,0,1), nrow = 3, sparse = TRUE) and assuming my local H2O is localH2O , I can't seem to do the following: as.h2o(test) It gives the error: cannot coerce class "structure("dgCMatrix", package = "Matrix")" to a data.frame . That seems to be pretty logical, however assuming that test is so big that I can't transform it into a dataframe, how am I

How to perform efficient sparse matrix multiplication by using tf.matmul?

浪子不回头ぞ 提交于 2019-11-28 06:18:56
问题 I'm trying to perform a sparse matrix multiplication by using tf.matmul(). However, the inference speed is much more slower than dense matrix multiplication. According to the description in tf.sparse_matmul() : The breakeven for using this versus a dense matrix multiply on one platform was 30% zero values in the sparse matrix. Thus , I make the sparse matrix with 7/8 zero values. Here is my code: import tensorflow as tf import numpy as np import time a = tf.Variable(np.arange(1000).reshape