9

Is there a simple way to check if a model instance solves a classification or regression task in the scikit-learn library?

the_man_in_black
  • 423
  • 6
  • 15
  • 1
    This is a funny thing to want, can't really imagine a scenario in which that info is not available prior to actually fitting a model. I can only think of checking what kind of data the model is predicting – yatu Oct 01 '19 at 13:26

2 Answers2

20

Use sklearn.base.is_classifier and/or is_regressor:

from sklearn.base import is_classifier, is_regressor
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier

models = [LinearRegression(), RandomForestClassifier(), RandomForestRegressor()]

for m in models:
    print(m.__class__.__name__, is_classifier(m), is_regressor(m))

Output:

# model_name is_classifier is_regressor
LinearRegression False True
RandomForestClassifier True False
RandomForestRegressor False True
Chris
  • 29,127
  • 3
  • 28
  • 51
1

I guess you ask this because you have a serialized model whose type you do not know. Open the file and do

mlType = type(variable_name)

where variable_name is the handle of your de-serialized model.

output e.g.

class 'sklearn.linear_model.base.LinearRegression'
CLpragmatics
  • 625
  • 6
  • 21