sparse-matrix

Generating random sparse matrix

不问归期 提交于 2019-12-24 02:18:48
问题 I am dealing with large matrices - on the order of 10^8 columns and 10^3-10^4 rows. Since these matrices are only ones and zeros (over 99% zeros), I think the sparse construction in the Matrix package is appropriate. However, I don't see a way to generate a random matrix like in the example below. Note that non-zero entries are defined by the column probabilities col_prob . set.seed(1) #For reproducibility ncols <- 20 nrows <- 10 col_prob <- runif(ncols,0.1,0.2) rmat <- matrix(rbinom(nrows

sparse_vector template class: How do I clean it up?

谁说胖子不能爱 提交于 2019-12-24 00:56:26
问题 I'm not sure if this is a good question or not — please close it if not. I set out to write (using boost::coordinate_vector as a starting point) a sparse_vector template class that efficiently implements a vector-like interface, but is sparse. It implements all the usual vector operations and a fast sparse iterator that iterates over the set elements. It also has a fast version of rotate . I need this class because I have a write-once-read-many use case, and I use many of these sparse_vectors

is eigen dense-sparse matrix product threaded?

南笙酒味 提交于 2019-12-24 00:54:40
问题 I know that sparse-dense product is threaded according to the documentation: https://eigen.tuxfamily.org/dox/TopicMultiThreading.html What about dense-sparse? 回答1: To be more precise, sparse-dense product is multithreaded only if the left-hand-side sparse matrix is row-major. Conversely, dense-sparse products is multithreaded if the right-hand-side sparse matrix is column-major. 来源: https://stackoverflow.com/questions/49959657/is-eigen-dense-sparse-matrix-product-threaded

Getting a vector of dictionary values in an array, python

不问归期 提交于 2019-12-24 00:38:26
问题 I am trying to get a vector of specific dictionary values which are in a numpy array. Here is what the array looks like: import numpy as np edge_array = np.array( [[1001, 7005, {'lanes': 9, 'length': 0.35, 'type': '99', 'modes': 'cw'}], [1001, 8259, {'lanes': 10, 'length': 0.46, 'type': '99', 'modes': 'cw'}], [1001, 14007, {'lanes': 7, 'length': 0.49, 'type': '99', 'modes': 'cw'}]]) I have a vector for the first two values of each row (i.e. 1001 and 7005 , but I need another vector for the

Why is vector dot product slower with scipy's sparse csr_matrix than numpy's dense array?

蹲街弑〆低调 提交于 2019-12-24 00:16:28
问题 I have a situation in which I need to extract a single row from a sparse matrix and take its dot product with a dense row. Using scipy's csr_matrix, this appears to be significantly slower than using numpy's dense array multiplication. This is surprising to me because I expected that sparse dot product would involve significantly fewer operations. Here is an example: import timeit as ti sparse_setup = 'import numpy as np; import scipy.sparse as si;' + \ 'u = si.eye(10000).tocsr()[10];' + \ 'v

Can I use Eigen sparse matrices for general storage requirements

柔情痞子 提交于 2019-12-23 15:11:23
问题 I need a templated sparse matrix implementation but only to reduce memory footprint, not do any numerical solving. So I tried to use Eigen, even though I don't need the math part. Why ? It just happened to be lying on my machine, and I already had used it a little for other stuff. But I am surely no Eigen expert! Context : I have a type T (say struct T{int a; float b; vector<int> c; }; and I need to store large matrices of this (say more than 1000x1000) and most of the values are null

2D array to represent a huge python dict, COOrdinate like solution to save memory

家住魔仙堡 提交于 2019-12-23 12:50:30
问题 I try to update a dict_with_tuples_key with the data from an array: myarray = np.array([[0, 0], # 0, 1 [0, 1], [1, 1], # 1, 2 [1, 2], # 1, 3 [2, 2], [1, 3]] ) # a lot of this with shape~(10e6, 2) dict_with_tuples_key = {(0, 1): 1, (3, 7): 1} # ~10e6 keys Using an array to store the dict values, (thanks to @MSeifert) we get this: def convert_dict_to_darray(dict_with_tuples_key, myarray): idx_max_array = np.max(myarray, axis=0) idx_max_dict = np.max(dict_with_tuples_key.keys(), axis=0) lens =

Repeat a scipy csr sparse matrix along axis 0

℡╲_俬逩灬. 提交于 2019-12-23 10:26:34
问题 I wanted to repeat the rows of a scipy csr sparse matrix, but when I tried to call numpy's repeat method, it simply treats the sparse matrix like an object, and would only repeat it as an object in an ndarray. I looked through the documentation, but I couldn't find any utility to repeats the rows of a scipy csr sparse matrix. I wrote the following code that operates on the internal data, which seems to work def csr_repeat(csr, repeats): if isinstance(repeats, int): repeats = np.repeat(repeats

Solve sparse upper triangular system

泪湿孤枕 提交于 2019-12-23 09:57:59
问题 I'm trying to figure out how to efficiently solve a sparse triangular system, Au*x = b in scipy sparse. For example, we can construct a sparse upper triangular matrix, Au, and a right hand side b with: import scipy.sparse as sp import scipy.sparse.linalg as sla import numpy as np n = 2000 A = sp.rand(n, n, density=0.4) + sp.eye(n) Au = sp.triu(A).tocsr() b = np.random.normal(size=(n)) We can get a solution to the problem using spsolve, however it is clear that the triangular structure is not

R: Best way to replicate an object of same size and type as other?

半世苍凉 提交于 2019-12-23 05:13:59
问题 Suppose you have some R object, such data.frame and Quanteda DFM sparse matrix. You want to replicate that object of the same size but no need to copy the content. Is there some R command to replicate any object without copying the content? And if yes, do they work over sparse objects and non-sparse objects? 回答1: this will create the same data structure filled with NA data("iris") iris.mt <- iris[0, ] iris.mt[nrow(iris), ] <- NA str(iris.mt) 'data.frame': 150 obs. of 5 variables: $ Sepal