<jupyter_start><jupyter_text>Supervised Learning: Decision Trees **Decision trees** recursively partition the space of predictor variables into a piecewise-constant response surface. We can consider each component $j=1,\ldots,k$ to be a region in the response surface, and $w_j$ the expected response in that region.$$f(x) = \sum_{j=1}^k w_j I(\mathbf{x} \in R_j)$$where $R_j$ defines a region, and the weight $w_j$ encodes the response value in that region. To get a sense of how decision trees work, consider the diabetes dataset that we have seen previously. In the plot below, the response variable (`target`, an index of disease progression) is color-coded as a function of two variables, metabolic rate (`bmi`) and a blood serum measurement (`ltg`).<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
sns.set()
from sklearn.datasets import load_diabetes

# Predictors: "age" "sex" "bmi" "map" "tc"  "ldl" "hdl" "tch" "ltg" "glu"
diabetes = load_diabetes()
y = diabetes['target']
bmi, ltg = diabetes['data'][:,[2,8]].T

plt.scatter(ltg, bmi,  c=y, cmap="Blues")
plt.colorbar()
plt.xlabel('ltg'); plt.ylabel('bmi');<jupyter_output><empty_output><jupyter_text>One approach to building a predictive model is to subdivide the variable space into regions, by sequentially subdividing each variable. For example, if we split `ltg` at a threshold value of -0.01, it does a reasonable job of isolating the large values in one of the resulting subspaces.<jupyter_code>ltg_split = -0.01
plt.scatter(ltg, bmi,  c=y, cmap="Blues")
plt.vlines(ltg_split, *plt.gca().get_ylim(), linestyles='dashed')
plt.colorbar()
plt.xlabel('ltg'); plt.ylabel('bmi');<jupyter_output><empty_output><jupyter_text>However, that region still contains a fair number of low (blue) values, so we can similarly bisect the region using a `bmi` value of -0.03 as a threshold value:<jupyter_code>bmi_split = -0.03
plt.scatter(ltg, bmi,  c=y, cmap="Blues")
plt.vlines(ltg_split, *plt.gca().get_ylim(), linestyles='dashed')
plt.hlines(bmi_split, ltg_split, plt.gca().get_xlim()[1], linestyles='dashed')
plt.colorbar()
plt.xlabel('ltg'); plt.ylabel('bmi');<jupyter_output><empty_output><jupyter_text>We can use this partition to create a piecewise-constant function, which returns the average value of the observations in each region defined by the threshold values. We could then use this rudimentary function as a predictive model.<jupyter_code>np.mean(y[(bmi>bmi_split) & (ltg>ltg_split)])
np.mean(y[(bmi<=bmi_split) & (ltg>ltg_split)])
np.mean(y[ltg<ltg_split])<jupyter_output><empty_output><jupyter_text>The choices for splitting the variables here were relatively arbitrary. Better choices can be made using a cost function $C$, such as **residual sums of squares** (RSS).$$C = \sum_j \sum_{i \in R_j} (y_i - \hat{y}_{R_j})^2$$where $\hat{y}_{R_j}$ is the mean response for the training observations in the jth region. ExerciseUse residual sums of squares to select competitive threshold values for the predictive model defined above<jupyter_code># Write your answer here<jupyter_output><empty_output><jupyter_text>The recursive partitioning demonstrated above results in a **decision tree**. The regions defined by the trees are called *terminal nodes*. Locations at which a predictor is split, such as `bmi`=-0.03, are called  *internal nodes*. As with this simple example, splits are not generally symmetric, in the sense that splits do not occur similarly on all branches.Now consider a subset of three variables from the Titanic dataset, which we would like to use to predict survival from the disaster. The following describes one such decision tree:- We first check if gender of the passenger is male. If "no", we follow the right branch and end up in a leaf where the probability of survival is $p(y=1,x_1=F)=0.73$, so we predict survival ($y=1$) at this node (36% of observations fall under this leaf)- If the passenger is male, we then check the age of the passenger. If he is older than 9.5 years, then the probability of survival $p(y=1,x_1=M,x_2>9.5)=0.17$, so we predict death ($y=0$). - If, on the other hand, the passenger is younger than 9.5 years, we then check if the number of siblings and spouses on board was higher than 2.5; if "yes", then the probability of survival  $p(y=1, x_1=M, x_2>9.5, x_3>2.5)=0.05$, so we predict death, otherwise we predict survival with $p(y=1, x_1=M, x_2>9.5 , x_3 \lt 2.5)=0.89$. Hence, these probabilities are just the empirical fraction of positive examples that satisfy each conjunction of feature values, which defines a path from the root to a leaf.![titanic tree](http://upload.wikimedia.org/wikipedia/commons/f/f3/CART_tree_titanic_survivors.png)There is no way to feasibly evaluate all possible partitions. Instead, the strategy is to use a top-down, **greedy** approach that is optimal (according to a particular cost function) for the current split only. By "greedy", we mean that at each step it chooses the most advantageous binary partition, not taking into account the impact of the choice on the quality of subsequent partitions.$$(j^*, t^*) = \text{argmin}_{j,t} C(\{\mathbf{x}_i,y_i: x_{ij} \le t\}) + C(\{\mathbf{x}_i,y_i: x_{ij} \gt t\})$$where $C$ is a cost function, $j$ and $t$ are a variable index and cutpoint, respectively. We will restrict consideration to **binary partitions**. Classification TreesIn addition to regression trees, we can also use decision trees on categorical outcomes, and these are called classification trees. The primary difference in implementation is that residual sums of squares is no longer an appropriate splitting criterion.  EntropyAn alternative splitting criterion for decision tree learning algorithms is *information gain*. It measures how well a particular attribute distinguishes among different target classifications. Information gain is measured in terms of the expected reduction in the entropy or impurity of the data. The entropy of a set of probabilities is:$$H(p) = -\sum_i p_i log_2(p_i)$$If we have a set of binary responses from some variable, all of which are positive/true/1, then knowing the values of the variable does not hold any predictive value for us, since all the outcomes are positive. Hence, the entropy is zero:<jupyter_code>import numpy as np
from scipy.stats import entropy

