Ignore warnings in from Python modules (seaborn, sklearn) - python

There are many questions related to the question title above and all basically tell you to do:
import warnings
warnings.filterwarnings('ignore')
and to make sure this is placed before the first import.
However, even after doing this I get many warnings from seaborn and sklearn. I get UserWarning, DataConversionWarning and RuntimeWarning which, according to documentation, all inherit from Warning and should be covered by the above code.
Is there another way to hide those warnings?
(I cannot really solve most of them anyway)
EDIT
Example 1:
C:\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py:645: DataConversionWarning: Data with input dtype int32, int64 were all converted to float64 by StandardScaler.
return self.partial_fit(X, y)
Example 2
C:\Anaconda3\lib\site-packages\seaborn\distributions.py:340: UserWarning: Attempted to set non-positive bottom ylim on a log-scaled axis.
Invalid limit will be ignored.
ax.set_ylim(0, auto=None)

Example2
It's a bit hard to track down; seaborn imports statsmodels. And in statsmodels/tools/sm_exceptions.py you find this line
warnings.simplefilter('always', category=UserWarning)
in which reverses any previous setting for user warnings.
A solution for now would be to remove that line or to set the warning state after the import of seaborn (and hence statsmodels). In a future version of statsmodels this will be fixed by PR 4712, so using the development version of statsmodels would also be an option.
Example1
I did not find a way to reproduce the first example from sklearn; so that may or may not have a different reason.

Related

bar_chart_race shows warning about fixedlocator ? how to solve it

when I use bar_chart_race, it shows this waring: how to solve it since inside there is no parameter on the set_yticklabels?
C:\ProgramData\Anaconda3\lib\site-packages\bar_chart_race_make_chart.py:286: UserWarning: FixedFormatter should only be used together with FixedLocator
ax.set_yticklabels(self.df_values.columns)

Disable linting in Spyder (orange triangles from Pyflakes)

I have an import that is only used in a doctest. The linter (Pyflakes) that is used by Spyder to generate warnings right next to the code doesn't like this. I tried a bunch of ways of turning off the "imported but unused" warning, but the orange triangle in the left margin doesn't go away.
Since these triangles do not stem from Pylint but from Pyflakes, options for pylint naturally don't work:
import pandas as pd # pylint: disable=unused-import
import pandas as pd # pylint: disable=W0611
How can I turn the warnings off? I installed Spyder via WinPython.
With the new Spyder version 5.3.3, the following methods are now available in order to suppress the orange warning triangles: Append either # noqa or # analysis:ignore to the end of the line in question.
Suppressing only specific error types does not seem to work at this point in time.
(This information is mostly based on the closing post of the GitHub issue which Ilya mentioned.)
Apparently, this is a known bug in Spyder.
https://github.com/spyder-ide/spyder/issues/7879

DeprecationWarning: `np.bool`

I am new to Python using Spyder. I have written the following code:
import os
import numpy as np
os.environ["CDF_LIB"] = "D:\Anaconda\Lib"
from spacepy import pycdf
cdf = pycdf.CDF('Sample.cdf')
print(cdf) # Print the titles of the CDF file
Diff_en=cdf['diff'][:]
For some reason I keep getting the following error and I'm not sure why (I don't know what bool is). Any help is appreciated:
Diff_en=cdf['diff'][:]
D:\Anaconda\lib\site-packages\spacepy\pycdf\__init__.py:3957: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
This is a harmless warning. If you can live with it, just leave it be.
It comes about because the NumPy package recently deprecated its numpy.bool in favour of the standard Python bool (or alternatively the awkward numpy.bool_), as stated on the documentation page linked in your warning message.
If you can't stand the warning, I suggest you try updating spacepy. You can also silence the warning explicitly from within your project.
As the other answer notes, it's a warning and generally won't affect anything.
The following code can be used to selectively silence this particular DeprecationWarning
from warnings import filterwarnings
filterwarnings(action='ignore', category=DeprecationWarning, message='`np.bool` is a deprecated alias')

Hide scikit-learn ConvergenceWarning: "Increase the number of iterations (max_iter) or scale the data"

I am using Python to predict values and getting many warnings like:
Increase the number of iterations (max_iter) or scale the data as
shown in:
https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(
C:\Users\ASMGX\anaconda3\lib\site-packages\sklearn\linear_model_logistic.py:762:
ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL
NO. of ITERATIONS REACHED LIMIT.
this prevents me from seeing the my own printed results.
Is there any way I can stop these warnings from showing?
You can use the warnings-module to temporarily suppress warnings. Either all warnings or specific warnings.
In this case scikit-learn is raising a ConvergenceWarning so I suggest suppressing exactly that type of warning. That warning-class is located in sklearn.exceptions.ConvergenceWarning so import it beforehand and use the context-manager catch_warnings and the function simplefilter to ignore the warning, i.e. not print it to the screen:
import warnings
from sklearn.exceptions import ConvergenceWarning
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=ConvergenceWarning)
optimizer_function_that_creates_warning()
You can also ignore that specific warning globally to avoid using the context-manager:
import warnings
warnings.simplefilter("ignore", category=ConvergenceWarning)
optimizer_function_that_creates_warning()
I suggest using the context-manager though since you are sure about where you suppress warnings. This way you will not suppress warnings from unexpected places.
Use the solver and max_iter to solve the problem...
from sklearn.linear_model import LogisticRegression
clf=LogisticRegression(solver='lbfgs', max_iter=500000).fit(x_train, y_train)

The sklearn.tree.tree module is deprecated in version 0.22 and will be removed in version 0.24

I'm using the DecisionTreeClassifier from scikit-learn (https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html) and getting the following warning:
FutureWarning: The sklearn.tree.tree module is deprecated in version
0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.tree. Anything that
cannot be imported from sklearn.tree is now part of the private API.
I'm a bit confused about why I'm receiving this warning as I'm not using sklearn.tree.tree anywhere. I am using sklearn.tree as the warning suggests but still receive this warning. In fact I'm using code of the form:
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(<params>)
tree.fit(training_data, training_labels)
As per the example code given in https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html but still get this warning.
I've searched the scikit documentation and online and can't find how to update my code inline with the suggestion in the warning. Does anyone know what I need to change to fix the warning?
You can ignore the deprecation warning, it's only a warning (I wouldn't worry if your code isn't referencing that subpackage, there's probably an import somewhere under the hood inside sklearn.)
You could suppress all FutureWarnings, but then you might miss another more important one, on sklearn or another package. So I'd just ignore it for now. But if you want to:
import warnings
warnings.simplefilter('ignore', FutureWarning)
from sklearn.tree import ...
# ... Then turn warnings back on for other packages
warnings.filterwarnings('module') # or 'once', or 'always'
See the doc, or How to suppress Future warning from import?, although obviously you replace import pandas with your own import statement.
link of the same kind of problem
It's just a warning, for now -- until you upgrade scikit/sklearn to version 0.24, You need to update your scikit/sklearn version.

Categories