Reputation: 13
I have used the BaggingRegressor class in order to build the best model with the following parameters:
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import BaggingRegressor
Reg_ensemble=BaggingRegressor(base_estimator=DecisionTreeRegressor(max_depth=3),n_estimators=10,random_state=0).fit(feature,target)
with above setting, it will create 10 trees. I want to extract and access to each member of ensemble regression(each tree)separately and then fit a test sample on each of them. Is it possible to access each model?
Upvotes: 1
Views: 806
Reputation: 60318
The estimators_
attribute of the fitted model provides a list with the ensemble estimators; here is an example with dummy data and n_estimators=3
for brevity:
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.datasets import make_regression
X, y = make_regression(n_samples=100, n_features=4,
n_informative=2, n_targets=1,
random_state=0, shuffle=False)
regr = BaggingRegressor(base_estimator=DecisionTreeRegressor(max_depth=3),
n_estimators=3, random_state=0)
regr.fit(X, y)
regr.estimators_
# result:
[DecisionTreeRegressor(ccp_alpha=0.0, criterion='mse', max_depth=3,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=2087557356, splitter='best'),
DecisionTreeRegressor(ccp_alpha=0.0, criterion='mse', max_depth=3,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=132990059, splitter='best'),
DecisionTreeRegressor(ccp_alpha=0.0, criterion='mse', max_depth=3,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=1109697837, splitter='best')]
After you have fitted the BaggingRegressor
(the base estimators do not exist before fitting), you can access the base estimators for fitting with data Xs, ys
simply as:
for model in regr.estimators_:
model.fit(Xs, Ys)
Upvotes: 1