sparse-matrix

Scipy: Sparse Matrix giving incorrect values

痞子三分冷 提交于 2019-12-24 06:04:11
问题 Below is my code for generating my sparse matrix: import numpy as np import scipy def sparsemaker(X, Y, Z): 'X, Y, and Z are 2D arrays of the same size' x_, row = np.unique(X, return_inverse=True) y_, col = np.unique(Y, return_inverse=True) return scipy.sparse.csr_matrix( (Z.flat,(row,col)), shape=(x_.size, y_.size) ) >>> print sparsemaker(A, B, C) #A, B, and C are (220, 256) sized arrays. (0, 0) 167064.269831 (0, 2) 56.6146564629 (0, 9) 53.8660340698 (0, 23) 80.6529717039 (0, 28) 0.0 (0, 33)

Create sparse circulant matrix in python

末鹿安然 提交于 2019-12-24 04:48:11
问题 I want to create a large (say 10^5 x 10^5) sparse circulant matrix in Python. It has 4 elements per row at positions [i,i+1], [i,i+2], [i,i+N-2], [i,i+N-1] , where I have assumed periodic boundary conditions for the indices (i.e. [10^5,10^5]=[0,0], [10^5+1,10^5+1]=[1,1] and so on). I looked at the scipy sparse matrices documentation but I am quite confused (I am new to Python). I can create the matrix with numpy import numpy as np def Bc(i, boundary): """(int, int) -> int Checks boundary

Multiplying column elements of sparse Matrix

妖精的绣舞 提交于 2019-12-24 04:38:06
问题 I have a sparse csc matrix with many zero elements for which I would like to compute the product of all column elements for each row. i.e.: A = [[1,2,0,0], [2,0,3,0]] should be converted to: V = [[2, 6]] Using a numpy dense matrix this can be accomplished by replacing all zero values with one values and using A.prod(1) . This is however not a option since the dense matrix would be too large. Is there any way to accomplish this without converting the sparse matrix into a dense one? 回答1:

C# Improve parallel performance of Sparse Matrix Row and Column Total Calculations

大兔子大兔子 提交于 2019-12-24 04:35:13
问题 I have a sparse matrix containing roughly 100 million non-zero elements: // [Row][Column][Element] public IDictionary<int, IDictionary<int, decimal>> MyMatrix { get; private set; } Getting the sum of each row is very fast: private void RowSum() { var rowTotals = new ConcurrentDictionary<int, decimal>(); Parallel.ForEach(MyMatrix, (row) => { rowTotals.TryAdd(row.Key, row.Value.Sum(x => x.Value)); }); } Getting the sum of each column is much slower: private void ColumnSum() { var columnTotals =

Error:Maximum variable size allowed by the program is exceeded. while using sub2ind

僤鯓⒐⒋嵵緔 提交于 2019-12-24 04:31:31
问题 Please suggest how to sort out this issue: nNodes = 50400; adj = sparse(nNodes,nNodes); adj(sub2ind([nNodes nNodes], ind, ind + 1)) = 1; %ind is a vector of indices ??? Maximum variable size allowed by the program is exceeded. 回答1: I think the problem is 32/64-bit related. If you have a 32 bit processor, you can address at most 2^32 = 4.294967296e+09 elements. If you have a 64-bit processor, this number increases to 2^64 = 9.223372036854776e+18 Unfortunately, for reasons that are at best

Visualizing a large matrix in matlab

空扰寡人 提交于 2019-12-24 04:11:31
问题 I have a huge sparse matrix (1,000 x 1,000,000) that I cannot load on matlab (not enough RAM). I want to visualize this matrix to have an idea of its sparsity and of the differences of the values. Because of the memory constraints, I want to proceed as follows: 1- Divide the matrix into 4 matrices 2- Load each matrix on matlab and visualize it so that the colors give an idea of the values (and of the zeros particularly) 3- "Stick" the 4 images I will get in order to have a global idea for the

Visualizing a large matrix in matlab

有些话、适合烂在心里 提交于 2019-12-24 04:11:10
问题 I have a huge sparse matrix (1,000 x 1,000,000) that I cannot load on matlab (not enough RAM). I want to visualize this matrix to have an idea of its sparsity and of the differences of the values. Because of the memory constraints, I want to proceed as follows: 1- Divide the matrix into 4 matrices 2- Load each matrix on matlab and visualize it so that the colors give an idea of the values (and of the zeros particularly) 3- "Stick" the 4 images I will get in order to have a global idea for the

Efficiently populate SciPy sparse matrix from subset of dictionary

此生再无相见时 提交于 2019-12-24 03:41:58
问题 I need to store word co-occurrence counts in several 14000x10000 matrices. Since I know the matrices will be sparse and I do not have enough RAM to store all of them as dense matrices, I am storing them as scipy.sparse matrices. I have found the most efficient way to gather the counts to be using Counter objects. Now I need to transfer the counts from the Counter objects to the sparse matrices, but this takes too long. It currently takes on the order of 18 hours to populate the matrices. The

Efficiently multiply a dense matrix by a sparse vector

旧时模样 提交于 2019-12-24 03:22:38
问题 I am looking for an efficient way to multiply a dense matrix by a sparse vector, Av , where A is of size (M x N) and v is (N x 1). The vector v is a scipy.sparse.csc_matrix. I have two methods I use at the moment: In method 1, I pick off the non-zero values in v, say vi, and element-wise multiply vi with the corresponding column of A, then sum up these columns. So if y = Av , then y = A[:, 0]*v0 + ... + A[:, N]*vN , only for the non-zero i. def dense_dot_sparse(dense_matrix, sparse_column):

how to solve a very large overdetermined system of linear equations?

时光毁灭记忆、已成空白 提交于 2019-12-24 03:19:57
问题 I am doing a project about image processing, and I need to solve the following set of equations: Nx+Nz*( z(x+1,y)-z(x,y) )=0 Ny+Nz*( z(x+1,y)-z(x,y) )=0 and equations of the boundary (bottom and right side of the image): Nx+Nz*( z(x,y)-z(x-1,y) )=0 Ny+Nz*( z(x,y)-z(x,y-1) )=0 where Nx,Ny,Nz are the surface normal vectors at the corresponding coordinates and are already determined. Now the problem is that since (x,y) are the coordinates on an image, which typically has a size of say x=300 and