#entropy = lambda p: -np.sum(p * np.log2(p)) if not 0 in p else 0
entropy([.4,.6], base=2)<jupyter_output><empty_output><jupyter_text>However, if the variable splits responses into equal numbers of positive and negative values, then entropy is maximized, and we wish to know about the feature:<jupyter_code>entropy([0.5, 0.5], base=2)
pvals = np.linspace(0, 1)        
plt.plot(pvals, [entropy([p,1-p]) for p in pvals])<jupyter_output><empty_output><jupyter_text>The entropy calculation tells us how much additional information we would obtain with knowledge of the variable.So, if we have a set of candidate covariates from which to choose as a node in a decision tree, we should choose the one that gives us the most information about the response variable (*i.e.* the one with the highest entropy). Misclassification RateAlternatively, we can use the misclassification rate:$$C(j,t) = \frac{1}{n_{jt}} \sum_{y_i: x_{ij} \gt t} I(y_i \ne \hat{y})$$where $\hat{y}$ is the most probable class label and $n_{ij}$ is the number of observations in the data subset obtained from splitting via $j,t$. Gini indexThe Gini index is simply the expected error rate:$$C(j,t) = \sum_{k=1}^K \hat{\pi}_{jt}[k] (1 - \hat{\pi}_{jt}[k]) = 1 - \sum_{k=1}^K \hat{\pi}_{jt}[k]^2$$where $\hat{\pi}_{jt}[k]$ is the probability of an observation being correctly classified as class $k$ for the data subset obtained from splitting via $j,t$ (hence, $(1 - \hat{\pi}_{jt}[k])$ is the misclassification probability).<jupyter_code>gini = lambda p: 1. - (np.array(p)**2).sum()
pvals = np.linspace(0, 1)        
plt.plot(pvals, [entropy([p,1-p])/2. for p in pvals], label='Entropy')
plt.plot(pvals, [gini([p,1-p]) for p in pvals], label='Gini')
plt.legend()<jupyter_output><empty_output><jupyter_text>ID3A given cost function can be used to construct a decision tree via one of several algorithms. The Iterative Dichotomiser 3 (ID3) is on such algorithm, which uses entropy, and a related concept, *information gain*, to choose features and partitions at each classification step in the tree.Information gain is the difference between the current entropy of a system and the entropy measured after a feature is chosen. If $S$ is a set of examples and $X$ is a possible feature on which to partition the examples, then:$$G(S,X) = \text{Entropy}(S) - \sum_{x \in X} \frac{\(S_x)}{\(S)} \text{Entropy}(S_x)$$where $\$ is the count function and $x$ is a particular value of $X$.Let's say $S$ is a set of survival events, $S = \{s_1=survived, s_2=died, s_3=died, s_4=died\}$ and a particular variable $X$ can have values $\{x_1, x_2, x_3\}$. To perform a sample calculation of information gain, we will say that:* $X(s_1) = x_2$* $X(s_2) = x_2$* $X(s_3) = x_3$* $X(s_4) = x_1$The current entropy of this state is:$$\text{Entropy}(S) = -p^{(+)} \log_2(p^{(+)}) - p^{(-)} \log_2(p^{(-)})$$$$= -0.25 \log_2(0.25) - 0.75 \log_2(0.75) = 0.5 + 0.311 = 0.811$$Now, we need to compute the information after selecting variable $X$, which is the sum of three terms:$$\frac{\(S_{x1})}{\(S)} \text{Entropy}(S) = 0.25 (-0 \log_2(0) - 1 \log_2(1)) = 0$$$$\frac{\(S_{x2})}{\(S)} \text{Entropy}(S) = 0.5 (-0.5 \log_2(0.5) - 0.5 \log_2(0.5) = 0.5$$$$\frac{\(S_{x3})}{\(S)} \text{Entropy}(S) = 0.25 (-0 \log_2(0) - 1 \log_2 1) = 0$$Therefore, the information gain is:$$G(S,X) = 0.811 - (0 + 0.5 + 0) = 0.311$$ This algorithm is implemented in the following function:<jupyter_code>import numpy as np

