I have encountered a difference in how slicing a scipy sparse matrix works in 0.10.0 and 0.10.1. Consider the following piece of code:

```
from numpy import array, ravel
from scipy.sparse import csr_matrix
mat = csr_matrix(array([[1, 0, 0], [0,1,0], [1,0,0]]))
desired_cols = ravel(mat.sum(0)) > 0
print mat[:, desired_cols].A
```

In scipy 0.10.0, I get what I expect to get:

```
[[1 0]
[0 1]
[1 0]]
```

In 0.10.1 and 0.12.0, I get

```
[[0 0 1]
[1 1 0]
[0 0 1]]
```

I am not sure if this is a bug or I am doing something wrong. I get the same results using `coo_matrix`

and `csc_matrix`

.

I am trying to remove all rows that sum to 0 from the matrix. I understand that `csr_matrix`

does not support efficient column slicing and I should not being this.

What is desired_cols in these cases. In recent scipy (0.13.0) the results match your first one (0.10.0). You may have to dig into the github source for scipy if you want to track down changes this far back in the versions.

Use `np.flatnonzero(desired_cols)`

instead of `desired_cols`

and `scipy.sparse`

will support it. Full matrix API support is unavailable in `scipy.sparse`

and features are gradually being introduced.

Similar Questions

I created a compressed sparse matrix, but while accessing to a positive index it complains that the index is negative: import scipy.sparse as sparse B= sparse.csc_matrix((110111213141516, 25)) B[11011

In Numpy, ix_() is used to grab rows and columns of a matrix, but it doesn't seem to work with sparse matrices. For instance, this code works because it uses a dense matrix: >>> import numpy

What's the best way to apply something like scipy.misc.logsumexp to a sparse matrix (for instance a scipy.sparse.csr_matrix), specifying one axis? The point is to leave the zeros out from the computat

I see 2 implementations of sparse matrix in this package. OpenMapRealMatrix SparseFieldMatrix Both are documented as Sparse matrix implementation based on an open addressed map. Do you know what a

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

While solving a machine learning problem using scikit (python) I need to do scaling of scipy.sparse matrix before doing the training using SVM in order to achieve higher accuracy. But its clearly ment

Using the Matrix package I can create a two-dimensional sparse matrix. Can someone suggest a package that would allow me to create a multidimensional (specifically a 3-dimensional) sparse matrix (arra

I have a large sparse matrix X in scipy.sparse.csr_matrix format and I would like to multiply this by a numpy array W making use of parallelism. After some research I discovered I need to use Array in

So, I'm doing some Kmeans classification using numpy arrays that are quite sparse-- lots and lots of zeroes. I figured that I'd use scipy's 'sparse' package to reduce the storage overhead, but I'm a l

I want to know how to add sparse matrices in python. I have a program that breaks a big task into subtasks and distributes them across several cpus. Each subtask yields a result (a scipy sparse matrix

Does anyone know that how to use scipy.sparse package to compute SVD on sparse matrix? I know that I need to use scipy.sparse.linalg.svds(). But I did as bellow: from scipy.sparse import * csr = csr_

I have been given this 63521x63521 real sparse symmetric matrix in MATLAB and for some reason it seems to be behaving weirdly for some commands. I am not sure if there is a 'defect' in the matrix fil

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 have a large sparse matrix (lil) implemented in Python with scipy, which comprises of Users on one axis, and songs they played on the other. So each row is a linked list of the songs that user has p

I want to use sparse matrices for BOW feature representation. I have experimented with coo_matrix from scipy, but it doesn't seem to support what I want to do: I would like to initialize a matrix of a

I need to overload [] operator in class Sparse Matrix. This operator must work like in 2D table access. For example tab[1][1], return reference. The problem is I have a vector of elements(struct). tem

type(A) <class 'scipy.sparse.csc.csc_matrix'> A.shape (8529, 60877) print A[0,:] (0, 25) 1.0 (0, 7422) 1.0 (0, 26062) 1.0 (0, 31804) 1.0 (0, 41602) 1.0 (0, 43791) 1.0 print A[1,:] (0, 7044) 1.0

I am working with an extremely large data set in a sparse matrix format. The data has the filing format (3 tab separated columns, where the string in the first column corresponds to a row, the string

We have an application that stores a sparse matrix. This matrix has entries that mostly exist around the main diagonal of the matrix. I was wondering if there were any efficient algorithms (or existin

I'm trying to implement a function in NumPy/Scipy to compute Jensen-Shannon divergence between a single (training) vector and a large number of other (observation) vectors. The observation vectors are

Suppose I have the following matrix in python: [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]] I want to slice it into the following matrices (or quadrants/corners): [[1,2], [5,6]] [[3,4], [7,8]]

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

Can anybody suggest a good C++ library for storing Multi-dimensional Sparse Matrix that focuses on the compression of data in matrix. The number of dimensions of the matrix will be huge (say, 80 dimen

I am trying to do some (k-means) clustering on a very large matrix. The matrix is approximately 500000 rows x 4000 cols yet very sparse (only a couple of 1 values per row). I want to get around 2000

I'm doing a project and I'm doing a lot of matrix computation in it. I'm looking for a smart way to speed up my code. In my project, I'm dealing with a sparse matrix of size 100Mx1M with around 10M n

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

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 process rather large matrices in Python/Scipy. I need to extract rows from large matrix (which is loaded to coo_matrix) and use them as diagonal elements. Currently I do that in the following fashio

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

I am using Scipy to construct a large, sparse (250k X 250k) co-occurrence matrix using scipy.sparse.lil_matrix. Co-occurrence matrices are triangular; that is, M[i,j] == M[j,i]. Since it would be high

I have a list of sparse vectors (in R). I need to convert this list to a sparse matrix. Doing it via a for-loop takes a long time. sm<-spMatrix(length(tc2),n.col) for(i in 1:length(tc2)){ sm[i,]<

I am starting dealing with sparse matrices so I'm not really proficient on this topic. My problem is, I have a simple coo-occurrences matrix from a word list, just a 2-dimensional co-occurrence matrix

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

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_,

How can a sparse matrix - matrix product be calculated? I know the 'classic' / mathematical way of doing it, but it seems pretty inefficient. Can it be improved? I thought about storing the first matr

I have a float matrix of 1024x1024 and I want to keep sign of this matrix inside a file. For this purpose, I want to keep the sign matrix as Matrix of boolean which I fail to do. Assume, my matrix is:

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

Say I have a square NxN crs matrix in spicy. I'd like to restrict that matrix to a subspace, by keeping M indices (given by an array of N booleans, M of which are true). This should give me an MxM spa

I recently encountered difficulty when converting a coo_matrix to a dense matrix using scipy. I have a dtype float16 sparse matrix and attempt to convert it to a dense matrix. The error complains abou

what's the best suitable data structure to use in C for sparse dynamic matrix. I know about the Yale format but it's for static matrices. I need to be able to add rows column and values in it. Thanks

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

Imagine a simple 3x3 matrix upon which I impose the following boundary conditions. Here are an array showing the corresponding indexes if flattened, and the boundary value. I = B = 0 3 6 3 0 1 1 4 7

I have a Table in MySQL with three columns: row-index, column-index and value, which I want to read in into a scipy csr_matrix. I use the Python-MySQL connector. There are 112,500 non-zero elements. T

What do you think? What would be faster and how much faster: Doing sparse matrix (CSR) multiplication (with a vector) on the GPU or the CPU (multithreaded)?

I would like to get the minimum nonzero values per row in a sparse matrix. Solutions I found for dense matrices suggested masking out the zero values by setting them to NaN or Inf. However, this obvio

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'm trying to optimize a piece of code that solves a large sparse nonlinear system using an interior point method. During the update step, this involves computing the Hessian matrix H, the gradient g,

Finding the maximum sum subrectangle in an NxN matrix can be done in O(n^3) time using 2-d kadane's algorithm, as pointed out in other posts. However, if the matrix is sparse, specifically O(n) non-ze

I noticed on Google Summer of Code 2013 that a possible project was implement sparse matrix support for Decision Trees and ensemble methods. Out of curiosity, did this project get anywhere? I really n

I want to create a custom optimized matrix operation (a smart kronecker product based on what I know about the sparse matrices i'm using) using MathNet.numerics for csharp. Is there an accessor to get