Shahin Shirazi
Shahin Shirazi

Reputation: 439

How to capture specific warning without raising error in python

I am running different sets of data to identify best modeling algorithm for each dataset. I loop through each datasets to check various algorithms and select the best models based on test score. I know that some of my datasets not going to converge for specific models (i.e: LogisticRegression) and getting converging warning (i.e:"lbfgs failed to converge (status=1):"). I don't want to ignore the warning. My goal is to return score for models that converge and don't return any value if I get this convergence warning.

I am able to work around this by turning this warning into error using "warnings.filterwarnings('error',category=ConvergenceWarning, module='sklearn')" and then go through try and except to get what I want. The problem with this method is that if there is any other error beside sklearn convergance warning it will bypass the try line and I wouldn't be able to know what cause the error. Is there any other way to capture this warning beside turning it to error?

Here is the simplified overview of my code ( data not included as its a big datasets and I don't think is relevant to the question). Most of stackoverflow questions that I was able to find is about how to supress the error(How to disable ConvergenceWarning using sklearn?)or to turn this warning into error and I didn't find any other method to capture the warning without turning it to error.

  from sklearn.linear_model import LogisticRegression
  from sklearn.exceptions import ConvergenceWarning
  warnings.filterwarnings('error',category=ConvergenceWarning, module='sklearn')
  try:
      model=LogisticRegression().fit(x_train,y_train)
      predict=model.predict(x_test)
  except:
      print('model didnt converge')

Upvotes: 0

Views: 786

Answers (1)

Cargo23
Cargo23

Reputation: 3189

There are a couple things that can help you here.

First, you can specify what kind of Exception you are looking for, any you can specify multiple except clauses. Here is an example from the docs:

import sys

try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except OSError as err:
    print("OS error: {0}".format(err))
except ValueError:
    print("Could not convert data to an integer.")
except:
    print("Unexpected error:", sys.exc_info()[0])
    raise

The other thing to notice in the above is the except OSError as err. Using this syntax, you can print the error message associated with the error.

Upvotes: 1

Related Questions