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

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

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

问题

  1. 我在测试Scikit-learn包的`SGDClassifier`的准确性根据`max_iter`属性的变化我也知道测试的`max_iter`值很小所以会出现大量的`ConvergenceWarning`所以我添加了一段代码来忽略这些警告
  2. (在Google Colab界面上进行测试使用本地运行时(Jupyter笔记本Windows 11上使用WSL2))
  3. ```python
  4. import warnings
  5. warnings.filterwarnings(action='ignore') # <----
  6. from sklearn.model_selection import cross_validate
  7. from sklearn.linear_model import SGDClassifier
  8. for _seq in range(5, 20 + 1, 5):
  9. sc = SGDClassifier(loss="log_loss", max_iter=_seq, random_state=42)
  10. scores = cross_validate(sc, train_scaled, train_target, n_jobs=-1)
  11. print(f"""max_iter: {_seq}, scores = {np.mean(scores["test_score"])}""")

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

  1. /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.
  2. warnings.warn(
  3. /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.
  4. warnings.warn(
  5. /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.
  6. warnings.warn(
  7. /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.
  8. warnings.warn(
  9. /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.
  10. warnings.warn(
  11. max_iter: 5, scores = 0.8196000000000001
  12. ...(缩写)...

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. (Testing on Google colab interface, using a local runtime(Jupyter notebook, WSL2 on Windows 11))
  5. ```python
  6. import warnings
  7. warnings.filterwarnings(action=&#39;ignore&#39;) # &lt;----
  8. from sklearn.model_selection import cross_validate
  9. from sklearn.linear_model import SGDClassifier
  10. for _seq in range(5, 20 + 1, 5):
  11. sc = SGDClassifier(loss = &quot;log_loss&quot;, max_iter = _seq, random_state = 42)
  12. scores = cross_validate(sc, train_scaled, train_target, n_jobs = -1)
  13. 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.

  1. /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.
  2. warnings.warn(
  3. /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.
  4. warnings.warn(
  5. /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.
  6. warnings.warn(
  7. /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.
  8. warnings.warn(
  9. /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.
  10. warnings.warn(
  11. max_iter: 5, scores = 0.8196000000000001
  12. ...(abbreviated)...

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

答案1

得分: 1

  1. 尝试:
  2. import logging
  3. logger = logging.getLogger()
  4. logger.setLevel(logging.CRITICAL)
  5. 或者:
  6. import logging, sys
  7. logging.disable(sys.maxsize)
英文:

Try:

  1. import logging
  2. logger = logging.getLogger()
  3. logger.setLevel(logging.CRITICAL)

OR:

  1. import logging, sys
  2. 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:

确定