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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:50:57+00:00 2026-06-11T10:50:57+00:00

Is it possible to get the length of the nonzero elements in a numpy

  • 0

Is it possible to get the length of the nonzero elements in a numpy array without iterating over the array or masking the array. Speed is the main goal of calculating the length.

Essentially, something like len(array).where(array != 0).

If it changes the answer, each row will begin with zeros. The array is filled on the diagonal with zeros.

  • 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-11T10:50:58+00:00Added an answer on June 11, 2026 at 10:50 am

    Assuming you mean total number of nonzero elements (and not total number of nonzero rows):

    In [12]: a = np.random.randint(0, 3, size=(100,100))
    
    In [13]: timeit len(a.nonzero()[0])
    1000 loops, best of 3: 306 us per loop
    
    In [14]: timeit (a != 0).sum()
    10000 loops, best of 3: 46 us per loop
    

    or even better:

    In [22]: timeit np.count_nonzero(a)
    10000 loops, best of 3: 39 us per loop
    

    This last one, count_nonzero, seems to behave well when the array is small, too, whereas the sum trick not so much:

    In [33]: a = np.random.randint(0, 3, size=(10,10))
    
    In [34]: timeit len(a.nonzero()[0])
    100000 loops, best of 3: 6.18 us per loop
    
    In [35]: timeit (a != 0).sum()
    100000 loops, best of 3: 13.5 us per loop
    
    In [36]: timeit np.count_nonzero(a)
    1000000 loops, best of 3: 686 ns per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to get length of an array without iterating complete array having
How to get the fraction length? If at all possible, without using string operation
Possible Duplicate: Get number of checkboxes that are checked in Javascript obj = document.getElementById('myform').elements.length.checked;
I wanted to follow some excellent C++ advice of calculating the array length once
Possible Duplicate: Get the length of an audio file php i am using php
Possible Duplicate: Get list of all input objects using JavaScript, without accessing a form
Is it possible to get specific bytes from a byte array in java? I
Why is it not possible to get the length of a buffer allocated in
In Linux command using wc -L it's possible to get the length of longest
Possible Duplicate: How to get the length of a function in bytes? I'm making

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.