sparse-matrix

Matlab: First Non-zero element of each row or column

≯℡__Kan透↙ 提交于 2019-12-04 03:12:33
For example, A = [ -1 0 -2 0 0 2 8 0 1 0 0 0 3 0 -2 0 -3 2 0 0 1 2 0 0 -4]; how can I get a vector of the first nonzero elements of each row? You can use max : >> [sel, c] = max( A ~=0, [], 2 ); Rows for which sel equalse zero - are all zeros and the corresponding column in c should be ignored. Result: >> [sel c]= max( A~=0, [], 2 ) sel = 1 1 1 1 1 c = 1 1 3 2 1 In order to find the first non-zero row index (for each column) you just need to apply max on the first dimension: >> [sel r] = max( A~=0, [], 1 ); Here is a solution based on accumarray that will work even if a row is all zeros. A = [

R error in glmnet: NA/NaN/Inf in foreign function call

穿精又带淫゛_ 提交于 2019-12-04 03:09:05
问题 I am trying to create a model using glmnet, (currently using cv to find the lambda value) and I am getting an error NA/NaN/Inf in foreign function call (arg 5) . I believe this has something to do with the NA values in my data set, because when I remove all data points with NAs the command runs successfully. I was under the impression that glmnet can handle NA values. I'm not sure where the error is coming from: > res <- cv.glmnet(features.mat, as.factor(tmp[,"outcome"]), family="binomial")

Add column to a sparse matrix

蓝咒 提交于 2019-12-04 02:58:47
When I execute the following code I get a spares matrix: import numpy as np from scipy.sparse import csr_matrix row = np.array([0, 0, 1, 2, 2, 2]) col = np.array([0, 2, 2, 0, 1, 2]) data = np.array([1, 2, 3, 4, 5, 6]) sp = csr_matrix((data, (row, col)), shape=(3, 3)) print(sp) (0, 0) 1 (0, 2) 2 (1, 2) 3 (2, 0) 4 (2, 1) 5 (2, 2) 6 I want to add another column to this sparse matrix so the output is: (0, 0) 1 (0, 2) 2 (0, 3) 7 (1, 2) 3 (1, 3) 7 (2, 0) 4 (2, 1) 5 (2, 2) 6 (2, 3) 6 Basically I want to add another column that has the values 7,7,7. The sparse.hstack used in @Paul Panzer's link is the

Are there any storage optimized Sparse Matrix implementations in C#?

自闭症网瘾萝莉.ら 提交于 2019-12-04 02:12:16
Are there any storage optimized Sparse Matrix implementations in C#? Amirshk There is Math.NET . It has some Spare Matrix implementations . (link is to the old Math.NET site. There is no longer an online version of the documentation). If you are looking for high performance sparse matrix implementation check out NMath from CenterSpace software. Here's a partial list of functionality cut from here on CenterSpace's website. Full-featured structured sparse matrix classes, including triangular, symmetric, Hermitian, banded, tridiagonal, symmetric banded, and Hermitian banded. Functions for

MATLAB - efficient way of computing distances between points in a graph/network using the adjacency matrix and coordinates

半腔热情 提交于 2019-12-04 02:11:38
问题 I have the network representation in a 2D coordinate space. I have an adjacency matrix Adj (which is sparse) and a coordinate matrix with the x,y values of all the points/nodes/vertices in the graph which are drawn. I would like to compute as efficiently as possible the distance between these points. I would like to avoid cycling through the entries in the matrix and computing the pairwise distances one by one. 回答1: [n, d] = size(coordinate); assert(d == 2); resi = sparse(Adj * diag(1:n));

How to add two Sparse Vectors in Spark using Python

夙愿已清 提交于 2019-12-03 21:05:34
问题 I've searched everywhere but I couldn't find how to add two sparse vectors using Python. I want to add two sparse vectors like this:- (1048576, {110522: 0.6931, 521365: 1.0986, 697409: 1.0986, 725041: 0.6931, 749730: 0.6931, 962395: 0.6931}) (1048576, {4471: 1.0986, 725041: 0.6931, 850325: 1.0986, 962395: 0.6931}) 回答1: Something like this should work: from pyspark.mllib.linalg import Vectors, SparseVector, DenseVector import numpy as np def add(v1, v2): """Add two sparse vectors >>> v1 =

How to convert sparse matrix to dense matrix in Eigen

喜欢而已 提交于 2019-12-03 19:05:13
问题 Is there some easy and fast way to convert a sparse matrix to a dense matrix of doubles? Because my SparseMatrix is not sparse any more, but became dense after some matrix products. Another question I have: The Eigen library has excellent performance, how is this possible? I don't understand why, because there are only header files, no compiled source. 回答1: Let's declare two matrices: SparseMatrix<double> spMat; MatrixXd dMat; Sparse to dense: dMat = MatrixXd(spMat); Dense to sparse: spMat =

R - convert BIG table into matrix by column names

荒凉一梦 提交于 2019-12-03 15:47:24
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/reference/DiscoveryData6Mo.csv", header=TRUE); My problem comes when I do the reshape command. reshape

How can I calculate inverse of sparse matrix in Eigen library

被刻印的时光 ゝ 提交于 2019-12-03 14:58:12
I have a question about Eigen library in C++. Actually, I want to calculate inverse matrix of sparse matrix. When I used Dense matrix in Eigen, I can use .inverse() operation to calculate inverse of dense matrix. But in Sparse matrix, I cannot find inverse operation anywhere. Does anyone who know to calculate inverse of sparse matrix? help me. You cannot do it directly, but you can always calculate it, using one of the sparse solvers. The idea is to solve A*X=I , where I is the identity matrix. If there is a solution, X will be your inverse matrix. The eigen documentation has a page about

pseudo inverse of sparse matrix in python

荒凉一梦 提交于 2019-12-03 14:39:38
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 and in every row of the matrix I'll have from 0 up to 4 non zero values. The matrix A size is given by