当我在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
在脚本开头尝试此操作:
import warnings
warnings.filterwarnings("ignore", message="Numerical issues were encountered ")
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)))
import warnings
with warnings.catch_warnings():
warnings.simplefilter('ignore')
# code that produces a warning
[warnings.catch_warnings()
的意思是“在该块中运行任何警告方法,退出该块时将其撤消”。