Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] [Solved] NumPy RuntimeWarning: All-NaN slice encountered

#1
[Solved] NumPy RuntimeWarning: All-NaN slice encountered

Problem Formulation


You use NumPy’s np.nanmedian() function in your code that is supposed to ignore NaN values when computing the mean of a NumPy array.

import numpy as np
a = np.array([np.NaN, np.NaN])
mean = np.nanmedian(a)

But when using it, NumPy raises a RuntimeWarning: All-NaN slice encountered message:

Warning (from warnings module): File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\nanfunctions.py", line 1114 overwrite_input=overwrite_input)
RuntimeWarning: All-NaN slice encountered

What is the reason for this warning and how to fix it?

Solution + Explanation


The reason this warning arises is because you apply the np.nanmedian() function on an empty array. The function doesn’t cause an error if the array has at least one non-NaN value:

>>> np.nanmedian([0.42, np.NaN, np.NaN])
0.42

If at least one element is a numerical value, the mean is clearly defined: get the median of all not NaN elements.

However, if all elements in a NumPy array are np.NaN, NumPy raises the RuntimeWarning:

import numpy as np
a = np.array([np.NaN, np.NaN])
mean = np.nanmedian(a)
print(mean) '''
OUTPUT:
Warning (from warnings module): File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\nanfunctions.py", line 1114 overwrite_input=overwrite_input)
RuntimeWarning: All-NaN slice encountered
nan '''

Yet, you can also see that it still generates the return value: not-a-number or nan.

As this border case is properly defined and unambiguous, this has caused some programmers to ask whether it makes even sense to issue this warning.

? In my opinion, issuing a warning doesn’t make a lot of sense in the case of the np.nanmedian() function. From Python’s Zen of Python, we know that “explicit is better than implicit”. So, either raise an exception and let the programmer handle it directly or just let it go through if everything is properly defined.

If, like me, you’re annoyed by this warning, you can simply suppress it:

How to Suppress RuntimeWarning?


The context manager warnings.catch_warnings suppresses the warning, but only if you indeed anticipate it coming. Otherwise, you may miss some additional RuntimeWarnings you didn’t see coming.

import numpy as np
import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore", category=RuntimeWarning) median = np.nanmedian([np.NaN, np.NaN]) print(median)

The output is without warning:

nan

However, you need to be sure that this is the only warning that can appear in the with block environment. Otherwise, you may miss some important warnings as already discussed.

A safer way would probably be to use warnings.filterwarnings(action='ignore', message='All-NaN slice encountered') to let through not anticipated RuntimeWarnings.

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post [Solved] NumPy RuntimeWarning: All-NaN slice encountered first appeared on Finxter.



https://www.sickgaming.net/blog/2021/03/...countered/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] [Solved] TypeError: ‘int’ Object Is Not Subscriptable in Python xSicKxBot 0 22 03-31-2021, 08:56 PM
Last Post: xSicKxBot
  [Tut] [Solved] TypeError: ‘Module’ Object Is Not Callable in Python xSicKxBot 0 18 03-25-2021, 08:09 PM
Last Post: xSicKxBot
  [Tut] np.ployfit() — Curve Fitting with NumPy Polyfit xSicKxBot 0 63 11-18-2020, 04:27 AM
Last Post: xSicKxBot
  [Tut] NumPy argpatition() xSicKxBot 0 157 07-23-2020, 08:20 AM
Last Post: xSicKxBot
  [Tut] NumPy polymulx() xSicKxBot 0 133 07-18-2020, 12:30 PM
Last Post: xSicKxBot
  [Tut] numpy.char.capitalize xSicKxBot 0 152 07-17-2020, 11:22 AM
Last Post: xSicKxBot
  [Tut] How to Get a List Slice with Arbitrary Indices in Python? xSicKxBot 0 137 06-18-2020, 03:52 PM
Last Post: xSicKxBot
  [Tut] How to Convert a List to a NumPy Array? xSicKxBot 0 173 06-15-2020, 08:50 AM
Last Post: xSicKxBot
  [Tut] Python Join List Slice xSicKxBot 0 155 06-14-2020, 06:37 AM
Last Post: xSicKxBot
  [Tut] How to Convert List of Lists to NumPy Array? xSicKxBot 0 205 04-27-2020, 07:57 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] Your First Dash App – How to Get S...
Last Post: xSicKxBot
Yesterday 06:58 PM
» Replies: 0
» Views: 3
(Indie Deal) Patrician, Sudden Strike, C...
Last Post: xSicKxBot
Yesterday 05:40 PM
» Replies: 0
» Views: 3
picoCAD — A Tiny Modeller For Tiny Model...
Last Post: xSicKxBot
Yesterday 05:40 PM
» Replies: 0
» Views: 4
News - Community Focus – Gumpita
Last Post: xSicKxBot
Yesterday 05:38 PM
» Replies: 0
» Views: 3
Xbox Wire - Black Desert: New Sage Class...
Last Post: xSicKxBot
Yesterday 05:37 PM
» Replies: 0
» Views: 4
News - Twitch outlines plan to take acti...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 8
News - Don’t Miss: Devs reflect on the i...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 7
News - World War 2 MMO Enlisted Out Now ...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 8
AppleInsider - Logitech discontinues Har...
Last Post: xSicKxBot
Yesterday 08:21 AM
» Replies: 0
» Views: 7
News - Legend Of Mana’s Physical Switch ...
Last Post: xSicKxBot
Yesterday 08:20 AM
» Replies: 0
» Views: 6

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020