def info_gain(X, y, feature):
    # Calculates the information gain based on entropy
    
    gain = 0
    n = len(X)

    # List the values that feature can take
    values = list(set(X[feature]))

    feature_counts = np.zeros(len(values))
    E = np.zeros(len(values))
    ivalue = 0
    
    # Find where those values appear in X[feature] and the corresponding class
    for value in values:
        
        new_y = [y[i] for i,d in enumerate(X[feature].values) if d==value]
        feature_counts[ivalue] += len(new_y)

        # Get the values in newClasses
        class_values = list(set(new_y))

        class_counts = np.zeros(len(class_values))
        iclass = 0
        for v in class_values:
            for c in new_y:
                if c == v:
                    class_counts[iclass] += 1 
            iclass += 1
        
        nc = float(np.sum(class_counts))
        new_entropy = entropy([class_counts[c] / nc for c in range(len(class_values))])
        E[ivalue] += new_entropy

        # Computes both the Gini gain and the entropy
        gain += float(feature_counts[ivalue])/n * E[ivalue]
        ivalue += 1
        
    return gain<jupyter_output><empty_output><jupyter_text>Consider a few variables from the titanic database:<jupyter_code>titanic = pd.read_excel("../data/titanic.xls", "titanic")
titanic.head(3)<jupyter_output><empty_output><jupyter_text>Here, we have selected pasenger class (`pclass`), sex, port of embarcation (`embarked`), and a derived variable called `adult`. We can calculate the information gain for each of these.<jupyter_code>X = titanic.copy()
y = X.pop('survived')
X['adult'] = titanic.age < 17

info_gain(X, y, 'pclass')
info_gain(X, y, 'sex')
info_gain(X, y, 'embarked')
info_gain(X, y, 'adult')<jupyter_output><empty_output><jupyter_text>Hence, the ID3 algorithm computes the information gain for each variable, selecting the one with the highest value (in this case, `adult`). In this way, it searches the "tree space" according to a greedy strategy.A tree can be constructed by recursively selecting the feature from the current dataset with the largest information gain, then removing it from the datset. Recursion stops when there are either no variables remaining, or there is only one class left in the subset (*e.g.* all `True` or all `False`).The ID3 algorithm is as follows:> * if all response data have the same class:> >     - return leaf with data label>     > * else if no features:> >     - return leaf with most common label>     > * else:> >     - choose variable $X'$ that maximizes information gain to be a tree node>     - add branch from node for each value of $X'$>     - for each branch of node:>     >         * calculate $S_{x}$ by removing $X'$ from $S$>         * set $S=S_{x}$ and call algorithm again The greedy approach of maximizing information gain at each step tends to bias solutions towards smaller trees.  Decision Trees in scikit-learnClassification trees, either binary or multi-class, are implemented in `scikit-learn` in the `DecisionTreeClassifier` class. Where trees are binary, it expects the response variable to be coded as `[-1,1]` for negative and positive outcomes.Let's build a decision tree on the wine dataset.<jupyter_code>wine = pd.read_table("../data/wine.dat", sep='\s+')

