忽略 Python 模块(seaborn、sklearn)中的警告

Ignore warnings in from Python modules (seaborn, sklearn)

上面题名相关的题很多,基本上都是告诉你要做的:

import warnings
warnings.filterwarnings('ignore')

并确保将其放置在第一次导入之前

然而,即使在这样做之后,我仍然收到来自 seabornsklearn 的许多警告。我得到 UserWarningDataConversionWarningRuntimeWarning,根据文档,它们都继承自 Warning,并且应该包含在上面的代码中。

还有其他方法可以隐藏这些警告吗? (反正大部分我都解决不了)

编辑

示例 1:

C:\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py:645: DataConversionWarning: Data with input dtype int32, int64 were all converted to float64 by StandardScaler.
  return self.partial_fit(X, y)

示例 2

C:\Anaconda3\lib\site-packages\seaborn\distributions.py:340: UserWarning: Attempted to set non-positive bottom ylim on a log-scaled axis.
Invalid limit will be ignored.
  ax.set_ylim(0, auto=None)

示例 2

有点难找; seaborn 导入统计模型。在 statsmodels/tools/sm_exceptions.py 中,您会找到 this line

warnings.simplefilter('always', category=UserWarning)

其中反转用户警告的任何先前设置。

目前的解决方案是删除该行或在导入 seaborn(以及 statsmodels)后设置警告状态。在 statsmodels 的未来版本中,PR 4712 将修复此问题,因此也可以选择使用 statsmodels 的开发版本。

示例 1

我没有找到重现 sklearn 中的第一个示例的方法;所以这可能有也可能没有不同的原因。