18

While I iterate within a for loop I continually receive the same warning, which I want to suppress. The warning reads:

C:\Users\Nick Alexander\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\preprocessing\data.py:193: UserWarning: Numerical issues were encountered when scaling the data and might not be solved. The standard deviation of the data is probably very close to 0. warnings.warn("Numerical issues were encountered "

The code that is producing the warning is as follows:

def monthly_standardize(cols, df_train, df_train_grouped, df_val, df_val_grouped, df_test, df_test_grouped):
    # Disable the SettingWithCopyWarning warning
    pd.options.mode.chained_assignment = None
    for c in cols:
        df_train[c] = df_train_grouped[c].transform(lambda x: scale(x.astype(float)))
        df_val[c] = df_val_grouped[c].transform(lambda x: scale(x.astype(float)))
        df_test[c] = df_test_grouped[c].transform(lambda x: scale(x.astype(float)))
    return df_train, df_val, df_test

I am already disabling one warning. I don't want to disable all warnings, I just want to disable this warning. I am using python 3.7 and sklearn version 0.0

njalex22
  • 367
  • 1
  • 4
  • 13
  • This might be helpful: https://stackoverflow.com/questions/9134795/how-to-get-rid-of-specific-warning-messages-in-python-while-keeping-all-other-wa – TYZ Feb 19 '19 at 14:30
  • 2
    Possible duplicate of [How to get rid of specific warning messages in python while keeping all other warnings as normal?](https://stackoverflow.com/questions/9134795/how-to-get-rid-of-specific-warning-messages-in-python-while-keeping-all-other-wa) – a_guest Feb 19 '19 at 14:31

4 Answers4

29

Try this at the beginning of the script to ignore specific warnings:

import warnings
warnings.filterwarnings("ignore", message="Numerical issues were encountered ")
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
27
import warnings
with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    # code that produces a warning

warnings.catch_warnings() means "whatever warnings. methods are run within this block, undo them when exiting the block".

pandichef
  • 706
  • 9
  • 11
0

The python contextlib has a contextmamager for this: suppress

from contextlib import suppress

with suppress(UserWarning):
    for c in cols:
        df_train[c] = df_train_grouped[c].transform(lambda x: scale(x.astype(float)))
        df_val[c] = df_val_grouped[c].transform(lambda x: scale(x.astype(float)))
handras
  • 1,548
  • 14
  • 28
0

To ignore for specific code blocks:

import warnings

class IgnoreWarnings(object):
    def __init__(self, message):
        self.message = message
    
    def __enter__(self):
        warnings.filterwarnings("ignore", message=f".*{self.message}.*")
    
    def __exit__(self, *_):
        warnings.filterwarnings("default", message=f".*{self.message}.*")

with IgnoreWarnings("fish"):
    warnings.warn("here be fish")
    warnings.warn("here be dog")
warnings.warn("here were fish")
UserWarning: here be dog
UserWarning: here were fish
OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101