I am trying to subsample a scipy sparse matrix as a numpy matrix like this to get every 10th row and every 10th column:

```
connections = sparse.csr_matrix((data,(node1_index,node2_index)),
shape=(dimensions,dimensions))
connections_sampled = np.zeros((dimensions/10, dimensions/10))
connections_sampled = connections[::10,::10]
```

However, when I run this and and query the shape of connections_sampled, I get the original dimensions of connections instead of dimensions that have been reduced by a factor of 10.

Does this type of subsampling now work with sparse matrices? It seems to work when I use smaller matrices, but I can't get this to give the correct answer.

You cannot sample every 10th row and column of a CSR matrix, not in Scipy 0.12 at least:

```
>>> import scipy.sparse as sps
>>> a = sps.rand(1000, 1000, format='csr')
>>> a[::10, ::10]
Traceback (most recent call last):
...
ValueError: slicing with step != 1 not supported
```

You can do it, though, by converting first to a LIL format matrix:

```
>>> a.tolil()[::10, ::10]
<100x100 sparse matrix of type '<type 'numpy.float64'>'
with 97 stored elements in LInked List format>
```

As you see, the shape is updated correctly. If you want a numpy array, not a sparse matrix, try:

```
>>> a.tolil()[::10, ::10].A
array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]])
```

Similar Questions

Suppose I have a matrix in the CSR format, what is the most efficient way to set a row (or rows) to zeros? The following code runs quite slowly: A = A.tolil() A[indices, :] = 0 A = A.tocsr() I had to

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

Say that I have a sparse matrix in scipy.sparse format. How can I extract a diagonal other than than the main diagonal? For a numpy array, you can use numpy.diag. Is there a scipy sparse equivalent? F

I can define a sparse Matrix using a vector for i, j, and x: i <- c(1,3:8) j <- c(2,9,6:10) x <- 7 * (1:7) (A <- sparseMatrix(i, j, x = x)) I want to extract the i, j, and x elements from

I have a simple search engine which echo's out results in a table with CSS formatting, this is working fine, however, i would now like to echo a different image after every 10 results. thankyou to any

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

What is the compact way of storing a sparse matrix that allows to iterate over each row and each column efficiently?

I have a loop that in each iteration gives me a column c of a sparse matrix N. To assemble/grow/accumulate N column by column I thought of using N = scipy.sparse.hstack([N, c]) To do this it would b

I have a scipy.sparse.csr.csr_matrix which is the output from TfidfVectorizer() class. I know I can access the individual components of this matrix in this manner: So if I have this matrix here: tf_id

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

Is there a well-vectorized way to take the product of all the nonzero elements in each column of a sparse matrix in octave (or matlab) (returning a row-vector of products)?

I got a problem when using octave sparse matrix. max(speye(65536)(:)) will result in a 0x0 variable. However, speye(65535) and speye(65537) works. How that happens? My octave version is 3.2.4 in Fedo

When dealing with sparse matrices, how do I convert Matrix Market format into CRS (Compressed Row Storage)?

I have a m x n matrix A, with n > m, and I am trying to identify independent rows by means of the row echelon form of it. Function scipy.linalg.lu returns a PLU factorization of my matrix, but U fa

I need to hold a 50,000x50,000 sparse matrix/2d-array, with ~5% of the cells, uniformly distributed, being non-empty. I will need to: edit I need to do this in numpy/scipy, sorry if wasn't clear. Also

I am examining java version sparse matrix multiplication program which is from JGF benchmark. I run this program in many kinds of cpu frequency. I also do some profile for this program. I classify it

I ran into the following issue trying to vstack two large CSR matrices: /usr/lib/python2.7/dist-packages/scipy/sparse/coo.pyc in _check(self) 229 raise ValueError('negative row index found') 230 if s

I was trying to iterate over the non zero elements of a row major sparse matrix, such as shown below: Eigen::SparseMatrix<double,Eigen::RowMajor> Test(2, 3); Test.insert(0, 1) = 34; Test.insert

There are at least two sparse matrix packages for R. I'm looking into these because I'm working with datasets that are too big and sparse to fit in memory with a dense representation. I want basic lin

