7

How is it possible to run script with sklearn's GridsearchCV() from command line and not to print all the warnings, when the parameter n_jobs is set to -1? With warnings.simplefilter('ignore') it is not working.

  • When n_jobs = 1 it is working correctly and no warnings are printed

  • When I run the script from spyder even with n_jobs = -1 it is working correctly

  • I tried multiple approaches described here Eliminating warnings from scikit-learn but none of them work when running the script from command line and Gridsearch with n_jobs = -1

  • I tried also to put the with statement to ignore the warnings after the if __name__ == '__main__'

    statement, but this is also not working

I have scikit-learn 0.21.3.

Here is a small reproducible example, where it prints multiple times the FutureWarning and DepreciationWarning from IsolationForest.

from sklearn import datasets
from sklearn.cluster import KMeans
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.ensemble import IsolationForest
import warnings
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin

class OutlierRemover(BaseEstimator, TransformerMixin):
    def __init__(self, contamination = 0.1):
        self.contamination = contamination
    def fit(self, x, y = None):          
        return self
    def transform(self, x, y = None):
        predicted_values = IsolationForest(n_jobs = -1, contamination = self.contamination).fit_predict(x)
        x = x[np.where(predicted_values == 1)] 
        return x
    
def pipe():
    iris = datasets.load_iris()
    x = iris.data

    pipe = Pipeline([('scalers', MinMaxScaler()),
                     ('outlierRemovers', OutlierRemover()),
                     ('cluster', KMeans(n_jobs = -1))])
    
    params = [{'scalers':[MinMaxScaler(), StandardScaler()]}]
    gridsearch = GridSearchCV(pipe, params, verbose = 100, cv = 2, n_jobs = -1)

    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        gridsearch.fit(x)
        
if __name__ == '__main__':
    pipe()
Community
  • 1
  • 1
jaroslav
  • 73
  • 6

1 Answers1

3

For the parallel backend warnings you need (from the terminal):

export PYTHONWARNINGS="ignore"

Next, run your script with python.

For the DeprecationWarning, you cannot and this is actually a bug. Feel free to open a request on github.

seralouk
  • 30,938
  • 9
  • 118
  • 133