Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if sklearn model is classifier or regressor

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

like image 452
the_man_in_black Avatar asked Oct 01 '19 13:10

the_man_in_black


People also ask

How do I know if my model is fitted sklearn?

If None , estimator is considered fitted if there exist an attribute that ends with a underscore and does not start with double underscore. The default error message is, “This %(name)s instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.”

What is a sklearn classifier?

Scikit-learn is the most popular Python library for performing classification, regression, and clustering algorithms. It is an essential part of other Python data science libraries like matplotlib , NumPy (for graphs and visualization), and SciPy (for mathematics).

What are the regression models in sklearn?

Three types of Machine Learning Models can be implemented using the Sklearn Regression Models: Reinforced Learning. Unsupervised Learning. Supervised Learning.

What is model Coef_?

coef_ − array, shape(n_features,) or (n_targets, n_features) It is used to estimate the coefficients for the linear regression problem. It would be a 2D array of shape (n_targets, n_features) if multiple targets are passed during fit. Ex. (y 2D).


1 Answers

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
like image 160
Chris Avatar answered Sep 28 '22 02:09

Chris