禁用jupyter笔记本中的警告

Sah*_*and 8 python jupyter-notebook

我在jupyter笔记本中收到这个警告.

/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:10: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.
  # Remove the CWD from sys.path while we load stuff.
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:11: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.
  # This is added back by InteractiveShellApp.init_path()
Run Code Online (Sandbox Code Playgroud)

这很烦人,因为它出现在我做的每一次跑步中:

在此输入图像描述

我该如何修复或禁用它?

Neo*_*Neo 17

尝试这个:

import warnings
warnings.filterwarnings('ignore')
warnings.simplefilter('ignore')
Run Code Online (Sandbox Code Playgroud)


Ram*_*uet 11

如果您确定您的代码是正确且简单的想要消除此警告以及笔记本中的所有其他警告,请执行以下操作:

import warnings
warnings.filterwarnings('ignore')
Run Code Online (Sandbox Code Playgroud)

  • 这应该是经过验证的答案! (3认同)

fhc*_*chl 6

您还可以仅针对某些代码行取消警告:

import warnings

def function_that_warns():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    function_that_warns()  # this will not show a warning
Run Code Online (Sandbox Code Playgroud)


Mar*_*ach 1

如果您将参数作为浮点数传递(应该是整数),您将收到此警告。

例如,在下面的示例中,num应该是整数,但作为浮点数传递:

import numpy as np
np.linspace(0, 10, num=3.0)
Run Code Online (Sandbox Code Playgroud)

这将打印您收到的警告:

ipykernel_launcher.py:2: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.
Run Code Online (Sandbox Code Playgroud)

由于缺少部分代码,我无法弄清楚应该将哪个参数作为整数传递,但以下示例显示了如何解决此问题:

import numpy as np
np.linspace(0, 10, num=int(3.0))
Run Code Online (Sandbox Code Playgroud)