Using the below code:
from sklearn.utils import all_estimators
from sklearn import base
# Print all regressors
estimators = all_estimators(type_filter="regressor")
for name in estimators:
print(name[0], name[1])
...renders this response:
ARDRegression <class 'sklearn.linear_model._bayes.ARDRegression'>
AdaBoostRegressor <class 'sklearn.ensemble._weight_boosting.AdaBoostRegressor'>
BaggingRegressor <class 'sklearn.ensemble._bagging.BaggingRegressor'>
BayesianRidge <class 'sklearn.linear_model._bayes.BayesianRidge'>
CCA <class 'sklearn.cross_decomposition._pls.CCA'>
DecisionTreeRegressor <class 'sklearn.tree._classes.DecisionTreeRegressor'>
DummyRegressor <class 'sklearn.dummy.DummyRegressor'>
ElasticNet <class 'sklearn.linear_model._coordinate_descent.ElasticNet'>
ElasticNetCV <class 'sklearn.linear_model._coordinate_descent.ElasticNetCV'>
ExtraTreeRegressor <class 'sklearn.tree._classes.ExtraTreeRegressor'>
ExtraTreesRegressor <class 'sklearn.ensemble._forest.ExtraTreesRegressor'>
GammaRegressor <class 'sklearn.linear_model._glm.glm.GammaRegressor'>
GaussianProcessRegressor <class 'sklearn.gaussian_process._gpr.GaussianProcessRegressor'>
...
How can one then import
the class object within the <>
's? So I end up with something like (psuedo):
# Print/import all regressors
estimators = all_estimators(type_filter="regressor")
for name in estimators:
print(name[0], name[1])
import name[1]
...but of course that doesn't work. Thanks!
UPDATE What I'm wanting to do is then use the imported class. Trying something like:
for name in estimators:
globals()[name[0]] = name[1]
params = name[0].get_params()
print(params)
... doesn't work.
CodePudding user response:
You've got a name and a class, so you could just add them to your namespace
for name in estimators:
globals()[name[0]] = name[1]
But a dictionary may be better
my_estimators = {name[0]:name[1] for name in estimators}
I don't know how these classes are instantiated, but suppose they take two paramters and you know the name of the one you want. Then you would do
my_estimator = my_estimators["ARDRegression"]("foo", "bar")
my_estimator.get_params()
CodePudding user response:
You can simply turn the return value from all_estimators
into a dictionary whose keys are the estimator names, and the values are the estimator class themselves, which you can directly instantiate:
from sklearn.utils import all_estimators
estimator_dict = dict(all_estimators(type_filter="regressor"))
# we get and instantiate the an ARDRegressor in the line below
ard_regression_estimator = estimator_dict['ARDRegression']()
print(ard_regression_estimator) # output: ARDRegression()