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 8879777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:01:39+00:00 2026-06-14T20:01:39+00:00

In NumPy, foo = np.array([[i+10*j for i in range(10)] for j in range(3)]) array([[

  • 0

In NumPy,

    foo = np.array([[i+10*j for i in range(10)] for j in range(3)])
    array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
           [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
    filter = np.nonzero(foo > 100)#nothing matches

    foo[:,filter]
    array([], shape=(3, 2, 0), dtype=int64)

    foo[:,0:0]
    array([], shape=(3, 0), dtype=int64)

filter2 = np.nonzero(np.sum(foo,axis=0) < 47)
foo[:,filter2]
array([[[ 0,  1,  2,  3,  4,  5]],

       [[10, 11, 12, 13, 14, 15]],

       [[20, 21, 22, 23, 24, 25]]])
foo[:,filter2].shape
(3, 1, 6)

I have a ‘filter’ condition where I want to perform an operation on all rows for all matching columns, but if filter is an empty array, somehow my foo[:,filter] gets broadcast into a 3D array. Another example is with filter2 -> again, foo[:,filter2] gives me a 3D array when I am expecting the result of foo[:,(np.sum(foo,axis=0) < 47)]

Can someone explain what the proper use case of np.nonzero is compared to using booleans to find the correct columns/indices?

  • 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-06-14T20:01:41+00:00Added an answer on June 14, 2026 at 8:01 pm

    First, foo[filter] == foo[filter.nonzero()] when filter is a Boolean array.

    To understand why you’re getting unexpected results you have to understand a little about how python does indexing. To do multidimensional indexing in python you can either use indices in [], separated by commas or use a tuple. So foo[1, 2, 3] is the same as foo[(1, 2, 3)]. With this in mind take a look at what happens when you do foo[:, something]. I believe in your example you were trying to get foo[:, something[0], something[1]], but instead you got foo[(slice[None], (something[0], something[1]))].

    This is all somewhat academic, because if you’re just using filter for indexing you probably don’t need to use nonzero, just use the boolean array as the index but if you need to, you can do something like:

    foo[:, filter[0]]
    
    # OR
    index = (slice(None),) + filter.nonzero()
    foo[index]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import numpy as np x = np.array(range(10 * 30)).reshape(100, 3) y = np.array(range(1010, 10,
I'm using numpy to create a cube array with sides of length 100, thus
I have a function foo that takes a NxM numpy array as an argument
Is there a numpy function to sum an array along (not over) a given
I have a 2D numpy array as follows: import numpy as np foo =
I have an array like this numpy array dd= [[foo 0.567 0.611] [bar 0.469
How do I get numpy array into python list? looking for ('foo', 1, 2,
In numpy: Foo = array([[ 3.5, 0. , 2.5, 2. , 0. , 1.
I'm having trouble editing values in a numpy array import numpy as np foo
The numpy.equal function does not work if a list or array contains strings: >>>

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.