version 0.2, May 2016
This notebook is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Special thanks goes to Kevin Markham
Why are we learning about ensembling?
The most typical form of an ensemble is made by combining $T$ different base classifiers.
Each base classifier $M(\mathcal{S}_j)$ is trained by applying algorithm $M$ to a random subset
$\mathcal{S}_j$ of the training set $\mathcal{S}$.
For simplicity we define $M_j \equiv M(\mathcal{S}_j)$ for $j=1,\dots,T$, and
$\mathcal{M}=\{M_j\}_{j=1}^{T}$ a set of base classifiers.
Then, these models are combined using majority voting to create the ensemble $H$ as follows
$$
f_{mv}(\mathcal{S},\mathcal{M}) = max_{c \in \{0,1\}} \sum_{j=1}^T
\mathbf{1}_c(M_j(\mathcal{S})).
$$
In [2]:
# read in and prepare the chrun data
# Download the dataset
import pandas as pd
import numpy as np
data = pd.read_csv('../datasets/churn.csv')
# Create X and y
# Select only the numeric features
X = data.iloc[:, [1,2,6,7,8,9,10]].astype(np.float)
# Convert bools to floats
X = X.join((data.iloc[:, [4,5]] == 'no').astype(np.float))
y = (data.iloc[:, -1] == 'True.').astype(np.int)
In [3]:
X.head()
Out[3]:
In [3]:
y.value_counts().to_frame('count').assign(percentage = lambda x: x/x.sum())
Out[3]:
In [4]:
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
Create 100 decision trees
In [5]:
n_estimators = 100
# set a seed for reproducibility
np.random.seed(123)
n_samples = X_train.shape[0]
# create bootstrap samples (will be used to select rows from the DataFrame)
samples = [np.random.choice(a=n_samples, size=n_samples, replace=True) for _ in range(n_estimators)]
In [6]:
from sklearn.tree import DecisionTreeClassifier
np.random.seed(123)
seeds = np.random.randint(1, 10000, size=n_estimators)
trees = {}
for i in range(n_estimators):
trees[i] = DecisionTreeClassifier(max_features="sqrt", max_depth=None, random_state=seeds[i])
trees[i].fit(X_train.iloc[samples[i]], y_train.iloc[samples[i]])
In [7]:
# Predict
y_pred_df = pd.DataFrame(index=X_test.index, columns=list(range(n_estimators)))
for i in range(n_estimators):
y_pred_df.ix[:, i] = trees[i].predict(X_test)
y_pred_df.head()
Out[7]:
Predict using majority voting
In [8]:
y_pred_df.sum(axis=1)[:10]
Out[8]:
In [9]:
y_pred = (y_pred_df.sum(axis=1) >= (n_estimators / 2)).astype(np.int)
from sklearn import metrics
metrics.f1_score(y_pred, y_test)
Out[9]:
In [10]:
metrics.accuracy_score(y_pred, y_test)
Out[10]:
In [11]:
from sklearn.ensemble import BaggingClassifier
clf = BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=100, bootstrap=True,
random_state=42, n_jobs=-1, oob_score=True)
In [12]:
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
metrics.f1_score(y_pred, y_test), metrics.accuracy_score(y_pred, y_test)
Out[12]:
The majority voting approach gives the same weight to each classfier regardless of the performance of each one. Why not take into account the oob performance of each classifier
First, in the traditional approach, a similar comparison of the votes of the base classifiers is made, but giving a weight $\alpha_j$ to each classifier $M_j$ during the voting phase $$ f_{wv}(\mathcal{S},\mathcal{M}, \alpha) =\max_{c \in \{0,1\}} \sum_{j=1}^T \alpha_j \mathbf{1}_c(M_j(\mathcal{S})), $$ where $\alpha=\{\alpha_j\}_{j=1}^T$. The calculation of $\alpha_j$ is related to the performance of each classifier $M_j$. It is usually defined as the normalized misclassification error $\epsilon$ of the base classifier $M_j$ in the out of bag set $\mathcal{S}_j^{oob}=\mathcal{S}-\mathcal{S}_j$ \begin{equation} \alpha_j=\frac{1-\epsilon(M_j(\mathcal{S}_j^{oob}))}{\sum_{j_1=1}^T 1-\epsilon(M_{j_1}(\mathcal{S}_{j_1}^{oob}))}. \end{equation}
Select each oob sample
In [13]:
samples_oob = []
# show the "out-of-bag" observations for each sample
for sample in samples:
samples_oob.append(sorted(set(range(n_samples)) - set(sample)))
Estimate the oob error of each classifier
In [14]:
errors = np.zeros(n_estimators)
for i in range(n_estimators):
y_pred_ = trees[i].predict(X_train.iloc[samples_oob[i]])
errors[i] = 1 - metrics.accuracy_score(y_train.iloc[samples_oob[i]], y_pred_)
In [15]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
plt.scatter(range(n_estimators), errors)
plt.xlim([0, n_estimators])
plt.title('OOB error of each tree')
Out[15]:
Estimate $\alpha$
In [16]:
alpha = (1 - errors) / (1 - errors).sum()
In [17]:
weighted_sum_1 = ((y_pred_df) * alpha).sum(axis=1)
In [18]:
weighted_sum_1.head(20)
Out[18]:
In [19]:
y_pred = (weighted_sum_1 >= 0.5).astype(np.int)
metrics.f1_score(y_pred, y_test), metrics.accuracy_score(y_pred, y_test)
Out[19]:
In [20]:
clf = BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=100, bootstrap=True,
random_state=42, n_jobs=-1, oob_score=True)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
metrics.f1_score(y_pred, y_test), metrics.accuracy_score(y_pred, y_test)
Out[20]:
In [21]:
errors = np.zeros(clf.n_estimators)
y_pred_all_ = np.zeros((X_test.shape[0], clf.n_estimators))
for i in range(clf.n_estimators):
oob_sample = ~clf.estimators_samples_[i]
y_pred_ = clf.estimators_[i].predict(X_train.values[oob_sample])
errors[i] = metrics.accuracy_score(y_pred_, y_train.values[oob_sample])
y_pred_all_[:, i] = clf.estimators_[i].predict(X_test)
alpha = (1 - errors) / (1 - errors).sum()
y_pred = (np.sum(y_pred_all_ * alpha, axis=1) >= 0.5).astype(np.int)
In [22]:
metrics.f1_score(y_pred, y_test), metrics.accuracy_score(y_pred, y_test)
Out[22]:
The staking method consists in combining the different base classifiers by learning a second level algorithm on top of them. In this framework, once the base classifiers are constructed using the training set $\mathcal{S}$, a new set is constructed where the output of the base classifiers are now considered as the features while keeping the class labels.
Even though there is no restriction on which algorithm can be used as a second level learner, it is common to use a linear model, such as $$ f_s(\mathcal{S},\mathcal{M},\beta) = g \left( \sum_{j=1}^T \beta_j M_j(\mathcal{S}) \right), $$ where $\beta=\{\beta_j\}_{j=1}^T$, and $g(\cdot)$ is the sign function $g(z)=sign(z)$ in the case of a linear regression or the sigmoid function, defined as $g(z)=1/(1+e^{-z})$, in the case of a logistic regression.
Lets first get a new training set consisting of the output of every classifier
In [23]:
X_train_2 = pd.DataFrame(index=X_train.index, columns=list(range(n_estimators)))
for i in range(n_estimators):
X_train_2[i] = trees[i].predict(X_train)
In [24]:
X_train_2.head()
Out[24]:
In [25]:
from sklearn.linear_model import LogisticRegressionCV
In [26]:
lr = LogisticRegressionCV()
lr.fit(X_train_2, y_train)
Out[26]:
In [27]:
lr.coef_
Out[27]:
In [28]:
y_pred = lr.predict(y_pred_df)
In [29]:
metrics.f1_score(y_pred, y_test), metrics.accuracy_score(y_pred, y_test)
Out[29]:
In [30]:
y_pred_all_ = np.zeros((X_test.shape[0], clf.n_estimators))
X_train_3 = np.zeros((X_train.shape[0], clf.n_estimators))
for i in range(clf.n_estimators):
X_train_3[:, i] = clf.estimators_[i].predict(X_train)
y_pred_all_[:, i] = clf.estimators_[i].predict(X_test)
lr = LogisticRegressionCV()
lr.fit(X_train_3, y_train)
y_pred = lr.predict(y_pred_all_)
metrics.f1_score(y_pred, y_test), metrics.accuracy_score(y_pred, y_test)
Out[30]:
vs using only one dt
In [31]:
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
y_pred = dt.predict(X_test)
metrics.f1_score(y_pred, y_test), metrics.accuracy_score(y_pred, y_test)
Out[31]:
While boosting is not algorithmically constrained, most boosting algorithms consist of iteratively learning weak classifiers with respect to a distribution and adding them to a final strong classifier. When they are added, they are typically weighted in some way that is usually related to the weak learners' accuracy. After a weak learner is added, the data is reweighted: examples that are misclassified gain weight and examples that are classified correctly lose weight (some boosting algorithms actually decrease the weight of repeatedly misclassified examples, e.g., boost by majority and BrownBoost). Thus, future weak learners focus more on the examples that previous weak learners misclassified. (Wikipedia)
In [32]:
from IPython.display import Image
Image(url= "http://vision.cs.chubu.ac.jp/wp/wp-content/uploads/2013/07/OurMethodv81.png", width=900)
Out[32]:
AdaBoost (adaptive boosting) is an ensemble learning algorithm that can be used for classification or regression. Although AdaBoost is more resistant to overfitting than many machine learning algorithms, it is often sensitive to noisy data and outliers.
AdaBoost is called adaptive because it uses multiple iterations to generate a single composite strong learner. AdaBoost creates the strong learner (a classifier that is well-correlated to the true classifier) by iteratively adding weak learners (a classifier that is only slightly correlated to the true classifier). During each round of training, a new weak learner is added to the ensemble and a weighting vector is adjusted to focus on examples that were misclassified in previous rounds. The result is a classifier that has higher accuracy than the weak learners’ classifiers.
Algorithm:
In [33]:
# read in and prepare the chrun data
# Download the dataset
import pandas as pd
import numpy as np
data = pd.read_csv('../datasets/churn.csv')
# Create X and y
# Select only the numeric features
X = data.iloc[:, [1,2,6,7,8,9,10]].astype(np.float)
# Convert bools to floats
X = X.join((data.iloc[:, [4,5]] == 'no').astype(np.float))
y = (data.iloc[:, -1] == 'True.').astype(np.int)
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
n_samples = X_train.shape[0]
In [34]:
n_estimators = 10
weights = pd.DataFrame(index=X_train.index, columns=list(range(n_estimators)))
In [35]:
t = 0
weights[t] = 1 / n_samples
Train the classifier
In [36]:
from sklearn.tree import DecisionTreeClassifier
trees = []
trees.append(DecisionTreeClassifier(max_depth=1))
trees[t].fit(X_train, y_train, sample_weight=weights[t].values)
Out[36]:
Estimate error
In [37]:
y_pred_ = trees[t].predict(X_train)
error = []
error.append(1 - metrics.accuracy_score(y_pred_, y_train))
error[t]
Out[37]:
In [38]:
alpha = []
alpha.append(np.log((1 - error[t]) / error[t]))
alpha[t]
Out[38]:
Update weights
In [39]:
weights[t + 1] = weights[t]
filter_ = y_pred_ != y_train
In [40]:
weights.loc[filter_, t + 1] = weights.loc[filter_, t] * np.exp(alpha[t])
Normalize weights
In [41]:
weights[t + 1] = weights[t + 1] / weights[t + 1].sum()
Iteration 2 - n_estimators
In [42]:
for t in range(1, n_estimators):
trees.append(DecisionTreeClassifier(max_depth=1))
trees[t].fit(X_train, y_train, sample_weight=weights[t].values)
y_pred_ = trees[t].predict(X_train)
error.append(1 - metrics.accuracy_score(y_pred_, y_train))
alpha.append(np.log((1 - error[t]) / error[t]))
weights[t + 1] = weights[t]
filter_ = y_pred_ != y_train
weights.loc[filter_, t + 1] = weights.loc[filter_, t] * np.exp(alpha[t])
weights[t + 1] = weights[t + 1] / weights[t + 1].sum()
In [43]:
error
Out[43]:
In [44]:
new_n_estimators = np.sum([x<0.5 for x in error])
In [45]:
y_pred_all = np.zeros((X_test.shape[0], new_n_estimators))
for t in range(new_n_estimators):
y_pred_all[:, t] = trees[t].predict(X_test)
In [46]:
y_pred = (np.sum(y_pred_all * alpha[:new_n_estimators], axis=1) >= 1).astype(np.int)
In [47]:
metrics.f1_score(y_pred, y_test.values), metrics.accuracy_score(y_pred, y_test.values)
Out[47]:
In [48]:
from sklearn.ensemble import AdaBoostClassifier
In [49]:
clf = AdaBoostClassifier()
clf
Out[49]:
In [50]:
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
metrics.f1_score(y_pred, y_test.values), metrics.accuracy_score(y_pred, y_test.values)
Out[50]:
In [51]:
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier()
clf
Out[51]:
In [52]:
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
metrics.f1_score(y_pred, y_test.values), metrics.accuracy_score(y_pred, y_test.values)
Out[52]: