Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6041811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:42:55+00:00 2026-05-23T06:42:55+00:00

NumPy arrays can be indexed with an array of booleans to select the rows

  • 0

NumPy arrays can be indexed with an array of booleans to select the rows corresponding to True entries:

>>> X = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> rows = np.array([True,False,True])
>>> X[rows]
array([[1, 2, 3],
       [7, 8, 9]])
>>> X[np.logical_not(rows)]
array([[4, 5, 6]])

But this seems not possible with SciPy sparse matrices; the indices are taken as numeric ones, so False select row 0 and True selects row 1. How can I get the NumPy-like behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:42:56+00:00Added an answer on May 23, 2026 at 6:42 am

    You can use np.nonzero (or ndarray.nonzero) on your boolean array to get corresponding numerical indices, then use these to access the sparse matrix. Since “fancy indexing” on sparse matrices is quite limited compared to dense ndarrays, you need to unpack the rows tuple returned by nonzero and specify that you want to retrieve all columns using the : slice:

    >>> rows.nonzero()
    (array([0, 2]),)
    >>> indices = rows.nonzero()[0]
    >>> indices
    array([0, 2])
    >>> sparse[indices, :]
    <2x100 sparse matrix of type '<type 'numpy.float64'>'
            with 6 stored elements in LInked List format>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I build a numpy array out of a generator object? Let me
I have two NumPy arrays, e.g.: a = [1,2,3,4,5] and a filter array, e.g.:
I'm working with 3-dimensional arrays (for the purpose of this example you can imagine
I made a function similar to numpy's array . It converts lists to arrays,
I have four numpy arrays like: X1 = array([[1, 2], [2, 0]]) X2 =
Say that I have 4 numpy arrays [1,2,3] [2,3,1] [3,2,1] [1,3,2] In this case,
According to docs numpy's default behaviour is to index arrays first by rows then
I need to match two very large Numpy arrays (one is 20000 rows, another
If I construct a numpy matrix like this: A = array([[1,2,3],[4,5,6]]) and then type
with numpy arrays, you can use some kind of inequality within the square bracket

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.