即使我设置了 warnings.filterwarnings('ignore'),如何在 Jupyter Notebook 中禁止 Python 警告?

How can I suppress Python warnings in Jupyter Notebook even if I have set warnings.filterwarnings('ignore')?

提问人:Hao Ren 提问时间:4/8/2023 更新时间:4/8/2023 访问量:587

问:

我在 Jupyter Notebook 中运行的 Python 代码中收到警告,即使我尝试使用 .具体来说,我收到警告“FutureWarning:默认值将在 1.4 中从 10 更改为”自动”。显式设置 的值以禁止显示警告“和 。warnings.filterwarnings('ignore')n_initn_initUserWarning

除了设置 的值之外,如何在 Jupyter Notebook 中禁止显示这些警告?n_init

这是我使用的代码:

import warnings
warnings.filterwarnings('ignore')

#parameter selection
bow_mx_copy = bow_mx.toarray().copy()
bow_mx_copy[bow_mx_copy==0] = 1e-6 #set a very small value to avoid zero vector, because Agglomerative Clustering doens't support when use Cosine

dbs_params = {
    "n_clusters": range(2,10),
}
print("K-means, Euclidean Distance")
metric = "euclidean"
gridsearch = GridSearchCV(KMeans(), dbs_params, scoring = 'adjusted_rand_score',\
                          n_jobs=-1).fit(bow_mx_copy[:500,]) #use the first 500 samples for parameter selection
best_estimator = gridsearch.best_estimator_
clustering = best_estimator.fit(bow_mx_copy)
ed_labels = clustering.labels_
score = silhouette_score(bow_mx, clustering.labels_, metric=metric)
print(f"metric={metric}, \n best params={gridsearch.best_params_}, \n  best silhouette_score={score}")

我还尝试了以下代码,但效果不佳。

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

我收到的警告有一小部分(太长而无法粘贴,我删除了垃圾):

 warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:778: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py", line 765, in _score
    scores = scorer(estimator, X_test)
TypeError: _BaseScorer.__call__() missing 1 required positional argument: 'y_true'

  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:778: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py", line 765, in _score
    scores = scorer(estimator, X_test)
TypeError: _BaseScorer.__call__() missing 1 required positional argument: 'y_true'

我还尝试了以下代码,但效果不佳。

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

我不明白的是,为什么即使我将 python 设置为忽略警告,它仍然返回警告。为什么这里的警告这么长?

我已经检查了类似的问题“无法使用 warnings.filterwarnings(”ignore“) 来抑制 Python 警告”,但似乎对我不起作用。我确定在运行单元之前运行忽略部分。

我将非常感谢您的任何回答和帮助。

python-3.x jupyter-notebook 警告

评论

0赞 RK Replogle 4/9/2023
为什么不显式设置 n_init=10 并保留与默认值相同的值并删除这些警告呢?这比提出问题花费的时间要少。
0赞 Hao Ren 4/9/2023
谢谢你的评论!但是,我想知道为什么在设置忽略警告后,它们在这种情况下仍然出现,而不是隐藏这些警告消息。也许弄清楚背后的原因可以避免将来出现类似的问题,导致错误的结果(很多时候这些错误并不容易发现)。

答: 暂无答案