Show Sidebar Hide Sidebar

# 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.

[1] J. Zhu, H. Zou, S. Rosset, T. Hastie, “Multi-class AdaBoost”, 2009.

#### New to Plotly?¶

You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version¶

In [1]:
import sklearn
sklearn.__version__

Out[1]:
'0.18.1'

### Imports¶

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

from sklearn.externals.six.moves import zip

from sklearn.datasets import make_gaussian_quantiles
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier


### Calculations¶

In [3]:
X, y = make_gaussian_quantiles(n_samples=13000, n_features=10,
n_classes=3, random_state=1)

n_split = 3000

X_train, X_test = X[:n_split], X[n_split:]
y_train, y_test = y[:n_split], y[n_split:]

DecisionTreeClassifier(max_depth=2),
n_estimators=600,
learning_rate=1)

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 = []

for real_test_predict, discrete_train_predict in zip(
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]


### Plot Results¶

In [4]:
fig = tools.make_subplots(rows=1, cols=3)

This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]  [ (1,3) x3,y3 ]


In [5]:
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)

In [6]:
py.iplot(fig)

Out[6]:

Author:

    Noel Dawe <noel.dawe@gmail.com>



    BSD 3 clause