Multi-Class AdaBoosted Decision Trees in Scikit-learn

This example reproduces Figure 1 of Zhu et al [1] and shows how boosting can improve prediction accuracy on a multi-class problem. The classification dataset is constructed by taking a ten-dimensional standard normal distribution and defining three classes separated by nested concentric ten-dimensional spheres such that roughly equal numbers of samples are in each class (quantiles of the \chi^2 distribution).

The performance of the SAMME and SAMME.R [1] algorithms are compared. SAMME.R uses the probability estimates to update the additive model, while SAMME uses the classifications only. As the example illustrates, the SAMME.R algorithm typically converges faster than SAMME, achieving a lower test error with fewer boosting iterations. The error of each algorithm on the test set after each boosting iteration is shown on the left, the classification error on the test set of each tree is shown in the middle, and the boost weight of each tree is shown on the right.

All trees have a weight of one in the SAMME.R algorithm and therefore are not shown.

X,y=make_gaussian_quantiles(n_samples=13000,n_features=10,n_classes=3,random_state=1)n_split=3000X_train,X_test=X[:n_split],X[n_split:]y_train,y_test=y[:n_split],y[n_split:]bdt_real=AdaBoostClassifier(DecisionTreeClassifier(max_depth=2),n_estimators=600,learning_rate=1)bdt_discrete=AdaBoostClassifier(DecisionTreeClassifier(max_depth=2),n_estimators=600,learning_rate=1.5,algorithm="SAMME")bdt_real.fit(X_train,y_train)bdt_discrete.fit(X_train,y_train)real_test_errors=[]discrete_test_errors=[]forreal_test_predict,discrete_train_predictinzip(bdt_real.staged_predict(X_test),bdt_discrete.staged_predict(X_test)):real_test_errors.append(1.-accuracy_score(real_test_predict,y_test))discrete_test_errors.append(1.-accuracy_score(discrete_train_predict,y_test))n_trees_discrete=len(bdt_discrete)n_trees_real=len(bdt_real)# Boosting might terminate early, but the following arrays are always# n_estimators long. We crop them to the actual number of trees here:discrete_estimator_errors=bdt_discrete.estimator_errors_[:n_trees_discrete]real_estimator_errors=bdt_real.estimator_errors_[:n_trees_real]discrete_estimator_weights=bdt_discrete.estimator_weights_[:n_trees_discrete]

samme=go.Scatter(x=range(1,n_trees_discrete+1),y=discrete_test_errors,showlegend=False,mode='lines',line=dict(color='black',width=1),name='SAMME')samme_r=go.Scatter(x=range(1,n_trees_real+1),y=real_test_errors,showlegend=False,mode='lines',line=dict(color='black',dash='dash',width=1),name='SAMME.R')fig.append_trace(samme,1,1)fig.append_trace(samme_r,1,1)fig['layout']['xaxis1'].update(title='Number of Trees',showgrid=False)fig['layout']['yaxis1'].update(title='Test Error',showgrid=False)samme1=go.Scatter(x=range(1,n_trees_discrete+1),y=discrete_estimator_errors,showlegend=False,mode='lines',line=dict(color='blue',width=1),name='SAMME')samme_r1=go.Scatter(x=range(1,n_trees_real+1),y=real_estimator_errors,showlegend=False,mode='lines',line=dict(color='red',width=1),name='SAMME.R')fig.append_trace(samme1,1,2)fig.append_trace(samme_r1,1,2)fig['layout']['xaxis2'].update(title='Number of Trees',showgrid=False)fig['layout']['yaxis2'].update(title='Error',showgrid=False,range=[.2,max(real_estimator_errors.max(),discrete_estimator_errors.max())*1.2])samme2=go.Scatter(x=range(1,n_trees_discrete+1),y=discrete_estimator_weights,showlegend=False,mode='lines',line=dict(color='blue',width=1),name='SAMME')fig.append_trace(samme2,1,3)fig['layout']['xaxis3'].update(title='Number of Trees',showgrid=False)fig['layout']['yaxis3'].update(title='Weight',showgrid=False)