抑制python警告

问题描述 投票:1回答:3

当我在for循环中进行迭代时,我不断收到相同的警告,我想取消它。该警告显示为:

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 "

产生警告的代码如下:

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

我已经禁用了一个警告。我不想禁用所有警告,我只想禁用此警告。我正在使用python 3.7和sklearn版本0.0

python suppress-warnings
3个回答
4
投票

在脚本开头尝试此操作:

import warnings
warnings.filterwarnings("ignore", message="Numerical issues were encountered ")

0
投票

python contextlib为此具有一个contextmamager: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)))

0
投票
import warnings
with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    # code that produces a warning

[warnings.catch_warnings()的意思是“在该块中运行任何警告方法,退出该块时将其撤消”。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.