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

问题描述 投票:0回答:0

我在 Jupyter Notebook 中运行的 Python 代码中收到警告,即使我尝试使用

warnings.filterwarnings('ignore')
抑制它们。具体来说,我收到警告“FutureWarning:
n_init
的默认值将在 1.4 中从 10 更改为‘auto’。明确设置
n_init
的值以抑制警告”和
UserWarning
.

除了设置

n_init
的值外,我如何在 Jupyter Notebook 中抑制这些警告?

这是我使用的代码:

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)

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

 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 仍然返回警告。为什么这里的警告这么长?

我检查了类似的问题“Cannot supress Python Warnings with warnings.filterwarnings("ignore")”,但似乎对我不起作用。我确定我在运行单元之前运行了忽略部分。

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

python python-3.x jupyter-notebook warnings
© www.soinside.com 2019 - 2024. All rights reserved.