0

Is it possible to select a non consecutive subset of rows of a numpy sparse matrix?

For example, I might want rows 0, 2 and 3, like:

mat[[True, False, True, True, False]]

I've been googling for a while and it looks like doing something like this on a numpy sparse matrix directly is not possible. What would be the most reasonable way of doing this by converting to a different data structure, selecting the subset, and converting back to numpy sparse matrix?

Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89
  • You could use `getrow()` and loop over the row indices you want but I guess that's not quite what you want to do, right? – David Wierichs Jul 03 '20 at 15:55
  • You can multiply by - in your example - the 3x5 matrix which has ones at (0,0) (1,2) and (2,3) and zeros everywhere else. This matrix is easy to construct from your mask using coo or csr format. – Paul Panzer Jul 03 '20 at 16:07
  • @DavidWierichs I'm guessing there would be severe performance issues from looping over rows and then constructing another sparse matrix one row at a time? – Atte Juvonen Jul 03 '20 at 16:11
  • @PaulPanzer If I understand correctly, after that operation I would have a matrix which is the same size as my original matrix, but some rows have been zeroed out? I need to be able to delete those rows entirely, it's not enough to fill their values with zeroes. – Atte Juvonen Jul 03 '20 at 16:14
  • No, only specified rows are retained. You can see that for example from the shapes: 3x5 @ 5x5 -> 3x5 – Paul Panzer Jul 03 '20 at 16:17
  • @PaulPanzer Could you provide a simple example to demonstrate what you mean? – Atte Juvonen Jul 03 '20 at 16:21
  • 1
    Nope, but luckily I already have ;-) https://stackoverflow.com/a/50542206/7207392 – Paul Panzer Jul 03 '20 at 16:25
  • 1
    Doesn't `mat[[0,2,3],:]` work (provided `mat` is in `csr` format)? Under the covers that kind of indexing does use the multiplication that @PaulPanzer describes. Even if boolean indexing doesn't work for sparse, integer array/list indexing does (though slower than with dense arrays). – hpaulj Jul 03 '20 at 16:57
  • @hpaulj Ah, that works! Want to post it as an answer? – Atte Juvonen Jul 03 '20 at 17:18

0 Answers0