通过在运行代码时使用FutureWarning消息来报告机器学习中的Sklearn库即将发生的更改。
警告消息可能让初学者感觉到很困惑,这会让初学者觉得是代码有问题,或者什么地方做错了。同样的警告消息也不利于写代码,这可能会掩盖错误和程序的输出。
处理警告消息的方法有很多,包括忽视警告信息、抑制警告、修改代码。
如何抑制警告消息
警告消息并不是错误消息,因此你程序所报出的警告消息如FutureWarnings,并不会让你的程序停止执行。我们可以通过编程的方式忽视警告消息。
1、可以在已知道会报警告消息的函数块周围添加以下块来实现
# run block of code and catch warnings
with warnings.catch_warnings():
# ignore all caught warnings
warnings.filterwarnings("ignore")
# execute code that will generate warnings
...
2、直接忽视所有的警告消息
# import warnings filter
from warnings import simplefilter
# ignore all future warnings
simplefilter(action='ignore', category=FutureWarning)