for in range ( , )xi [ ,jxt yt xf yf datasplit ( , , ,xitmpt tnode ( xt yttmpf tnode ( xf yfloss_t tmpt calculateloss (loss_f tmpf calculateloss (curr_val loss_t loss_f if curr_val best_split_val )best_split_val curr_val best_var best_xi xi return best_var best_xi finallywe implement the recursive method for prediction def predict ( ,node)if(node right =none and node left !none)return predict ( ,node leftif(node right !none and node left =none)return predict ( ,node right if(node right =none and node left =none)return node elseif( [node <node xi)return predict ( ,node leftelsereturn predict ( ,node right running the main function defined above gives similar result to what one would achieve with the sklearn packageusing the decisiontreeregressor method main (run the main program compare with sklearn from sklearn tree import decisiontreeregressor x_train x_test y_train y_test makedata (use the same data regtree decisiontreeregressor max_depth random_state = regtree fit(x_train y_train y_hat regtree predict x_test mse np mean(np power y_hat y_test , )print (decisiontreeregressor tree loss "mse basic treetree loss decisiontreeregressor tree loss after establishing best split , sklearn assigns the corresponding feature vector randomly to one of the two child nodesrather than to the true child