example (bagging for regression treewe next proceed with basic bagging example for regression treein which we compare the decision tree estimator with the corresponding bagged estimator we use the metric (coefficient of determinationfor comparison baggingexample py import numpy as np from sklearn datasets import make_friedman from sklearn tree import decisiontreeregressor from sklearn model_selection import train_test_split from sklearn metrics import _score np random seed ( create regression problem n_points points xy make_friedman n_samples =n_points n_features = noise = random_state = split to train /test set x_train x_test y_train y_test train_test_split (xytest_size = random_state = training regtree decisiontreeregressor random_state = regtree fit(x_train y_train test yhat regtree predict x_test bagging construction n_estimators = bag np empty (n_estimators )dtype object bootstrap_ds_arr np empty (n_estimators )dtype object for in range n_estimators )sample bootstrapped data set ids np random choice range ( lenx_train )),size=lenx_train )replace =truex_boot x_train [idsy_boot y_train [idsbootstrap_ds_arr [inp unique (idsbag[idecisiontreeregressor (bag[ifit(x_boot y_boot bagging prediction yhatbag np zeros (leny_test )for in range n_estimators )yhatbag yhatbag bag[ipredict x_test yhatbag yhatbag n_estimators out of bag loss estimation