英文:
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's `SGDClassifier`'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='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"])}""")
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论