sparse-matrix

Create CSR matrix from x_index, y_index, value

人盡茶涼 提交于 2019-12-05 07:58:26
问题 I have data of the format (x_index, y_index, value) and I'm trying to create a CSR matrix using scipy (scipy.sparse.csr.csr_matrix). For example, convert: 0 0 10 0 1 5 1 0 3 1 1 4 To the following: 10 5 3 4 I've read the documentation here: http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html However I'm still not clear which of the examples applies to my use case. 回答1: If you can separate the input data into a sequence of row indices, a sequence of column indices

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

蓝咒 提交于 2019-12-05 07:12:24
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 resulting matrix would be symmetric. I would like to know what libraries can achieve the computation

Overloading operator [] for a sparse vector

↘锁芯ラ 提交于 2019-12-05 06:51:38
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 Boost function that expects [] to work correctly. The const version is simple enough: check whether the

scipy sparse matrix to cvxopt spmatrix?

北战南征 提交于 2019-12-05 06:08:20
I need to convert a scipy sparse matrix to cvxopt's sparse matrix format, spmatrix, and haven't come across anything yet (the matrix is too big to be converted to dense, of course). Any ideas how to do this? Mtap1 The more robust answer is a combination of hpaulj's answer and OferHelman's answer . def scipy_sparse_to_spmatrix(A): coo = A.tocoo() SP = spmatrix(coo.data.tolist(), coo.row.tolist(), coo.col.tolist(), size=A.shape) return SP Defining the shape variable preserves the dimensionality of A on SP. I found that any zero columns ending the scipy sparse matrix would be lost without this

convert simple triplet matrix(slam) to sparse matrix(Matrix) in R

百般思念 提交于 2019-12-05 02:25:37
Is there a built-in function in either slam package or Matrix package to convert a sparse matrix in simple triplet matrix form (from slam package) to a sparse matrix in dgTMatrix/dgCMatrix form (from Matrix package) ? And is there a built-in way to access non-zero entries from simple triplet matrix ? I'm working in R Actually, there is a built-in way: simple_triplet_matrix_sparse <- sparseMatrix(i=simple_triplet_matrix_sparse$i, j=simple_triplet_matrix_sparse$j, x=simple_triplet_matrix_sparse$v, dims=c(simple_triplet_matrix_sparse$nrow, simple_triplet_matrix_sparse$ncol)) From my own

R - convert BIG table into matrix by column names

白昼怎懂夜的黑 提交于 2019-12-05 00:48:47
问题 This is an extension to an existing question: Convert table into matrix by column names I am using the final answer: https://stackoverflow.com/a/2133898/1287275 The original CSV file matrix has about 1.5M rows with three columns ... row index, column index, and a value. All numbers are long integers. The underlying matrix is a sparse matrix about 220K x 220K in size with an average of about 7 values per row. The original read.table works just fine. x <- read.table("/users/wallace/Hadoop_Local

Loading Matlab sparse matrix saved with -v7.3 (HDF5) into Python and operating on it

喜夏-厌秋 提交于 2019-12-04 23:41:41
问题 I'm new to python, coming from matlab. I have a large sparse matrix saved in matlab v7.3 (HDF5) format. I've so far found two ways of loading in the file, using h5py and tables . However operating on the matrix seems to be extremely slow after either. For example, in matlab: >> whos Name Size Bytes Class Attributes M 11337x133338 77124408 double sparse >> tic, sum(M(:)); toc Elapsed time is 0.086233 seconds. Using tables: t = time.time() sum(f.root.M.data) elapsed = time.time() - t print

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

谁都会走 提交于 2019-12-04 22:59:47
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 The scipy.sparse.*_matrix has several useful methods, for example, if a is e.g. scipy.sparse.csr_matrix : a.toarray() or a.A - Return a dense ndarray

Does scipy support multithreading for sparse matrix multiplication when using MKL BLAS?

不问归期 提交于 2019-12-04 22:58:17
问题 According to MKL BLAS documentation "All matrix-matrix operations (level 3) are threaded for both dense and sparse BLAS." http://software.intel.com/en-us/articles/parallelism-in-the-intel-math-kernel-library I have built Scipy with MKL BLAS. Using the test code below, I see the expected multithreaded speedup for dense, but not sparse, matrix multiplication. Are there any changes to Scipy to enable multithreaded sparse operations? # test dense matrix multiplication from numpy import * import

Computing N smallest eigenvalues of Sparse Matrix in Python

匆匆过客 提交于 2019-12-04 22:36:58
问题 I'd like to find the N smallest eigenvalues of a sparse matrix in Python. I've tried using the scipy.sparse.linalg.eigen.arpack package, but it is very slow at computing the smallest eigenvalues. I read somewhere that there is a shift-invert mode, but when I try using it, I receive an error message telling me that the shift-invert mode is not yet supported. Any ideas as to how I should proceed? 回答1: SciPy Versions Comparing the documentation of scipy.sparse.linalg.eigs from SciPy v0.9 with