Opps_0
Opps_0

Reputation: 438

How to get base model score with ensemble from bagging

I have created a Bagging Ensemble Model. The model is given below

def get_models():
    models = dict()
    n_trees = [10, 50, 100, 500, 500, 1000, 5000]
    for n in n_trees:
        models[str(n)] = BaggingRegressor(base_estimator=DecisionTreeRegressor(), n_estimators=n)
        return models

I want to get the score for each base model and the final ensemble model's score. So, I am using (with base_estimator_) the below code to access the base model

So, after fitting the main model I am using this code to get the score for base models

        for learner in regressor.base_estimator_:
            base_dfs.append(
                evaluate_base_learner(
                    learner, X_train[train_index], X_test, y_train[train_index], y_test, k, method, learner_name = type(model).__name__,
                )
            )

But I am getting an error TypeError: 'DecisionTreeRegressor' object is not iterable. Could you tell me why I am getting this error and how can I solve the issue?

Upvotes: 0

Views: 105

Answers (1)

0Knowledge
0Knowledge

Reputation: 755

If you want to access the base model, you should use the

for learner in regressor.estimators_:

More info

Moroever, if you want to run more model as I understand based on your code you need to change the return code (not inside the loop)

Upvotes: 1

Related Questions