I have a sparse matrix W, when I use `linalg.pinv(W)`

, it throws some errors:

```
Traceback (most recent call last):
File "/Users/ad9075/PycharmProjects/bednmf/test.py", line 14, in testNmfRun
self.factor = factorization(self.V)
File "/Users/ad9075/PycharmProjects/bednmf/nmf.py", line 18, in factorization
W_trans = linalg.pinv(W)
File "/Library/Python/2.7/site-packages/scipy/linalg/basic.py", line 540, in pinv
b = np.identity(a.shape[0], dtype=a.dtype)
IndexError: tuple index out of range`
```

But when I modify it to `linalg.pinv(W.todense())`

, it works well. However, do I really need to convert the sparse matrix if I want to calculate the **generaized inverse**? Does anyone have ideas about this?

Thanks!

Your answer lies here : Good matrix inversion routines in C ( don't let the title disturb you, the discussion is about scipy )

But you have to know the inverse matrix is not necessarily a sparse matrix and, as said by George E. Forsythe and Cleve B. Moler :

"Almost anything you can do with A^{-1} can be done without it"

The inverse (and generalized inverse) of a sparse matrix is usually dense, unless you can permute the rows and columns of the matrix so that it becomes block diagonal.

So your problem splits into two parts: (i) find a permutation that makes it block-diagonal, and (ii) compute the generalized inverse using linalg.pinv separately for each block. If your matrix is small enough, just converting it to a dense matrix first and then computing the pseudoinverse is also efficient.

If you on the other hand want to compute something like "A^{-1} x", using gmres or some other iterative routine may be a more efficient solution.

Similar Questions

Help me to calculate inverse normal distribution in Python which library should i use. scipy? I can't found the function/method to do calculate.

I have downloaded and included UJMP (Universal Java Matrix Package) library to my project for generating sparse matrix. But I could not find any documentation about functions of the library, how to cr

I am working on an algorithm that uses diagonal and first off-diagonal blocks of a large (will be e06 x e06) block diagonal sparse matrix. Right now I create a dict that stores the blocks in such a wa

I need to do a simple curve fitting using scipy (curve_fit). However, my data is in the form of a matrix. I can easily do this in numpy but I wanted to see the goodness of fit for scipy. Problem: AX =

I'm trying to manipulate some data in a sparse matrix. Once I've created one, how do I add / alter / update values in it? This seems very basic, but I can't find it in the documentation for the sparse

I would appreciate any help, to understand following behavior when slicing a lil_matrix (A) from the scipy.sparse package. Actually, I would like to extract a submatrix based on an arbitrary index lis

For a current project I have to compute the inner product of a lot of vectors with the same matrix (which is quite sparse). The vectors are associated with a two dimensional grid so I store the vector

I have to compute massive similarity computations between vectors in a sparse matrix. What is currently the best tool, scipy-sparse or pandas, for this task?

I'm using numpy and scipy. I have a large sparse matrix and I want to find the largest eigenvalue of the sparse matrix. How can I do that?

I am trying to take the inverse of a matrix in Python and keep getting a syntax error. I am new to Python. After doing an internet search and trying multiple things, I am still not getting it. Can som

I just started to learn to program in Python and I am trying to construct a sparse matrix using Scipy package. I found that there are different types of sparse matrices, but all of them require to sto

I'm coding the program that using linked list to store a sparse matrix. First I create a class Node contains the index of entry, value of entry and two pointers to next row and next column. Second I

I have a sparse matrix that I arrived at through a complicated bunch of calculations which I cannot reproduce here. I will try to find a simpler example of this. For now, does anyone know how it might

Possible Duplicates: Defining a matrix as an array of arrays and computation its inverse matrix in C++ Simple 3x3 matrix inverse code (C++) how to find a inverse of matrix in c++ . simple coding jus

I have two dimensional matrix. My matrix is sparse. I am facing performance problem. Can any body please answer that what api or class i can use in java to handle sparse matrix to improve my program p

I have a large matrix M like this M=[A1, Z, Z, Z, Z, Z ; Z, A2, Z, Z, Z, Z ; Z, Z, A3, Z, Z, Z ; Z, Z, Z, A4, Z, Z ; Z, Z, Z, Z, A5, Z ; Z, Z, Z, Z, Z, A6]; A1,A2,A3,A4,A5,A6 are 4×4 real symmetric m

scipy.sparse.issparse is used in this post. Is it possible to specify your own distance function using Scikits.Learn K-Means Clustering? However, I have no idea how it works. I already find the docume

I have a large sparse matrix, implemented as a lil sparse matrix from sci-py. I just want a statistic for how sparse the matrix is once populated. Is there a method to find out this?

I have a matrix of factors in R and want to convert it to a matrix of dummy variables 0-1 for all possible levels of each factors. However this dummy matrix is very large (91690x16593) and very spar

This question already has an answer here: Calculate Matrix Rank using scipy 6 answers I mat a problem when solving inverse of a matrix. Firstly, I use python numpy library to make it, by coding

I am implementing K nearest neighbour algorithm for a very sparse data. I want to calculate the distance between a test instance and each sample in the training set, but I am confused. Because most of

I create a sparse matrix in scala breeze, ie using http://www.scalanlp.org/api/breeze/linalg/CSCMatrix.html. Now I want to get a column slice from it. How to do this? Edit: there are some further requ

I have two large square sparse matrices, A & B, and need to compute the following: A * B^-1 in the most efficient way. I have a feeling that the answer involves using scipy.sparse, but can't for t

I'm having trouble efficiently loading data into a sparse matrix format in R. Here is an (incomplete) example of my current strategy: library(Matrix) a1=Matrix(0,5000,100000,sparse=T) for(i in 1:5000)

I've got a SciPy sparse matrix A, let's say in CSR format, and a vector v of matching length. What's the best way of row-scaling A with v, i.e., performing diag(v) * A?

How do I raise a scipy.sparse matrix to a power, element-wise? numpy.power should, according to its manual, do this, but it fails on sparse matrices: >>> X <1353x32100 sparse matrix of typ

How do you determine if a matrix has an inverse in R? So is there in R a function that with a matrix input, will return somethin like: TRUE (this matrix has inverse)/FALSE(it hasn't ...).

Hello I know there are a lot of questions on sparse matrix multiplication, but many of the answers say to just use libraries. I want to do it without using library functions. So far I've done the easy

I am currently trying to speed up my large sparse (scipy) matrix multiplications. I have successfully linked my numpy installation with OpenBLAS and henceforth, also scipy. I have run these tests with

I have a square matrix with a few tens of thousands rows and columns with only a few 1 beside tons of 0, so I use the Matrix package to store that in R in an efficient way. The base::matrix object can

I want to calculate SVD , but I didn't find good java library for this. Now, I have data store in hashmap, because matrix didn't fit into memory due to the fact that sizes are about 400 000 X 10 000 a

I wanted to calculate the multiplicative inverse of a positive real number without using division. After reading through various pages on Newton Raphson method, i implemented the following code for ca

I really couldn't google it. How to transform sparse matrix to ndarray? Assume, I have sparse matrix t of zeros. Then g = t.todense() g[:10] matrix([[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]])

In a scipy program I'm creating a dia_matrix (sparse matrix type) with 5 diagonals. The centre diagonal the +1 & -1 diagonals and the +4 & -4 diagonals (usually >> 4, but the principle i

Hi i'm trying to figure out how to calculate the inverse of a quaternion. A code example would be awesome. Cheers

There is no inverse command in glsl es 2.0 But I saw that I can 1.0/mat2 . But I fear it'll just divide component wisely. Or not? But if so, is there some trick for this (get 1/det fast)?

Good Afternoon, I am trying to do: scipy.sparse.dia_matrx(x, shape = (x.size, x.size)) but the resulting shape of the matrix is x.size x 1. Am i doing something wrong? Or did i miss something in the

Given a sparse matrix listing, what's the best way to calculate the cosine similarity between each of the columns (or rows) in the matrix? I would rather not iterate n-choose-two times. Say the input

I have a very large Scipy sparse matrix ( CSR_MATRIX ). I just want to know how i can compute the sum of values for each row and also the sum of values for each column of the matrix. I have a code tha

I'm trying to use the the Matrix package to read a MatrixMarket formatted file, but I get back a ngTMatrix. I can't convert it with as since there is no method for converting from ngTMatrix to dgCMatr

My goal is to combine many sparse matrices together to form one large sparse matrix. The only two ideas I've been able to think of are (1) create a large sparse matrix and overwrite certain blocks, (2

I have a CSR matrix: >> print type(tfidf) <class 'scipy.sparse.csr.csr_matrix'> I want to take dot product of two rows of this CSR matrix: >> v1 = tfidf.getrow(1) >> v2 = tfid

I am using the C++ Eigen 3 library in my program. In particular, I need to multiply two Eigen sparse matrix and store the result into another Eigen sparse matrix. However, I noticed that if the some e

I'm calculating the dot product between a scipy.sparse matrix (CSC) and a numpy ndarray vector: >>> print type(np_vector), np_vector.shape <type 'numpy.ndarray'> (200,) >>> pri

How in Matlab we can form a matrix X, 1000 by 1000, which is sparse with, say, 5% of independent Bernoulli +-1 nonzero entries? I.e. such a matrix would have rho = ||X||_0/10^6 = 0.05.

I've found a quite good sparse matrix implementation for c# over http://www.blackbeltcoder.com/Articles/algorithms/creating-a-sparse-matrix-in-net. But as i work in 3d coordinate-system, i need a spar

i am surprised why following algorithm shows me ,that matrix is not invertible,i have input identity matrix,here is my code for calculate teterminant float determinant(float a[5][5],float k) { float s

I normally use matrix[:, i:] It seems not work as fast as I expected.

I have two large, scipy sparse matrices, representing time series data. In the first, each row represents a user's music listening over a number of months (the columns), with each value in the row bei

How can I filter elements of an NxM matrix in scipy/numpy in Python by some condition on the rows? For example, just you can do where(my_matrix != 3) which treats the matrix element-wise, I want to