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

  • SEARCH
  • Home
  • 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 7936823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:19:49+00:00 2026-06-03T22:19:49+00:00

I’m using numpy.delete to remove elements from an array that is inside a while

  • 0

I’m using numpy.delete to remove elements from an array that is inside a while loop.
This while loop is valid only if the array is not empty. This code works fine but slows down
considerably when the array has over 1e6 elements. Here is an example:

while(array.shape[0] > 0):
     ix = where((array >= x) & (array <= y))[0]
     array = delete(array,ix,None)

I’ve tried to make this code efficient but I cannot find a good way to speed up the while loop. The bottleneck here is, I think, the delete which must involve a copy of some kind. I’ve tried using masked array in order to avoid copying but I’m not that good at python and masked array are not that easy to search. Is there a good and fast way to use delete or replace it so that 7e6 elements can be handled by the loop above without taking 24 hours?

Thanks

  • 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-03T22:19:50+00:00Added an answer on June 3, 2026 at 10:19 pm

    So you can substantially improve the performance of your code by:

    • eliminating the loop; and

    • avoiding the delete operations (which cause a copy of the original
      array)

    NumPy 1.7 introduced a new mask that is far easier to use than the original; it’s performance is also much better because it’s part of the NumPy core array object. I think this might be useful to you because by using it you can avoid the expensive delete operation.

    In other words, instead of deleting the array elements you don’t want, just mask them. This has been suggested in other Answers, but i am suggesting to use the new mask

    to use NA, just import NA

    >>> from numpy import NA as NA
    

    then for a given array, set the maskna flag to True

    >>> A.flags.maskna = True
    

    Alternatively, most array constructors (as of 1.7) have the parameter maskna, which you can set to True

    >>> A[3,3] = NA
    
    array([[7, 5, 4, 8, 4],
           [2, 4, 3, 7, 3],
           [3, 1, 3, 2, 1],
           [8, 2, 0, NA, 7],
           [0, 7, 2, 5, 5],
           [5, 4, 2, 7, 4],
           [1, 2, 9, 2, 3],
           [7, 5, 1, 2, 9]])
    
    >>> A.sum(axis=0)
    array([33, 30, 24, NA, 36])
    

    Often this is not what you want–i.e., you still want the sum of that column with the NA treated as if it were 0:

    To get that behavior, pass in True for the skipma parameter (most NumPy array constructors have this parameter in NumPy 1.7):

    >>> A.sum(axis=0, skipna=True)
    array([33, 30, 24, 33, 36])
    

    In sum, to speed up your code, eliminate the loop and use the new mask:

    >>> A[(A<=3)&(A<=6)] = NA
    
    >>> A
    array([[8, 8, 4, NA, NA],
           [7, 9, NA, NA, 8],
           [NA, 6, 9, 5, NA],
           [9, 4, 6, 6, 5],
           [NA, 6, 8, NA, NA],
           [8, 5, 7, 7, NA],
           [NA, 4, 5, 9, 9],
           [NA, 8, NA, 5, 9]])
    

    The NA placeholders–in this context–behave like 0s, which i believe is what you want:

    >>> A.sum(axis=0, skipna=True)
    array([32, 50, 39, 32, 31])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.