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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:45:45+00:00 2026-06-09T17:45:45+00:00

If I have this numpy array: >>> a array([[ 1, 2, 3], [ 4,

  • 0

If I have this numpy array:

>>> a
array([[ 1,  2,  3],
       [ 4,  4,  6],
       [ 4, 10,  9]])

What’s the fastest way to select out of it all the rows where a condition holds true of at least N many elements?

For example select all the rows where at least two numbers are evenly divisible by 2.

The solution I came up with is:

@ find rows where 2 or more elements are evenly divisible by two
N = 2  
a[where(array(map(lambda x: sum(x), a % 2 == 0)) >= N)]

An alternative solution using apply_along_axis is:

a[where(sum(numpy.apply_along_axis(lambda x: x % 2 == 0, 1, a), axis=1) >= 2)]

Is there a more elegant/faster way in numpy/scipy than these? If not, which of the above two is best?

  • 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-09T17:45:46+00:00Added an answer on June 9, 2026 at 5:45 pm

    I’d probably do

    In [29]: timeit a[(a % 2 == 0).sum(axis=1) >= 2]
    10000 loops, best of 3: 29.5 us per loop
    

    which works because True/False have integer values of 1/0. For comparison:

    In [30]: timeit a[where(array(map(lambda x: sum(x), a % 2 == 0)) >= N)]
    10000 loops, best of 3: 72 us per loop
    
    In [31]: timeit a[where(sum(apply_along_axis(lambda x: x % 2 == 0, 1, a), axis=1) >= 2)]
    1000 loops, best of 3: 220 us per loop
    

    Note that using lambdas costs you a lot of the benefits of using numpy in the first place, and lambda x: sum(x) is simply a more verbose and slower way of writing sum here anyway.

    Also note that if the array were large, it’d probably be more efficient to use a method which could short-circuit rather than the above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
I have a numpy array like this a = np.array(1) Now if I want
I have a 2-dimensional numpy array that looks like this: [[a b c] [d
I have a very large NumPy array: a = np.array . From this array
Is is possible to have a 3-D record array in numpy? (Maybe this is
This is a simplification of my question. I have a numpy array: x =
How does one add rows to a numpy array? I have an array A:
I have a NumPy array of values. I want to count how many of
I have a 2d numpy array Something like this: [[ 1 2 3 4],
I have a 2D numpy array. Some of the values in this array are

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.