I have a N*N matrix: N=3 x = scipy.sparse.lil_matrix( (N,N) ) for _ in xrange(N): x[random.randint(0,N-1),random.randint(0,N-1)]=random.randint(1,100) Assume the matrix looks as below: X Y Z X 0 [2,

I have a sparse matrix represented as > (f <- data.frame(row=c(1,2,3,1,2,1,2,3,4,1,1,2),value=1:12)) row value 1 1 1 2 2 2 3 3 3 4 1 4 5 2 5 6 1 6 7 2 7 8 3 8 9 4 9 10 1 10 11 1 11 12 2 12 Here

To my understanding, numpy.sparse.csr_sparse.dot(other) does multiply other to my sparse matrix from the right: A = numpy.sparse.csr_sparse(something) B = numpy.matrix(something) C = A.dot(B) # C = A*

I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat random, and I can't count on all the values being a set distance away from the diagonal. However, it is still sparse, and I w

I want to sum up each row and column in matrix and check whether all of it are the same. I want to use sum of row and transpose method to do it, but I had no ideas how it can check whether sum of ever

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 have sparse vectors with dimensionalities of around 200.000. I also have a matrix with the same amount of columns, and the same amount of rows as the number of vectors. I want to set all of these in

I've got a matrix (mat1), say 100 rows and 100 columns; I want to create another matrix where every row is the same as the 1st row in mat1 (except that I want to keep the 1st col as the original value

What's the best way to represent a sparse data matrix in PostgreSQL? The two obvious methods I see are: Store data in a single a table with a separate column for every conceivable feature (potentiall

Given a scipy.sparse.crs_matrix, I would like to extract the submatrix that in Numpy's dense algebra would be expressed as A[0::2, 0::2] i.e., A_{new}(i,j) = A(2*i,2*j) (chessboard black-squares ma

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_matri

I want to plot a sparse matrix in an imagesc type of style (one color for each pixel, and not symbols a la scatter). The matrix consists of blobs that are spread ut over a 10000x10000 square. I expect

I have three lists namely A , B , C All these lists contain 97510 items . I need to create a sparse matrix like this matrix[A[0]][B[0]] = C[0] For example , A=[1,2,3,4,5] B=[7,8,9,10,11] C=[14,15,1

I have a large 500x53380 sparse matrix and trying to dichotomize it. I have tried using event2dichot under sna package but no success because it requires an adjacency matrix or network object. I al

How can you take the log base 10 of every element in a sparse matrix (COO)? >>print type(X) <class 'scipy.sparse.coo.coo_matrix'> I've tried this but it doesn't work: import math X.data =

Original matrix below: 3 -1 4 0 2 1 Looking to get a vector containing the values: 3, 2. Only the elements in each row which are greater than 0, and only for the first column. Assume all rows contain

I am using scipy.sparse.linalg.cg to solve a large, sparse linear system, and it works fine, except that I would like to add a progress report, so that I can monitor the residual as the solver works.

Basically, I am just trying to do a simple matrix multiplication, specifically, extract each column of it and normalize it by dividing it with its length. #csc sparse matrix self.__WeightMatrix__ = s

I'm creating a little blog layout and I want every 4th and 5th, 10th and 11th, 14th and 15th, etc. post to have a different class. So basically there'll be 3 regular posts, then 2 with this new class,

I want to augment the scipy.sparse.csr_matrix class with a few methods and replace a few others for personal use. I am making a child class which inherits from csr_matrix, as such: class SparseMatrix(

I have two large sparse matrices: In [3]: trainX Out[3]: <6034195x755258 sparse matrix of type '<type 'numpy.float64'>' with 286674296 stored elements in Compressed Sparse Row format> In [

I don`t know how to solve this problem in Fundamentals of data structure in C ed.2nd ch2.5 On a computer with w bits per word, how much storage is needed to represent a sparse matrix, A, with t nonzer

I'm trying to write a spectral clustering algorithm using NumPy/SciPy for larger (but still tractable) systems, making use of SciPy's sparse linear algebra library. Unfortunately, I'm running into sta

I have to perform this operation: N = A'*P*A The structure of the P matrix is block diagonal while the A matrix is largely sparse (also in a banded structure). The multiplication is performed in blo

What is the state of the art for fastest linear solver for sparse, positive semi definite and strictly diagonally dominant matrix with N varies from ~700 to ~3000, and about a 1/16 of the matrix is no

I need to read in a CSV file with no headers and with an unknown number of columns and rows. However , every other column belongs in one matrix while the next needs to be in a different matrix. Exampl

Here is another noob question. I have a file like this John 30 Mike 0.0786268 Tyson 0.114889 Gabriel 0.176072 Fiona 0.101895 I need to shift every second row to a new column so it should look like

This Sparse Matrix and its 3-Tuple representation is not getting into my head... Either its bit tricky or my resources from where I am studying are really not that good... here is the URI Sparse Matri

I have a matrix, named as p_c_w having dimensions 6X7599 and the other matrix named as p_w having dimensions 1X7599. I wish to have their element-wise multiplication but I am unable to do that. For si

I am implementing a sparse matrix class in compressed row format. This means i have a fixed number of rows and each row consists of a number of elements (this number can be different for different row

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