The Python Oracle

Delete whole 3d array if contains any nan

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping

--

Chapters
00:00 Delete Whole 3d Array If Contains Any Nan
00:39 Accepted Answer Score 1
01:01 Thank you

--

Full question
https://stackoverflow.com/questions/7107...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #numpyndarray

#avk47



ACCEPTED ANSWER

Score 1


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!