`warnings.filterwarnings()`无法抑制SGDClassifier的ConvergenceWarning。

huangapple go评论61阅读模式
英文:

warnings.filterwarnings() doesn't work to suppress ConvergenceWarning of SGDClassifier

问题

我在测试Scikit-learn包的`SGDClassifier`的准确性根据`max_iter`属性的变化我也知道测试的`max_iter`值很小所以会出现大量的`ConvergenceWarning`,所以我添加了一段代码来忽略这些警告
(在Google Colab界面上进行测试使用本地运行时(Jupyter笔记本在Windows 11上使用WSL2))

```python
import warnings
warnings.filterwarnings(action='ignore')        # <----

from sklearn.model_selection import cross_validate
from sklearn.linear_model import SGDClassifier

for _seq in range(5, 20 + 1, 5):
    sc = SGDClassifier(loss="log_loss", max_iter=_seq, random_state=42)
    scores = cross_validate(sc, train_scaled, train_target, n_jobs=-1)
    print(f"""max_iter: {_seq}, scores = {np.mean(scores["test_score"])}""")

不幸的是,这段代码没有起作用,不必要的警告充斥着整个控制台,让我难以查看模型性能的变化。

/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
max_iter: 5, scores = 0.8196000000000001
...(缩写)...

有没有办法抑制这些烦人且不必要的警告消息?非常感谢您能提供的任何帮助。


<details>
<summary>英文:</summary>

I was testing the Scikit-learn package&#39;s `SGDClassifier`&#39;s accuracy according to the change of the `max_iter` property. I also knew that the testing `max_iter` values are small so there would be a bunch of `ConvergenceWarning`, so I added a code to ignore those warnings.
(Testing on Google colab interface, using a local runtime(Jupyter notebook, WSL2 on Windows 11))

```python
import warnings
warnings.filterwarnings(action=&#39;ignore&#39;)        # &lt;----

from sklearn.model_selection import cross_validate
from sklearn.linear_model import SGDClassifier

for _seq in range(5, 20 + 1, 5):
    sc = SGDClassifier(loss = &quot;log_loss&quot;, max_iter = _seq, random_state = 42)
    scores = cross_validate(sc, train_scaled, train_target, n_jobs = -1)
    print(f&quot;&quot;&quot;max_iter: {_seq}, scores = {np.mean(scores[&quot;test_score&quot;])}&quot;&quot;&quot;)

Unfortunately, the code didn't work and the unnecessary warnings filled all over the console, and bothered me looking at the change in the model performances.

/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
max_iter: 5, scores = 0.8196000000000001
...(abbreviated)...

Is there a way to suppress those annoying and unnecessary warning messages? I really appreciate any help you can provide.

答案1

得分: 1

尝试:

    import logging
    logger = logging.getLogger()
    logger.setLevel(logging.CRITICAL)

或者:

    import logging, sys
    logging.disable(sys.maxsize)
英文:

Try:

import logging
logger = logging.getLogger()
logger.setLevel(logging.CRITICAL)

OR:

import logging, sys
logging.disable(sys.maxsize)

huangapple
  • 本文由 发表于 2023年6月19日 12:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定