Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Delete whole 3d array if contains any nan

I have a set of images represented by a 3d ndarray. Abstractly what I want to do is, delete an entire image if any of its pixel values is a nan. Imagine we got the following ndarray:

a = np.arange(18).reshape(3, 2, 3)
a = 1.0 * a
a[0][0][1] = np.nan
a[1][0][0] = np.nan
a
[[[ 0. nan  2.]
  [ 3.  4.  5.]]

 [[nan  7.  8.]
  [ 9. 10. 11.]]

 [[12. 13. 14.]
  [15. 16. 17.]]]

Now what I want to get is a function that given that ndarray returns True, True, False. In order to finally use np.delete.

I have try the following, which works:

np.delete(a, [np.isnan(image.flatten()).any() for image in a], axis=0)
array([[[12., 13., 14.],
        [15., 16., 17.]]]))

However, I find it hard to believe that there isn't a function in numpy that is more efficient, and since I have a lot of images I would like to optimise it as much as possible.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As already answered by Michael Szczesny, more Pythonic way would be:

filtered_images=a[~np.isnan(a).any(axis=(2,1))]

If that piece of code is hard to understand, then consider extracting each image with a for loop as follows:

filtered_images=list()
for value in a:
  if(np.isnan(value).any()!=True):
    filtered_images.append(value)

Both approaches should give you similar output!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!