attributes = [
    'Alcohol',
    'Malic acid',
    'Ash',
    'Alcalinity of ash',
    'Magnesium',
    'Total phenols',
    'Flavanoids',
    'Nonflavanoid phenols',
    'Proanthocyanins',
    'Color intensity',
    'Hue',
    'OD280/OD315 of diluted wines',
    'Proline'
]

grape = wine.pop('region')
y = grape
wine.columns = attributes
X = wine
from sklearn import tree
from sklearn import model_selection

X_train, X_test, y_train, y_test = model_selection.train_test_split(
        X, y, test_size=0.4, random_state=0)

clf = tree.DecisionTreeClassifier(criterion='entropy',
                                  max_features="auto",
                                  min_samples_leaf=10)
clf.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>If you have [GraphViz](http://www.graphviz.org) installed, you can draw the resulting tree:<jupyter_code>with open("wine.dot", 'w') as f:
    f = tree.export_graphviz(clf, out_file=f)
! dot -Tpng wine.dot -o wine.png
for i,x in enumerate(X.columns):
    print(i,x)<jupyter_output><empty_output><jupyter_text>In order to render graphs as images, you will need to install the `pydot` package:<jupyter_code>!pip install pydot2
from IPython.core.display import Image
Image("wine.png")
preds = clf.predict(X_test)
pd.crosstab(y_test, preds, rownames=['actual'], 
            colnames=['prediction'])<jupyter_output><empty_output><jupyter_text>PruningDespite the **inductive bias** associated with trees that tend to make them small, the ID3 algorithm continues choosing nodes and branches until either it runs out of variables, or all outputs are of the same class. This can clearly lead to overfit trees.To prevent overfitting, we can **stop growing** the tree if the information gain (or reduction in error, etc.) is not sufficient to justify the extra complexity of adding another node. However, this simple rule is not optimal, because an uninformative subtree can lead to informative ones later on. The standard approach is therefore to grow a full tree, and then to **prune** it. The easiest approach is to remove branches that give the least increase in the error (information gain). To determine how far back to prune, we can evaluate the cross-validated error on each candidate pruning, and then pick the tree whose CV error is within 1 standard error of the minimum.|Analogous to the lasso or ridge regression, we can **penalize** the number of terminal nodes in a tree:$$\sum_{m=1}^{|T|} \sum_{x_i \in R_m} (y_i - \hat{y}_{R_j})^2 + \alpha |T|$$where $|T|$ is the number of terminal nodes in tree T. Pruned Decision Tree Algorithm1. Use recursive binary splitting to grow a large tree, such that each terminal node has fewer than some minimum number of observations.2. Apply pruning to obtain a sequence of best subtrees, as a function of $\alpha$.3. Use k-fold cross-validation to choose $\alpha$. Average results and pick $\alpha$ to minimize the average error.4. Return subtree from (2) that corresponds to chosen $\alpha$.   Averaging TreesDecision trees have several advantages: * ease of interpretation* easy of implementation* handles continuous and discrete features (and mixed)* invariant to monotone transformation of features* variable selection automated (ignores redundant variables)* robust* scalable (can handle huge datasets)However, relative to other statistical learning methods, trees do not predict very accurately, due to the greedy nature of the tree construction algorithm. Also, trees tend to be **unstable**, as small changes to the inputs can have large effects on the structure of the tree; poor decisions near the root of the tree will propogate to the rest of the tree. Hence, trees are **high variance** (*i.e.* noisy) estimators.One way to reduce the variance of an estimate is to average together many estimates. In the case of decision trees, we can train $T$ different trees on random subsets of the data (with replacement) then average according to:$$\hat{f}(\mathbf{x}) = \frac{1}{T} \sum_{i=1}^T f_t(\mathbf{x})$$where $f_t$ is the $t^{th}$ tree. This approach is called "bootstrap aggregating", or **bagging**.Note that, since we are averaging over trees, there is *no need to prune*. With bagging, we reduce variance by averaging, rather than by pruning.<jupyter_code>from sklearn.ensemble import BaggingClassifier

bc = BaggingClassifier(n_jobs=4, n_estimators=20, oob_score=True)
bc
bc.fit(X_train, y_train)

preds = bc.predict(X_test)
pd.crosstab(y_test, preds, rownames=['actual'], 
            colnames=['prediction'])<jupyter_output><empty_output><jupyter_text>Test error of a bagged model is measured by estimating **out-of-bag error**.On average, each bagged tree uses about 2/3 of observations, leaving the remaining third as "out-of bag". The response for the ith observation for each of the trees in which that observation was excluded (on average, B/3) is averaged. This essentially the same as performing leave-one-out (LOO) cross-validation.<jupyter_code>bc.oob_score_<jupyter_output><empty_output><jupyter_text>This approach is an **ensemble learning** method, because it takes a set of *weak* learners, and combines them to construct a *strong* learner that is more robust, with lower generalization error.An average of B trees, each with variance $\sigma^2$, has variance $\sigma^2/B$. If the variables are simply identically distributed, with positive pairwise correlation $\rho$, then the variance of the average of the B trees is:$$\rho \sigma^2 + \frac{1-\rho}{B}\sigma^2$$ As the number of trees becomes large, the second term goes to zero. Further reductions in variance are limited by the size of the **correlation** among the trees $\rho$.  Random Forests**Random forests** improves upon bagging by creating a set of decision trees that are less correlated than bootstrapped trees. This is done by selecting from only a subset $m$ out of $M$ possible predictors at each split. Typically, we choose approximately the square root of the available number.This procedure is used to create a set of trees, most of which will be poor at predicting a given observation. However, classification is based on a *majority vote* of the constituent trees.<jupyter_code>from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier(n_jobs=4)
rf.fit(X_train, y_train)

preds = rf.predict(X_test)
pd.crosstab(y_test, preds, rownames=['actual'], 
            colnames=['prediction'])<jupyter_output><empty_output><jupyter_text>With random forests, it is possible to quantify the **relative importance** of feature inputs for classification. In scikit-learn, the Gini index (recall, a measure of error reduction) is calculated for each internal node that splits on a particular feature of a given tree, which is multiplied by the number of samples that were routed to the node (this approximates the probability of reaching that node). For each variable, this quantity is averaged over the trees in the forest to yield a measure of importance.<jupyter_code>importances = rf.feature_importances_
indices = np.argsort(importances)[::-1]

# Print the feature ranking
print("Feature ranking:")

for f in range(X.shape[1]):
    print("%d. %s (%f)" % (f + 1, X.columns[indices[f]], importances[indices[f]]))
plt.figure()
plt.title("Feature importances")
plt.bar(range(X.shape[1]), importances[indices],
       color="r", align="center")
plt.xticks(range(X.shape[1]), X.columns[indices], rotation=90)
plt.xlim([-1, X.shape[1]]);<jupyter_output><empty_output><jupyter_text>`RandomForestClassifier` uses the Gini impurity index by default; one may instead use the entropy information gain as a criterion.<jupyter_code>rf = RandomForestClassifier(n_jobs=4, criterion='entropy')
rf.fit(X_train, y_train)
importances = rf.feature_importances_
indices = np.argsort(importances)[::-1]

# Print the feature ranking
print("Feature ranking:")

for f in range(X.shape[1]):
    print("%d. %s (%f)" % (f + 1, X.columns[indices[f]], importances[indices[f]]))<jupyter_output><empty_output><jupyter_text>Exercise: Classifying IncomeUsing the US Census, predict whether someone has income over 50k.You'll need to pre-process the data. Create dummy variables (unfortunately). Binarize the target.Fit a `DecisionTreeClassifier` and a `RandomForestClassifier` to the data. There is a holdout set that you can test the performance of your classifier on using `sklearn.metrics.accuracy_score`.<jupyter_code>with open("../data/adult.names") as fin:
    notes = fin.read()
    
print(notes)
dta = pd.read_csv("../data/adult.data.cleaned.csv.gz", compression="gzip")
test = pd.read_csv("../data/adult.test.cleaned.csv.gz", compression="gzip")
del dta['fnlwgt']
del dta['education_num']
del test['fnlwgt']
del test['education_num']
dta.head()
y = dta.pop('income')
y_test = test.pop('income')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1088910177455408, 'fraction_numerical': 0.011541444277177137, 'mean_word_length': 4.708854166666667, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '263298', 'license': 'MIT', 'n_tokens_mistral': 6885, 'n_tokens_neox': 6551, 'n_words': 2692, 'path': 'notebooks/Day3_1-Decision-Trees-and-Random-Forests.ipynb', 'repo_name': 'vcalderon2009/cqs_machine_learning', 'size': '21921'}