1915 lines
74 KiB
Plaintext
1915 lines
74 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"<!-- dom:TITLE: week 44: From Decision Trees to Bagging methods -->\n",
|
||
"# week 44: From Decision Trees to Bagging methods\n",
|
||
"<!-- dom:AUTHOR: Morten Hjorth-Jensen at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University -->\n",
|
||
"<!-- Author: --> \n",
|
||
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
|
||
"\n",
|
||
"Date: **Sep 16, 2020**\n",
|
||
"\n",
|
||
"Copyright 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"## Decision trees, overarching aims\n",
|
||
"\n",
|
||
"\n",
|
||
"We start here with the most basic algorithm, the so-called decision\n",
|
||
"tree. With this basic algorithm we can in turn build more complex\n",
|
||
"networks, spanning from homogeneous and heterogenous forests (bagging,\n",
|
||
"random forests and more) to one of the most popular supervised\n",
|
||
"algorithms nowadays, the extreme gradient boosting, or just\n",
|
||
"XGBoost. But let us start with the simplest possible ingredient.\n",
|
||
"\n",
|
||
"Decision trees are supervised learning algorithms used for both,\n",
|
||
"classification and regression tasks.\n",
|
||
"\n",
|
||
"\n",
|
||
"The main idea of decision trees\n",
|
||
"is to find those descriptive features which contain the most\n",
|
||
"**information** regarding the target feature and then split the dataset\n",
|
||
"along the values of these features such that the target feature values\n",
|
||
"for the resulting underlying datasets are as pure as possible.\n",
|
||
"\n",
|
||
"The descriptive features which reproduce best the target/output features are normally said\n",
|
||
"to be the most informative ones. The process of finding the **most\n",
|
||
"informative** feature is done until we accomplish a stopping criteria\n",
|
||
"where we then finally end up in so called **leaf nodes**. \n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"A decision tree is typically divided into a **root node**, the **interior nodes**,\n",
|
||
"and the final **leaf nodes** or just **leaves**. These entities are then connected by so-called **branches**.\n",
|
||
"\n",
|
||
"The leaf nodes\n",
|
||
"contain the predictions we will make for new query instances presented\n",
|
||
"to our trained model. This is possible since the model has \n",
|
||
"learned the underlying structure of the training data and hence can,\n",
|
||
"given some assumptions, make predictions about the target feature value\n",
|
||
"(class) of unseen query instances.\n",
|
||
"\n",
|
||
"## A typical Decision Tree with its pertinent Jargon, Classification Problem\n",
|
||
"\n",
|
||
"<!-- dom:FIGURE: [DataFiles/cancer.png, width=600 frac=0.8] -->\n",
|
||
"<!-- begin figure -->\n",
|
||
"\n",
|
||
"<p></p>\n",
|
||
"<img src=\"DataFiles/cancer.png\" width=600>\n",
|
||
"\n",
|
||
"<!-- end figure -->\n",
|
||
"\n",
|
||
"\n",
|
||
"This tree was produced using the Wisconsin cancer data (discussed here as well, see code examples below) using **Scikit-Learn**'s decision tree classifier. Here we have used the so-called **gini** index (see below) to split the various branches.\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"## General Features\n",
|
||
"\n",
|
||
"The overarching approach to decision trees is a top-down approach.\n",
|
||
"\n",
|
||
"* A leaf provides the classification of a given instance.\n",
|
||
"\n",
|
||
"* A node specifies a test of some attribute of the instance.\n",
|
||
"\n",
|
||
"* A branch corresponds to a possible values of an attribute.\n",
|
||
"\n",
|
||
"* An instance is classified by starting at the root node of the tree, testing the attribute specified by this node, then moving down the tree branch corresponding to the value of the attribute in the given example.\n",
|
||
"\n",
|
||
"This process is then repeated for the subtree rooted at the new\n",
|
||
"node.\n",
|
||
"\n",
|
||
"\n",
|
||
"## How do we set it up?\n",
|
||
"\n",
|
||
"\n",
|
||
"In simplified terms, the process of training a decision tree and\n",
|
||
"predicting the target features of query instances is as follows:\n",
|
||
"\n",
|
||
"1. Present a dataset containing of a number of training instances characterized by a number of descriptive features and a target feature\n",
|
||
"\n",
|
||
"2. Train the decision tree model by continuously splitting the target feature along the values of the descriptive features using a measure of information gain during the training process\n",
|
||
"\n",
|
||
"3. Grow the tree until we accomplish a stopping criteria create leaf nodes which represent the *predictions* we want to make for new query instances\n",
|
||
"\n",
|
||
"4. Show query instances to the tree and run down the tree until we arrive at leaf nodes\n",
|
||
"\n",
|
||
"Then we are essentially done!\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"## Decision trees and Regression"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"%matplotlib inline\n",
|
||
"\n",
|
||
"import numpy as np\n",
|
||
"import matplotlib.pyplot as plt\n",
|
||
"from sklearn.preprocessing import PolynomialFeatures\n",
|
||
"from sklearn.linear_model import LinearRegression\n",
|
||
"\n",
|
||
"steps=250\n",
|
||
"\n",
|
||
"distance=0\n",
|
||
"x=0\n",
|
||
"distance_list=[]\n",
|
||
"steps_list=[]\n",
|
||
"while x<steps:\n",
|
||
" distance+=np.random.randint(-1,2)\n",
|
||
" distance_list.append(distance)\n",
|
||
" x+=1\n",
|
||
" steps_list.append(x)\n",
|
||
"plt.plot(steps_list,distance_list, color='green', label=\"Random Walk Data\")\n",
|
||
"\n",
|
||
"steps_list=np.asarray(steps_list)\n",
|
||
"distance_list=np.asarray(distance_list)\n",
|
||
"\n",
|
||
"X=steps_list[:,np.newaxis]\n",
|
||
"\n",
|
||
"#Polynomial fits\n",
|
||
"\n",
|
||
"#Degree 2\n",
|
||
"poly_features=PolynomialFeatures(degree=2, include_bias=False)\n",
|
||
"X_poly=poly_features.fit_transform(X)\n",
|
||
"\n",
|
||
"lin_reg=LinearRegression()\n",
|
||
"poly_fit=lin_reg.fit(X_poly,distance_list)\n",
|
||
"b=lin_reg.coef_\n",
|
||
"c=lin_reg.intercept_\n",
|
||
"print (\"2nd degree coefficients:\")\n",
|
||
"print (\"zero power: \",c)\n",
|
||
"print (\"first power: \", b[0])\n",
|
||
"print (\"second power: \",b[1])\n",
|
||
"\n",
|
||
"z = np.arange(0, steps, .01)\n",
|
||
"z_mod=b[1]*z**2+b[0]*z+c\n",
|
||
"\n",
|
||
"fit_mod=b[1]*X**2+b[0]*X+c\n",
|
||
"plt.plot(z, z_mod, color='r', label=\"2nd Degree Fit\")\n",
|
||
"plt.title(\"Polynomial Regression\")\n",
|
||
"\n",
|
||
"plt.xlabel(\"Steps\")\n",
|
||
"plt.ylabel(\"Distance\")\n",
|
||
"\n",
|
||
"#Degree 10\n",
|
||
"poly_features10=PolynomialFeatures(degree=10, include_bias=False)\n",
|
||
"X_poly10=poly_features10.fit_transform(X)\n",
|
||
"\n",
|
||
"poly_fit10=lin_reg.fit(X_poly10,distance_list)\n",
|
||
"\n",
|
||
"y_plot=poly_fit10.predict(X_poly10)\n",
|
||
"plt.plot(X, y_plot, color='black', label=\"10th Degree Fit\")\n",
|
||
"\n",
|
||
"plt.legend()\n",
|
||
"plt.show()\n",
|
||
"\n",
|
||
"\n",
|
||
"#Decision Tree Regression\n",
|
||
"from sklearn.tree import DecisionTreeRegressor\n",
|
||
"regr_1=DecisionTreeRegressor(max_depth=2)\n",
|
||
"regr_2=DecisionTreeRegressor(max_depth=5)\n",
|
||
"regr_3=DecisionTreeRegressor(max_depth=7)\n",
|
||
"regr_1.fit(X, distance_list)\n",
|
||
"regr_2.fit(X, distance_list)\n",
|
||
"regr_3.fit(X, distance_list)\n",
|
||
"\n",
|
||
"X_test = np.arange(0.0, steps, 0.01)[:, np.newaxis]\n",
|
||
"y_1 = regr_1.predict(X_test)\n",
|
||
"y_2 = regr_2.predict(X_test)\n",
|
||
"y_3=regr_3.predict(X_test)\n",
|
||
"\n",
|
||
"# Plot the results\n",
|
||
"plt.figure()\n",
|
||
"plt.scatter(X, distance_list, s=2.5, c=\"black\", label=\"data\")\n",
|
||
"plt.plot(X_test, y_1, color=\"red\",\n",
|
||
" label=\"max_depth=2\", linewidth=2)\n",
|
||
"plt.plot(X_test, y_2, color=\"green\", label=\"max_depth=5\", linewidth=2)\n",
|
||
"plt.plot(X_test, y_3, color=\"m\", label=\"max_depth=7\", linewidth=2)\n",
|
||
"\n",
|
||
"plt.xlabel(\"Data\")\n",
|
||
"plt.ylabel(\"Darget\")\n",
|
||
"plt.title(\"Decision Tree Regression\")\n",
|
||
"plt.legend()\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Building a tree, regression\n",
|
||
"\n",
|
||
"There are mainly two steps\n",
|
||
"1. We split the predictor space (the set of possible values $x_1,x_2,\\dots, x_p$) into $J$ distinct and non-non-overlapping regions, $R_1,R_2,\\dots,R_J$. \n",
|
||
"\n",
|
||
"2. For every observation that falls into the region $R_j$ , we make the same prediction, which is simply the mean of the response values for the training observations in $R_j$.\n",
|
||
"\n",
|
||
"How do we construct the regions $R_1,\\dots,R_J$? In theory, the\n",
|
||
"regions could have any shape. However, we choose to divide the\n",
|
||
"predictor space into high-dimensional rectangles, or boxes, for\n",
|
||
"simplicity and for ease of interpretation of the resulting predictive\n",
|
||
"model. The goal is to find boxes $R_1,\\dots,R_J$ that minimize the\n",
|
||
"MSE, given by"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"\\sum_{j=1}^J\\sum_{i\\in R_j}(y_i-\\overline{y}_{R_j})^2,\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"where $\\overline{y}_{R_j}$ is the mean response for the training observations \n",
|
||
"within box $j$. \n",
|
||
"\n",
|
||
"## A top-down approach, recursive binary splitting\n",
|
||
"\n",
|
||
"Unfortunately, it is computationally infeasible to consider every\n",
|
||
"possible partition of the feature space into $J$ boxes. The common\n",
|
||
"strategy is to take a top-down approach\n",
|
||
"\n",
|
||
"The approach is top-down because it begins at the top of the tree (all\n",
|
||
"observations belong to a single region) and then successively splits\n",
|
||
"the predictor space; each split is indicated via two new branches\n",
|
||
"further down on the tree. It is greedy because at each step of the\n",
|
||
"tree-building process, the best split is made at that particular step,\n",
|
||
"rather than looking ahead and picking a split that will lead to a\n",
|
||
"better tree in some future step.\n",
|
||
"\n",
|
||
"## Making a tree\n",
|
||
"\n",
|
||
"In order to implement the recursive binary splitting we start by selecting\n",
|
||
"the predictor $x_j$ and a cutpoint $s$ that splits the predictor space into two regions $R_1$ and $R_2$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"\\left\\{X\\vert x_j < s\\right\\},\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"and"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"\\left\\{X\\vert x_j \\geq s\\right\\},\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"so that we obtain the lowest MSE, that is"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"\\sum_{i:x_i\\in R_j}(y_i-\\overline{y}_{R_1})^2+\\sum_{i:x_i\\in R_2}(y_i-\\overline{y}_{R_2})^2,\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"which we want to minimize by considering all predictors\n",
|
||
"$x_1,x_2,\\dots,x_p$. We consider also all possible values of $s$ for\n",
|
||
"each predictor. These values could be determined by randomly assigned\n",
|
||
"numbers or by starting at the midpoint and then proceed till we find\n",
|
||
"an optimal value.\n",
|
||
"\n",
|
||
"For any $j$ and $s$, we define the pair of half-planes where\n",
|
||
"$\\overline{y}_{R_1}$ is the mean response for the training\n",
|
||
"observations in $R_1(j,s)$, and $\\overline{y}_{R_2}$ is the mean\n",
|
||
"response for the training observations in $R_2(j,s)$.\n",
|
||
"\n",
|
||
"Finding the values of $j$ and $s$ that minimize the above equation can be\n",
|
||
"done quite quickly, especially when the number of features $p$ is not\n",
|
||
"too large.\n",
|
||
"\n",
|
||
"Next, we repeat the process, looking\n",
|
||
"for the best predictor and best cutpoint in order to split the data\n",
|
||
"further so as to minimize the MSE within each of the resulting\n",
|
||
"regions. However, this time, instead of splitting the entire predictor\n",
|
||
"space, we split one of the two previously identified regions. We now\n",
|
||
"have three regions. Again, we look to split one of these three regions\n",
|
||
"further, so as to minimize the MSE. The process continues until a\n",
|
||
"stopping criterion is reached; for instance, we may continue until no\n",
|
||
"region contains more than five observations.\n",
|
||
"\n",
|
||
"<!-- !split -->\n",
|
||
"## Pruning the tree\n",
|
||
"\n",
|
||
"The above procedure is rather straightforward, but leads often to\n",
|
||
"overfitting and unnecessarily large and complicated trees. The basic\n",
|
||
"idea is to grow a large tree $T_0$ and then prune it back in order to\n",
|
||
"obtain a subtree. A smaller tree with fewer splits (fewer regions) can\n",
|
||
"lead to smaller variance and better interpretation at the cost of a\n",
|
||
"little more bias.\n",
|
||
"\n",
|
||
"The so-called Cost complexity pruning algorithm gives us a\n",
|
||
"way to do just this. Rather than considering every possible subtree,\n",
|
||
"we consider a sequence of trees indexed by a nonnegative tuning\n",
|
||
"parameter $\\alpha$.\n",
|
||
"\n",
|
||
"## Cost complexity pruning\n",
|
||
"For each value of $\\alpha$ there corresponds a subtree $T \\in T_0$ such that"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"\\sum_{m=1}^{\\overline{T}}\\sum_{i:x_i\\in R_m}(y_i-\\overline{y}_{R_m})^2+\\alpha\\overline{T},\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"is as small as possible. Here $\\overline{T}$ is \n",
|
||
"the number of terminal nodes of the tree $T$ , $R_m$ is the\n",
|
||
"rectangle (i.e. the subset of predictor space) corresponding to the $m$-th terminal node.\n",
|
||
"\n",
|
||
"The tuning parameter $\\alpha$ controls a trade-off between the subtree’s\n",
|
||
"com- plexity and its fit to the training data. When $\\alpha = 0$, then the\n",
|
||
"subtree $T$ will simply equal $T_0$, \n",
|
||
"because then the above equation just measures the\n",
|
||
"training error. \n",
|
||
"However, as $\\alpha$ increases, there is a price to pay for\n",
|
||
"having a tree with many terminal nodes. The above equation will\n",
|
||
"tend to be minimized for a smaller subtree. \n",
|
||
"\n",
|
||
"\n",
|
||
"It turns out that as we increase $\\alpha$ from zero\n",
|
||
"branches get pruned from the tree in a nested and predictable fashion,\n",
|
||
"so obtaining the whole sequence of subtrees as a function of $\\alpha$ is\n",
|
||
"easy. We can select a value of $\\alpha$ using a validation set or using\n",
|
||
"cross-validation. We then return to the full data set and obtain the\n",
|
||
"subtree corresponding to $\\alpha$. \n",
|
||
"\n",
|
||
"\n",
|
||
"## Schematic Regression Procedure\n",
|
||
"\n",
|
||
"**Building a Regression Tree.**\n",
|
||
"\n",
|
||
"1. Use recursive binary splitting to grow a large tree on the training data, stopping only when each terminal node has fewer than some minimum number of observations.\n",
|
||
"\n",
|
||
"2. Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of $\\alpha$.\n",
|
||
"\n",
|
||
"3. Use for example $K$-fold cross-validation to choose $\\alpha$. Divide the training observations into $K$ folds. For each $k=1,2,\\dots,K$ we: \n",
|
||
"\n",
|
||
" * repeat steps 1 and 2 on all but the $k$-th fold of the training data. \n",
|
||
"\n",
|
||
" * Then we valuate the mean squared prediction error on the data in the left-out $k$-th fold, as a function of $\\alpha$.\n",
|
||
"\n",
|
||
" * Finally we average the results for each value of $\\alpha$, and pick $\\alpha$ to minimize the average error.\n",
|
||
"\n",
|
||
"\n",
|
||
"4. Return the subtree from Step 2 that corresponds to the chosen value of $\\alpha$.\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"## A Classification Tree\n",
|
||
"\n",
|
||
"A classification tree is very similar to a regression tree, except\n",
|
||
"that it is used to predict a qualitative response rather than a\n",
|
||
"quantitative one. Recall that for a regression tree, the predicted\n",
|
||
"response for an observation is given by the mean response of the\n",
|
||
"training observations that belong to the same terminal node. In\n",
|
||
"contrast, for a classification tree, we predict that each observation\n",
|
||
"belongs to the most commonly occurring class of training observations\n",
|
||
"in the region to which it belongs. In interpreting the results of a\n",
|
||
"classification tree, we are often interested not only in the class\n",
|
||
"prediction corresponding to a particular terminal node region, but\n",
|
||
"also in the class proportions among the training observations that\n",
|
||
"fall into that region. \n",
|
||
"\n",
|
||
"## Growing a classification tree\n",
|
||
"\n",
|
||
"The task of growing a\n",
|
||
"classification tree is quite similar to the task of growing a\n",
|
||
"regression tree. Just as in the regression setting, we use recursive\n",
|
||
"binary splitting to grow a classification tree. However, in the\n",
|
||
"classification setting, the MSE cannot be used as a criterion for making\n",
|
||
"the binary splits. A natural alternative to MSE is the **classification\n",
|
||
"error rate**. Since we plan to assign an observation in a given region\n",
|
||
"to the most commonly occurring error rate class of training\n",
|
||
"observations in that region, the classification error rate is simply\n",
|
||
"the fraction of the training observations in that region that do not\n",
|
||
"belong to the most common class. \n",
|
||
"\n",
|
||
"When building a classification tree, either the Gini index or the\n",
|
||
"entropy are typically used to evaluate the quality of a particular\n",
|
||
"split, since these two approaches are more sensitive to node purity\n",
|
||
"than is the classification error rate. \n",
|
||
"\n",
|
||
"\n",
|
||
"## Classification tree, how to split nodes\n",
|
||
"\n",
|
||
"If our targets are the outcome of a classification process that takes\n",
|
||
"for example $k=1,2,\\dots,K$ values, the only thing we need to think of\n",
|
||
"is to set up the splitting criteria for each node.\n",
|
||
"\n",
|
||
"We define a PDF $p_{mk}$ that represents the number of observations of\n",
|
||
"a class $k$ in a region $R_m$ with $N_m$ observations. We represent\n",
|
||
"this likelihood function in terms of the proportion $I(y_i=k)$ of\n",
|
||
"observations of this class in the region $R_m$ as"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"p_{mk} = \\frac{1}{N_m}\\sum_{x_i\\in R_m}I(y_i=k).\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"We let $p_{mk}$ represent the majority class of observations in region\n",
|
||
"$m$. The three most common ways of splitting a node are given by\n",
|
||
"\n",
|
||
"* Misclassification error"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"p_{mk} = \\frac{1}{N_m}\\sum_{x_i\\in R_m}I(y_i\\ne k) = 1-p_{mk}.\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"* Gini index $g$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"g = \\sum_{k=1}^K p_{mk}(1-p_{mk}).\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"* Information entropy or just entropy $s$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"s = -\\sum_{k=1}^K p_{mk}\\log{p_{mk}}.\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Visualizing the Tree, Classification"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import os\n",
|
||
"from sklearn.datasets import load_breast_cancer\n",
|
||
"from sklearn.tree import DecisionTreeClassifier\n",
|
||
"from sklearn.model_selection import train_test_split\n",
|
||
"from sklearn.metrics import confusion_matrix\n",
|
||
"from sklearn.tree import export_graphviz\n",
|
||
"\n",
|
||
"from IPython.display import Image \n",
|
||
"from pydot import graph_from_dot_data\n",
|
||
"import pandas as pd\n",
|
||
"import numpy as np\n",
|
||
"\n",
|
||
"\n",
|
||
"cancer = load_breast_cancer()\n",
|
||
"X = pd.DataFrame(cancer.data, columns=cancer.feature_names)\n",
|
||
"print(X)\n",
|
||
"y = pd.Categorical.from_codes(cancer.target, cancer.target_names)\n",
|
||
"y = pd.get_dummies(y)\n",
|
||
"print(y)\n",
|
||
"X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)\n",
|
||
"tree_clf = DecisionTreeClassifier(max_depth=5)\n",
|
||
"tree_clf.fit(X_train, y_train)\n",
|
||
"\n",
|
||
"export_graphviz(\n",
|
||
" tree_clf,\n",
|
||
" out_file=\"DataFiles/cancer.dot\",\n",
|
||
" feature_names=cancer.feature_names,\n",
|
||
" class_names=cancer.target_names,\n",
|
||
" rounded=True,\n",
|
||
" filled=True\n",
|
||
")\n",
|
||
"cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'\n",
|
||
"os.system(cmd)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Visualizing the Tree, The Moons"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Common imports\n",
|
||
"import numpy as np\n",
|
||
"from sklearn.model_selection import train_test_split \n",
|
||
"from sklearn.tree import DecisionTreeClassifier\n",
|
||
"from sklearn.datasets import make_moons\n",
|
||
"from sklearn.tree import export_graphviz\n",
|
||
"from pydot import graph_from_dot_data\n",
|
||
"import pandas as pd\n",
|
||
"import os\n",
|
||
"\n",
|
||
"np.random.seed(42)\n",
|
||
"X, y = make_moons(n_samples=100, noise=0.25, random_state=53)\n",
|
||
"X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0)\n",
|
||
"tree_clf = DecisionTreeClassifier(max_depth=5)\n",
|
||
"tree_clf.fit(X_train, y_train)\n",
|
||
"\n",
|
||
"export_graphviz(\n",
|
||
" tree_clf,\n",
|
||
" out_file=\"DataFiles/moons.dot\",\n",
|
||
" rounded=True,\n",
|
||
" filled=True\n",
|
||
")\n",
|
||
"cmd = 'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png'\n",
|
||
"os.system(cmd)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Algorithms for Setting up Decision Trees\n",
|
||
"\n",
|
||
"Two algorithms stand out in the set up of decision trees:\n",
|
||
"1. The CART (Classification And Regression Tree) algorithm for both classification and regression\n",
|
||
"\n",
|
||
"2. The ID3 algorithm based on the computation of the information gain for classification\n",
|
||
"\n",
|
||
"We discuss both algorithms with applications here. The popular library\n",
|
||
"**Scikit-Learn** uses the CART algorithm. For classification problems\n",
|
||
"you can use either the **gini** index or the **entropy** to split a tree\n",
|
||
"in two branches.\n",
|
||
"\n",
|
||
"## The CART algorithm for Classification\n",
|
||
"\n",
|
||
"For classification, the CART algorithm splits the data set in two subsets using a single feature $k$ and a threshold $t_k$.\n",
|
||
"This could be for example a threshold set by a number below a certain circumference of a malign tumor.\n",
|
||
"\n",
|
||
"How do we find these two quantities?\n",
|
||
"We search for the pair $(k,t_k)$ that produces the purest subset using for example the **gini** factor $G$.\n",
|
||
"The cost function it tries to minimize is then"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"C(k,t_k) = \\frac{m_{\\mathrm{left}}}{m}G_{\\mathrm{left}}+ \\frac{m_{\\mathrm{right}}}{m}G_{\\mathrm{right}},\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"where $G_{\\mathrm{left/right}}$ measures the impurity of the left/right subset and $m_{\\mathrm{left/right}}$\n",
|
||
" is the number of instances in the left/right subset\n",
|
||
"\n",
|
||
"Once it has successfully split the training set in two, it splits the subsets using the same logic, then the subsubsets\n",
|
||
"and so on, recursively. It stops recursing once it reaches the maximum depth (defined by the\n",
|
||
"$max\\_depth$ hyperparameter), or if it cannot find a split that will reduce impurity. A few other\n",
|
||
"hyperparameters control additional stopping conditions such as the $min\\_samples\\_split$,\n",
|
||
"$min\\_samples\\_leaf$, $min\\_weight\\_fraction\\_leaf$, and $max\\_leaf\\_nodes$.\n",
|
||
"\n",
|
||
"## The CART algorithm for Regression\n",
|
||
"\n",
|
||
"The CART algorithm for regression works is similar to the one for classification except that instead of trying to split the\n",
|
||
"training set in a way that minimizes say the **gini** or **entropy** impurity, it now tries to split the training set in a way that minimizes our well-known mean-squared error (MSE). The cost function is now"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"C(k,t_k) = \\frac{m_{\\mathrm{left}}}{m}\\mathrm{MSE}_{\\mathrm{left}}+ \\frac{m_{\\mathrm{right}}}{m}\\mathrm{MSE}_{\\mathrm{right}}.\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Here the MSE for a specific node is defined as"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"\\mathrm{MSE}_{\\mathrm{node}}=\\frac{1}{m_\\mathrm{node}}\\sum_{i\\in \\mathrm{node}}(\\overline{y}_{\\mathrm{node}}-y_i)^2,\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"with"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"$$\n",
|
||
"\\overline{y}_{\\mathrm{node}}=\\frac{1}{m_\\mathrm{node}}\\sum_{i\\in \\mathrm{node}}y_i,\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"the mean value of all observations in a specific node.\n",
|
||
"\n",
|
||
"Without any regularization, the regression task for decision trees, \n",
|
||
"just like for classification tasks, is prone to overfitting.\n",
|
||
"\n",
|
||
"\n",
|
||
"## Computing the Gini index\n",
|
||
"\n",
|
||
"The example we will look at is a classical one in many Machine\n",
|
||
"Learning applications. Based on various meteorological features, we\n",
|
||
"have several so-called attributes which decide whether we at the end\n",
|
||
"will do some outdoor activity like skiing, going for a bike ride etc\n",
|
||
"etc. The table here contains the feautures **outlook**, **temperature**,\n",
|
||
"**humidity** and **wind**. The target or output is whether we ride\n",
|
||
"(True=1) or whether we do something else that day (False=0). The\n",
|
||
"attributes for each feature are then sunny, overcast and rain for the\n",
|
||
"outlook, hot, cold and mild for temperature, high and normal for\n",
|
||
"humidity and weak and strong for wind.\n",
|
||
"\n",
|
||
"The table here summarizes the various attributes and\n",
|
||
"<table border=\"1\">\n",
|
||
"<thead>\n",
|
||
"<tr><th align=\"center\">Day</th> <th align=\"center\">Outlook </th> <th align=\"center\">Temperature</th> <th align=\"center\">Humidity</th> <th align=\"center\"> Wind </th> <th align=\"center\">Ride</th> </tr>\n",
|
||
"</thead>\n",
|
||
"<tbody>\n",
|
||
"<tr><td align=\"center\"> 1 </td> <td align=\"center\"> Sunny </td> <td align=\"center\"> Hot </td> <td align=\"center\"> High </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 0 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 2 </td> <td align=\"center\"> Sunny </td> <td align=\"center\"> Hot </td> <td align=\"center\"> High </td> <td align=\"center\"> Strong </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 3 </td> <td align=\"center\"> Overcast </td> <td align=\"center\"> Hot </td> <td align=\"center\"> High </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 4 </td> <td align=\"center\"> Rain </td> <td align=\"center\"> Mild </td> <td align=\"center\"> High </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 5 </td> <td align=\"center\"> Rain </td> <td align=\"center\"> Cool </td> <td align=\"center\"> Normal </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 6 </td> <td align=\"center\"> Rain </td> <td align=\"center\"> Cool </td> <td align=\"center\"> Normal </td> <td align=\"center\"> Strong </td> <td align=\"center\"> 0 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 7 </td> <td align=\"center\"> Overcast </td> <td align=\"center\"> Cool </td> <td align=\"center\"> Normal </td> <td align=\"center\"> Strong </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 8 </td> <td align=\"center\"> Sunny </td> <td align=\"center\"> Mild </td> <td align=\"center\"> High </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 0 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 9 </td> <td align=\"center\"> Sunny </td> <td align=\"center\"> Cool </td> <td align=\"center\"> Normal </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 10 </td> <td align=\"center\"> Rain </td> <td align=\"center\"> Mild </td> <td align=\"center\"> Normal </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 11 </td> <td align=\"center\"> Sunny </td> <td align=\"center\"> Mild </td> <td align=\"center\"> Normal </td> <td align=\"center\"> Strong </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 12 </td> <td align=\"center\"> Overcast </td> <td align=\"center\"> Mild </td> <td align=\"center\"> High </td> <td align=\"center\"> Strong </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 13 </td> <td align=\"center\"> Overcast </td> <td align=\"center\"> Hot </td> <td align=\"center\"> Normal </td> <td align=\"center\"> Weak </td> <td align=\"center\"> 1 </td> </tr>\n",
|
||
"<tr><td align=\"center\"> 14 </td> <td align=\"center\"> Rain </td> <td align=\"center\"> Mild </td> <td align=\"center\"> High </td> <td align=\"center\"> Strong </td> <td align=\"center\"> 0 </td> </tr>\n",
|
||
"</tbody>\n",
|
||
"</table>\n",
|
||
"\n",
|
||
"## Simple Python Code to read in Data and perform Classification"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Common imports\n",
|
||
"import numpy as np\n",
|
||
"import pandas as pd\n",
|
||
"import matplotlib.pyplot as plt\n",
|
||
"from sklearn.tree import DecisionTreeClassifier\n",
|
||
"from sklearn.model_selection import train_test_split\n",
|
||
"from sklearn.tree import export_graphviz\n",
|
||
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
|
||
"from sklearn.compose import ColumnTransformer\n",
|
||
"from IPython.display import Image \n",
|
||
"from pydot import graph_from_dot_data\n",
|
||
"import os\n",
|
||
"\n",
|
||
"# Where to save the figures and data files\n",
|
||
"PROJECT_ROOT_DIR = \"Results\"\n",
|
||
"FIGURE_ID = \"Results/FigureFiles\"\n",
|
||
"DATA_ID = \"DataFiles/\"\n",
|
||
"\n",
|
||
"if not os.path.exists(PROJECT_ROOT_DIR):\n",
|
||
" os.mkdir(PROJECT_ROOT_DIR)\n",
|
||
"\n",
|
||
"if not os.path.exists(FIGURE_ID):\n",
|
||
" os.makedirs(FIGURE_ID)\n",
|
||
"\n",
|
||
"if not os.path.exists(DATA_ID):\n",
|
||
" os.makedirs(DATA_ID)\n",
|
||
"\n",
|
||
"def image_path(fig_id):\n",
|
||
" return os.path.join(FIGURE_ID, fig_id)\n",
|
||
"\n",
|
||
"def data_path(dat_id):\n",
|
||
" return os.path.join(DATA_ID, dat_id)\n",
|
||
"\n",
|
||
"def save_fig(fig_id):\n",
|
||
" plt.savefig(image_path(fig_id) + \".png\", format='png')\n",
|
||
"\n",
|
||
"infile = open(data_path(\"rideclass.csv\"),'r')\n",
|
||
"\n",
|
||
"# Read the experimental data with Pandas\n",
|
||
"from IPython.display import display\n",
|
||
"ridedata = pd.read_csv(infile,names = ('Outlook','Temperature','Humidity','Wind','Ride'))\n",
|
||
"ridedata = pd.DataFrame(ridedata)\n",
|
||
"\n",
|
||
"# Features and targets\n",
|
||
"X = ridedata.loc[:, ridedata.columns != 'Ride'].values\n",
|
||
"y = ridedata.loc[:, ridedata.columns == 'Ride'].values\n",
|
||
"\n",
|
||
"# Create the encoder.\n",
|
||
"encoder = OneHotEncoder(handle_unknown=\"ignore\")\n",
|
||
"# Assume for simplicity all features are categorical.\n",
|
||
"encoder.fit(X) \n",
|
||
"# Apply the encoder.\n",
|
||
"X = encoder.transform(X)\n",
|
||
"print(X)\n",
|
||
"# Then do a Classification tree\n",
|
||
"tree_clf = DecisionTreeClassifier(max_depth=2)\n",
|
||
"tree_clf.fit(X, y)\n",
|
||
"print(\"Train set accuracy with Decision Tree: {:.2f}\".format(tree_clf.score(X,y)))\n",
|
||
"#transfer to a decision tree graph\n",
|
||
"export_graphviz(\n",
|
||
" tree_clf,\n",
|
||
" out_file=\"DataFiles/ride.dot\",\n",
|
||
" rounded=True,\n",
|
||
" filled=True\n",
|
||
")\n",
|
||
"cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'\n",
|
||
"os.system(cmd)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Computing the Gini Factor\n",
|
||
"\n",
|
||
"The above functions (gini, entropy and misclassification error) are\n",
|
||
"important components of the so-called CART algorithm. We will discuss\n",
|
||
"this algorithm below after we have discussed the information gain\n",
|
||
"algorithm ID3.\n",
|
||
"\n",
|
||
"In the example here we have converted all our attributes into numerical values $0,1,2$ etc."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Split a dataset based on an attribute and an attribute value\n",
|
||
"def test_split(index, value, dataset):\n",
|
||
"\tleft, right = list(), list()\n",
|
||
"\tfor row in dataset:\n",
|
||
"\t\tif row[index] < value:\n",
|
||
"\t\t\tleft.append(row)\n",
|
||
"\t\telse:\n",
|
||
"\t\t\tright.append(row)\n",
|
||
"\treturn left, right\n",
|
||
" \n",
|
||
"# Calculate the Gini index for a split dataset\n",
|
||
"def gini_index(groups, classes):\n",
|
||
"\t# count all samples at split point\n",
|
||
"\tn_instances = float(sum([len(group) for group in groups]))\n",
|
||
"\t# sum weighted Gini index for each group\n",
|
||
"\tgini = 0.0\n",
|
||
"\tfor group in groups:\n",
|
||
"\t\tsize = float(len(group))\n",
|
||
"\t\t# avoid divide by zero\n",
|
||
"\t\tif size == 0:\n",
|
||
"\t\t\tcontinue\n",
|
||
"\t\tscore = 0.0\n",
|
||
"\t\t# score the group based on the score for each class\n",
|
||
"\t\tfor class_val in classes:\n",
|
||
"\t\t\tp = [row[-1] for row in group].count(class_val) / size\n",
|
||
"\t\t\tscore += p * p\n",
|
||
"\t\t# weight the group score by its relative size\n",
|
||
"\t\tgini += (1.0 - score) * (size / n_instances)\n",
|
||
"\treturn gini\n",
|
||
"\n",
|
||
"# Select the best split point for a dataset\n",
|
||
"def get_split(dataset):\n",
|
||
"\tclass_values = list(set(row[-1] for row in dataset))\n",
|
||
"\tb_index, b_value, b_score, b_groups = 999, 999, 999, None\n",
|
||
"\tfor index in range(len(dataset[0])-1):\n",
|
||
"\t\tfor row in dataset:\n",
|
||
"\t\t\tgroups = test_split(index, row[index], dataset)\n",
|
||
"\t\t\tgini = gini_index(groups, class_values)\n",
|
||
"\t\t\tprint('X%d < %.3f Gini=%.3f' % ((index+1), row[index], gini))\n",
|
||
"\t\t\tif gini < b_score:\n",
|
||
"\t\t\t\tb_index, b_value, b_score, b_groups = index, row[index], gini, groups\n",
|
||
"\treturn {'index':b_index, 'value':b_value, 'groups':b_groups}\n",
|
||
" \n",
|
||
"dataset = [[0,0,0,0,0],\n",
|
||
" [0,0,0,1,1],\n",
|
||
" [1,0,0,0,1],\n",
|
||
" [2,1,0,0,1],\n",
|
||
" [2,2,1,0,1],\n",
|
||
" [2,2,1,1,0],\n",
|
||
" [1,2,1,1,1],\n",
|
||
" [0,1,0,0,0],\n",
|
||
" [0,2,1,0,1],\n",
|
||
" [2,1,1,0,1],\n",
|
||
" [0,1,1,1,1],\n",
|
||
" [1,1,0,1,1],\n",
|
||
" [1,0,1,0,1],\n",
|
||
" [2,1,0,1,0]]\n",
|
||
"\n",
|
||
"split = get_split(dataset)\n",
|
||
"print('Split: [X%d < %.3f]' % ((split['index']+1), split['value']))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Entropy and the ID3 algorithm\n",
|
||
"\n",
|
||
"ID3, learns decision trees by constructing\n",
|
||
"them topdown, beginning with the question **which attribute should be tested at the root of the tree**?\n",
|
||
"\n",
|
||
"1. Each instance attribute is evaluated using a statistical test to determine how well it alone classifies the training examples.\n",
|
||
"\n",
|
||
"2. The best attribute is selected and used as the test at the root node of the tree.\n",
|
||
"\n",
|
||
"3. A descendant of the root node is then created for each possible value of this attribute.\n",
|
||
"\n",
|
||
"4. Training examples are sorted to the appropriate descendant node.\n",
|
||
"\n",
|
||
"5. The entire process is then repeated using the training examples associated with each descendant node to select the best attribute to test at that point in the tree.\n",
|
||
"\n",
|
||
"6. This forms a greedy search for an acceptable decision tree, in which the algorithm never backtracks to reconsider earlier choices. \n",
|
||
"\n",
|
||
"The ID3 algorithm selects, which attribute to test at each node in the\n",
|
||
"tree.\n",
|
||
"\n",
|
||
"We would like to select the attribute that is most useful for classifying\n",
|
||
"examples.\n",
|
||
"\n",
|
||
"What is a good quantitative measure of the worth of an attribute?\n",
|
||
"\n",
|
||
"Information gain measures how well a given attribute separates the\n",
|
||
"training examples according to their target classification.\n",
|
||
"\n",
|
||
"The ID3 algorithm uses this information gain measure to select among the candidate\n",
|
||
"attributes at each step while growing the tree.\n",
|
||
"\n",
|
||
"## Implementing the ID3 Algorithm"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import re\n",
|
||
"import math\n",
|
||
"from collections import deque\n",
|
||
"\n",
|
||
"# x is examples in training set\n",
|
||
"# y is set of targets\n",
|
||
"# label is target attributes\n",
|
||
"# Node is a class which has properties values, childs, and next\n",
|
||
"# root is top node in the decision tree\n",
|
||
"\n",
|
||
"class Node(object):\n",
|
||
"\tdef __init__(self):\n",
|
||
"\t\tself.value = None\n",
|
||
"\t\tself.next = None\n",
|
||
"\t\tself.childs = None\n",
|
||
"\n",
|
||
"# Simple class of Decision Tree\n",
|
||
"# Aimed for who want to learn Decision Tree, so it is not optimized\n",
|
||
"class DecisionTree(object):\n",
|
||
"\tdef __init__(self, sample, attributes, labels):\n",
|
||
"\t\tself.sample = sample\n",
|
||
"\t\tself.attributes = attributes\n",
|
||
"\t\tself.labels = labels\n",
|
||
"\t\tself.labelCodes = None\n",
|
||
"\t\tself.labelCodesCount = None\n",
|
||
"\t\tself.initLabelCodes()\n",
|
||
"\t\t# print(self.labelCodes)\n",
|
||
"\t\tself.root = None\n",
|
||
"\t\tself.entropy = self.getEntropy([x for x in range(len(self.labels))])\n",
|
||
"\n",
|
||
"\tdef initLabelCodes(self):\n",
|
||
"\t\tself.labelCodes = []\n",
|
||
"\t\tself.labelCodesCount = []\n",
|
||
"\t\tfor l in self.labels:\n",
|
||
"\t\t\tif l not in self.labelCodes:\n",
|
||
"\t\t\t\tself.labelCodes.append(l)\n",
|
||
"\t\t\t\tself.labelCodesCount.append(0)\n",
|
||
"\t\t\tself.labelCodesCount[self.labelCodes.index(l)] += 1\n",
|
||
"\n",
|
||
"\tdef getLabelCodeId(self, sampleId):\n",
|
||
"\t\treturn self.labelCodes.index(self.labels[sampleId])\n",
|
||
"\n",
|
||
"\tdef getAttributeValues(self, sampleIds, attributeId):\n",
|
||
"\t\tvals = []\n",
|
||
"\t\tfor sid in sampleIds:\n",
|
||
"\t\t\tval = self.sample[sid][attributeId]\n",
|
||
"\t\t\tif val not in vals:\n",
|
||
"\t\t\t\tvals.append(val)\n",
|
||
"\t\t# print(vals)\n",
|
||
"\t\treturn vals\n",
|
||
"\n",
|
||
"\tdef getEntropy(self, sampleIds):\n",
|
||
"\t\tentropy = 0\n",
|
||
"\t\tlabelCount = [0] * len(self.labelCodes)\n",
|
||
"\t\tfor sid in sampleIds:\n",
|
||
"\t\t\tlabelCount[self.getLabelCodeId(sid)] += 1\n",
|
||
"\t\t# print(\"-ge\", labelCount)\n",
|
||
"\t\tfor lv in labelCount:\n",
|
||
"\t\t\t# print(lv)\n",
|
||
"\t\t\tif lv != 0:\n",
|
||
"\t\t\t\tentropy += -lv/len(sampleIds) * math.log(lv/len(sampleIds), 2)\n",
|
||
"\t\t\telse:\n",
|
||
"\t\t\t\tentropy += 0\n",
|
||
"\t\treturn entropy\n",
|
||
"\n",
|
||
"\tdef getDominantLabel(self, sampleIds):\n",
|
||
"\t\tlabelCodesCount = [0] * len(self.labelCodes)\n",
|
||
"\t\tfor sid in sampleIds:\n",
|
||
"\t\t\tlabelCodesCount[self.labelCodes.index(self.labels[sid])] += 1\n",
|
||
"\t\treturn self.labelCodes[labelCodesCount.index(max(labelCodesCount))]\n",
|
||
"\n",
|
||
"\tdef getInformationGain(self, sampleIds, attributeId):\n",
|
||
"\t\tgain = self.getEntropy(sampleIds)\n",
|
||
"\t\tattributeVals = []\n",
|
||
"\t\tattributeValsCount = []\n",
|
||
"\t\tattributeValsIds = []\n",
|
||
"\t\tfor sid in sampleIds:\n",
|
||
"\t\t\tval = self.sample[sid][attributeId]\n",
|
||
"\t\t\tif val not in attributeVals:\n",
|
||
"\t\t\t\tattributeVals.append(val)\n",
|
||
"\t\t\t\tattributeValsCount.append(0)\n",
|
||
"\t\t\t\tattributeValsIds.append([])\n",
|
||
"\t\t\tvid = attributeVals.index(val)\n",
|
||
"\t\t\tattributeValsCount[vid] += 1\n",
|
||
"\t\t\tattributeValsIds[vid].append(sid)\n",
|
||
"\t\t# print(\"-gig\", self.attributes[attributeId])\n",
|
||
"\t\tfor vc, vids in zip(attributeValsCount, attributeValsIds):\n",
|
||
"\t\t\t# print(\"-gig\", vids)\n",
|
||
"\t\t\tgain -= vc/len(sampleIds) * self.getEntropy(vids)\n",
|
||
"\t\treturn gain\n",
|
||
"\n",
|
||
"\tdef getAttributeMaxInformationGain(self, sampleIds, attributeIds):\n",
|
||
"\t\tattributesEntropy = [0] * len(attributeIds)\n",
|
||
"\t\tfor i, attId in zip(range(len(attributeIds)), attributeIds):\n",
|
||
"\t\t\tattributesEntropy[i] = self.getInformationGain(sampleIds, attId)\n",
|
||
"\t\tmaxId = attributeIds[attributesEntropy.index(max(attributesEntropy))]\n",
|
||
"\t\treturn self.attributes[maxId], maxId\n",
|
||
"\n",
|
||
"\tdef isSingleLabeled(self, sampleIds):\n",
|
||
"\t\tlabel = self.labels[sampleIds[0]]\n",
|
||
"\t\tfor sid in sampleIds:\n",
|
||
"\t\t\tif self.labels[sid] != label:\n",
|
||
"\t\t\t\treturn False\n",
|
||
"\t\treturn True\n",
|
||
"\n",
|
||
"\tdef getLabel(self, sampleId):\n",
|
||
"\t\treturn self.labels[sampleId]\n",
|
||
"\n",
|
||
"\tdef id3(self):\n",
|
||
"\t\tsampleIds = [x for x in range(len(self.sample))]\n",
|
||
"\t\tattributeIds = [x for x in range(len(self.attributes))]\n",
|
||
"\t\tself.root = self.id3Recv(sampleIds, attributeIds, self.root)\n",
|
||
"\n",
|
||
"\tdef id3Recv(self, sampleIds, attributeIds, root):\n",
|
||
"\t\troot = Node() # Initialize current root\n",
|
||
"\t\tif self.isSingleLabeled(sampleIds):\n",
|
||
"\t\t\troot.value = self.labels[sampleIds[0]]\n",
|
||
"\t\t\treturn root\n",
|
||
"\t\t# print(attributeIds)\n",
|
||
"\t\tif len(attributeIds) == 0:\n",
|
||
"\t\t\troot.value = self.getDominantLabel(sampleIds)\n",
|
||
"\t\t\treturn root\n",
|
||
"\t\tbestAttrName, bestAttrId = self.getAttributeMaxInformationGain(\n",
|
||
"\t\t\tsampleIds, attributeIds)\n",
|
||
"\t\t# print(bestAttrName)\n",
|
||
"\t\troot.value = bestAttrName\n",
|
||
"\t\troot.childs = [] # Create list of children\n",
|
||
"\t\tfor value in self.getAttributeValues(sampleIds, bestAttrId):\n",
|
||
"\t\t\t# print(value)\n",
|
||
"\t\t\tchild = Node()\n",
|
||
"\t\t\tchild.value = value\n",
|
||
"\t\t\troot.childs.append(child) # Append new child node to current\n",
|
||
"\t\t\t\t\t\t\t\t\t # root\n",
|
||
"\t\t\tchildSampleIds = []\n",
|
||
"\t\t\tfor sid in sampleIds:\n",
|
||
"\t\t\t\tif self.sample[sid][bestAttrId] == value:\n",
|
||
"\t\t\t\t\tchildSampleIds.append(sid)\n",
|
||
"\t\t\tif len(childSampleIds) == 0:\n",
|
||
"\t\t\t\tchild.next = self.getDominantLabel(sampleIds)\n",
|
||
"\t\t\telse:\n",
|
||
"\t\t\t\t# print(bestAttrName, bestAttrId)\n",
|
||
"\t\t\t\t# print(attributeIds)\n",
|
||
"\t\t\t\tif len(attributeIds) > 0 and bestAttrId in attributeIds:\n",
|
||
"\t\t\t\t\ttoRemove = attributeIds.index(bestAttrId)\n",
|
||
"\t\t\t\t\tattributeIds.pop(toRemove)\n",
|
||
"\t\t\t\tchild.next = self.id3Recv(\n",
|
||
"\t\t\t\t\tchildSampleIds, attributeIds, child.next)\n",
|
||
"\t\treturn root\n",
|
||
"\n",
|
||
"\tdef printTree(self):\n",
|
||
"\t\tif self.root:\n",
|
||
"\t\t\troots = deque()\n",
|
||
"\t\t\troots.append(self.root)\n",
|
||
"\t\t\twhile len(roots) > 0:\n",
|
||
"\t\t\t\troot = roots.popleft()\n",
|
||
"\t\t\t\tprint(root.value)\n",
|
||
"\t\t\t\tif root.childs:\n",
|
||
"\t\t\t\t\tfor child in root.childs:\n",
|
||
"\t\t\t\t\t\tprint('({})'.format(child.value))\n",
|
||
"\t\t\t\t\t\troots.append(child.next)\n",
|
||
"\t\t\t\telif root.next:\n",
|
||
"\t\t\t\t\tprint(root.next)\n",
|
||
"\n",
|
||
"\n",
|
||
"def test():\n",
|
||
"\tf = open('DataFiles/rideclass.csv')\n",
|
||
"\tattributes = f.readline().split(',')\n",
|
||
"\tattributes = attributes[1:len(attributes)-1]\n",
|
||
"\tprint(attributes)\n",
|
||
"\tsample = f.readlines()\n",
|
||
"\tf.close()\n",
|
||
"\tfor i in range(len(sample)):\n",
|
||
"\t\tsample[i] = re.sub('\\d+,', '', sample[i])\n",
|
||
"\t\tsample[i] = sample[i].strip().split(',')\n",
|
||
"\tlabels = []\n",
|
||
"\tfor s in sample:\n",
|
||
"\t\tlabels.append(s.pop())\n",
|
||
"\t# print(sample)\n",
|
||
"\t# print(labels)\n",
|
||
"\tdecisionTree = DecisionTree(sample, attributes, labels)\n",
|
||
"\tprint(\"System entropy {}\".format(decisionTree.entropy))\n",
|
||
"\tdecisionTree.id3()\n",
|
||
"\tdecisionTree.printTree()\n",
|
||
"\n",
|
||
"\n",
|
||
"if __name__ == '__main__':\n",
|
||
"\ttest()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Cancer Data again now with Decision Trees and other Methods"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import matplotlib.pyplot as plt\n",
|
||
"import numpy as np\n",
|
||
"from sklearn.model_selection import train_test_split \n",
|
||
"from sklearn.datasets import load_breast_cancer\n",
|
||
"from sklearn.svm import SVC\n",
|
||
"from sklearn.linear_model import LogisticRegression\n",
|
||
"from sklearn.tree import DecisionTreeClassifier\n",
|
||
"\n",
|
||
"# Load the data\n",
|
||
"cancer = load_breast_cancer()\n",
|
||
"\n",
|
||
"X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)\n",
|
||
"print(X_train.shape)\n",
|
||
"print(X_test.shape)\n",
|
||
"# Logistic Regression\n",
|
||
"logreg = LogisticRegression(solver='lbfgs')\n",
|
||
"logreg.fit(X_train, y_train)\n",
|
||
"print(\"Test set accuracy with Logistic Regression: {:.2f}\".format(logreg.score(X_test,y_test)))\n",
|
||
"# Support vector machine\n",
|
||
"svm = SVC(gamma='auto', C=100)\n",
|
||
"svm.fit(X_train, y_train)\n",
|
||
"print(\"Test set accuracy with SVM: {:.2f}\".format(svm.score(X_test,y_test)))\n",
|
||
"# Decision Trees\n",
|
||
"deep_tree_clf = DecisionTreeClassifier(max_depth=None)\n",
|
||
"deep_tree_clf.fit(X_train, y_train)\n",
|
||
"print(\"Test set accuracy with Decision Trees: {:.2f}\".format(deep_tree_clf.score(X_test,y_test)))\n",
|
||
"#now scale the data\n",
|
||
"from sklearn.preprocessing import StandardScaler\n",
|
||
"scaler = StandardScaler()\n",
|
||
"scaler.fit(X_train)\n",
|
||
"X_train_scaled = scaler.transform(X_train)\n",
|
||
"X_test_scaled = scaler.transform(X_test)\n",
|
||
"# Logistic Regression\n",
|
||
"logreg.fit(X_train_scaled, y_train)\n",
|
||
"print(\"Test set accuracy Logistic Regression with scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))\n",
|
||
"# Support Vector Machine\n",
|
||
"svm.fit(X_train_scaled, y_train)\n",
|
||
"print(\"Test set accuracy SVM with scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))\n",
|
||
"# Decision Trees\n",
|
||
"deep_tree_clf.fit(X_train_scaled, y_train)\n",
|
||
"print(\"Test set accuracy with Decision Trees and scaled data: {:.2f}\".format(deep_tree_clf.score(X_test_scaled,y_test)))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Another example, the moons again"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from __future__ import division, print_function, unicode_literals\n",
|
||
"\n",
|
||
"# Common imports\n",
|
||
"import numpy as np\n",
|
||
"import os\n",
|
||
"\n",
|
||
"# to make this notebook's output stable across runs\n",
|
||
"np.random.seed(42)\n",
|
||
"\n",
|
||
"# To plot pretty figures\n",
|
||
"import matplotlib\n",
|
||
"import matplotlib.pyplot as plt\n",
|
||
"from matplotlib.colors import ListedColormap\n",
|
||
"plt.rcParams['axes.labelsize'] = 14\n",
|
||
"plt.rcParams['xtick.labelsize'] = 12\n",
|
||
"plt.rcParams['ytick.labelsize'] = 12\n",
|
||
"\n",
|
||
"\n",
|
||
"from sklearn.svm import SVC\n",
|
||
"from sklearn import datasets\n",
|
||
"from sklearn.tree import DecisionTreeClassifier\n",
|
||
"from sklearn.datasets import make_moons\n",
|
||
"from sklearn.tree import export_graphviz\n",
|
||
"\n",
|
||
"Xm, ym = make_moons(n_samples=100, noise=0.25, random_state=53)\n",
|
||
"\n",
|
||
"deep_tree_clf1 = DecisionTreeClassifier(random_state=42)\n",
|
||
"deep_tree_clf2 = DecisionTreeClassifier(min_samples_leaf=4, random_state=42)\n",
|
||
"deep_tree_clf1.fit(Xm, ym)\n",
|
||
"deep_tree_clf2.fit(Xm, ym)\n",
|
||
"\n",
|
||
"\n",
|
||
"def plot_decision_boundary(clf, X, y, axes=[0, 7.5, 0, 3], iris=True, legend=False, plot_training=True):\n",
|
||
" x1s = np.linspace(axes[0], axes[1], 100)\n",
|
||
" x2s = np.linspace(axes[2], axes[3], 100)\n",
|
||
" x1, x2 = np.meshgrid(x1s, x2s)\n",
|
||
" X_new = np.c_[x1.ravel(), x2.ravel()]\n",
|
||
" y_pred = clf.predict(X_new).reshape(x1.shape)\n",
|
||
" custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0'])\n",
|
||
" plt.contourf(x1, x2, y_pred, alpha=0.3, cmap=custom_cmap)\n",
|
||
" if not iris:\n",
|
||
" custom_cmap2 = ListedColormap(['#7d7d58','#4c4c7f','#507d50'])\n",
|
||
" plt.contour(x1, x2, y_pred, cmap=custom_cmap2, alpha=0.8)\n",
|
||
" if plot_training:\n",
|
||
" plt.plot(X[:, 0][y==0], X[:, 1][y==0], \"yo\", label=\"Iris-Setosa\")\n",
|
||
" plt.plot(X[:, 0][y==1], X[:, 1][y==1], \"bs\", label=\"Iris-Versicolor\")\n",
|
||
" plt.plot(X[:, 0][y==2], X[:, 1][y==2], \"g^\", label=\"Iris-Virginica\")\n",
|
||
" plt.axis(axes)\n",
|
||
" if iris:\n",
|
||
" plt.xlabel(\"Petal length\", fontsize=14)\n",
|
||
" plt.ylabel(\"Petal width\", fontsize=14)\n",
|
||
" else:\n",
|
||
" plt.xlabel(r\"$x_1$\", fontsize=18)\n",
|
||
" plt.ylabel(r\"$x_2$\", fontsize=18, rotation=0)\n",
|
||
" if legend:\n",
|
||
" plt.legend(loc=\"lower right\", fontsize=14)\n",
|
||
"plt.figure(figsize=(11, 4))\n",
|
||
"plt.subplot(121)\n",
|
||
"plot_decision_boundary(deep_tree_clf1, Xm, ym, axes=[-1.5, 2.5, -1, 1.5], iris=False)\n",
|
||
"plt.title(\"No restrictions\", fontsize=16)\n",
|
||
"plt.subplot(122)\n",
|
||
"plot_decision_boundary(deep_tree_clf2, Xm, ym, axes=[-1.5, 2.5, -1, 1.5], iris=False)\n",
|
||
"plt.title(\"min_samples_leaf = {}\".format(deep_tree_clf2.min_samples_leaf), fontsize=14)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Playing around with regions"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"np.random.seed(6)\n",
|
||
"Xs = np.random.rand(100, 2) - 0.5\n",
|
||
"ys = (Xs[:, 0] > 0).astype(np.float32) * 2\n",
|
||
"\n",
|
||
"angle = np.pi/4\n",
|
||
"rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])\n",
|
||
"Xsr = Xs.dot(rotation_matrix)\n",
|
||
"\n",
|
||
"tree_clf_s = DecisionTreeClassifier(random_state=42)\n",
|
||
"tree_clf_s.fit(Xs, ys)\n",
|
||
"tree_clf_sr = DecisionTreeClassifier(random_state=42)\n",
|
||
"tree_clf_sr.fit(Xsr, ys)\n",
|
||
"\n",
|
||
"plt.figure(figsize=(11, 4))\n",
|
||
"plt.subplot(121)\n",
|
||
"plot_decision_boundary(tree_clf_s, Xs, ys, axes=[-0.7, 0.7, -0.7, 0.7], iris=False)\n",
|
||
"plt.subplot(122)\n",
|
||
"plot_decision_boundary(tree_clf_sr, Xsr, ys, axes=[-0.7, 0.7, -0.7, 0.7], iris=False)\n",
|
||
"\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Regression trees"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Quadratic training set + noise\n",
|
||
"np.random.seed(42)\n",
|
||
"m = 200\n",
|
||
"X = np.random.rand(m, 1)\n",
|
||
"y = 4 * (X - 0.5) ** 2\n",
|
||
"y = y + np.random.randn(m, 1) / 10"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.tree import DecisionTreeRegressor\n",
|
||
"\n",
|
||
"tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42)\n",
|
||
"tree_reg.fit(X, y)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Final regressor code"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.tree import DecisionTreeRegressor\n",
|
||
"\n",
|
||
"tree_reg1 = DecisionTreeRegressor(random_state=42, max_depth=2)\n",
|
||
"tree_reg2 = DecisionTreeRegressor(random_state=42, max_depth=3)\n",
|
||
"tree_reg1.fit(X, y)\n",
|
||
"tree_reg2.fit(X, y)\n",
|
||
"\n",
|
||
"def plot_regression_predictions(tree_reg, X, y, axes=[0, 1, -0.2, 1], ylabel=\"$y$\"):\n",
|
||
" x1 = np.linspace(axes[0], axes[1], 500).reshape(-1, 1)\n",
|
||
" y_pred = tree_reg.predict(x1)\n",
|
||
" plt.axis(axes)\n",
|
||
" plt.xlabel(\"$x_1$\", fontsize=18)\n",
|
||
" if ylabel:\n",
|
||
" plt.ylabel(ylabel, fontsize=18, rotation=0)\n",
|
||
" plt.plot(X, y, \"b.\")\n",
|
||
" plt.plot(x1, y_pred, \"r.-\", linewidth=2, label=r\"$\\hat{y}$\")\n",
|
||
"\n",
|
||
"plt.figure(figsize=(11, 4))\n",
|
||
"plt.subplot(121)\n",
|
||
"plot_regression_predictions(tree_reg1, X, y)\n",
|
||
"for split, style in ((0.1973, \"k-\"), (0.0917, \"k--\"), (0.7718, \"k--\")):\n",
|
||
" plt.plot([split, split], [-0.2, 1], style, linewidth=2)\n",
|
||
"plt.text(0.21, 0.65, \"Depth=0\", fontsize=15)\n",
|
||
"plt.text(0.01, 0.2, \"Depth=1\", fontsize=13)\n",
|
||
"plt.text(0.65, 0.8, \"Depth=1\", fontsize=13)\n",
|
||
"plt.legend(loc=\"upper center\", fontsize=18)\n",
|
||
"plt.title(\"max_depth=2\", fontsize=14)\n",
|
||
"\n",
|
||
"plt.subplot(122)\n",
|
||
"plot_regression_predictions(tree_reg2, X, y, ylabel=None)\n",
|
||
"for split, style in ((0.1973, \"k-\"), (0.0917, \"k--\"), (0.7718, \"k--\")):\n",
|
||
" plt.plot([split, split], [-0.2, 1], style, linewidth=2)\n",
|
||
"for split in (0.0458, 0.1298, 0.2873, 0.9040):\n",
|
||
" plt.plot([split, split], [-0.2, 1], \"k:\", linewidth=1)\n",
|
||
"plt.text(0.3, 0.5, \"Depth=2\", fontsize=13)\n",
|
||
"plt.title(\"max_depth=3\", fontsize=14)\n",
|
||
"\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"tree_reg1 = DecisionTreeRegressor(random_state=42)\n",
|
||
"tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)\n",
|
||
"tree_reg1.fit(X, y)\n",
|
||
"tree_reg2.fit(X, y)\n",
|
||
"\n",
|
||
"x1 = np.linspace(0, 1, 500).reshape(-1, 1)\n",
|
||
"y_pred1 = tree_reg1.predict(x1)\n",
|
||
"y_pred2 = tree_reg2.predict(x1)\n",
|
||
"\n",
|
||
"plt.figure(figsize=(11, 4))\n",
|
||
"\n",
|
||
"plt.subplot(121)\n",
|
||
"plt.plot(X, y, \"b.\")\n",
|
||
"plt.plot(x1, y_pred1, \"r.-\", linewidth=2, label=r\"$\\hat{y}$\")\n",
|
||
"plt.axis([0, 1, -0.2, 1.1])\n",
|
||
"plt.xlabel(\"$x_1$\", fontsize=18)\n",
|
||
"plt.ylabel(\"$y$\", fontsize=18, rotation=0)\n",
|
||
"plt.legend(loc=\"upper center\", fontsize=18)\n",
|
||
"plt.title(\"No restrictions\", fontsize=14)\n",
|
||
"\n",
|
||
"plt.subplot(122)\n",
|
||
"plt.plot(X, y, \"b.\")\n",
|
||
"plt.plot(x1, y_pred2, \"r.-\", linewidth=2, label=r\"$\\hat{y}$\")\n",
|
||
"plt.axis([0, 1, -0.2, 1.1])\n",
|
||
"plt.xlabel(\"$x_1$\", fontsize=18)\n",
|
||
"plt.title(\"min_samples_leaf={}\".format(tree_reg2.min_samples_leaf), fontsize=14)\n",
|
||
"\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Pros and cons of trees, pros\n",
|
||
"\n",
|
||
"* White box, easy to interpret model. Some people believe that decision trees more closely mirror human decision-making than do the regression and classification approaches discussed earlier (think of support vector machines)\n",
|
||
"\n",
|
||
"* Trees are very easy to explain to people. In fact, they are even easier to explain than linear regression!\n",
|
||
"\n",
|
||
"* No feature normalization needed\n",
|
||
"\n",
|
||
"* Tree models can handle both continuous and categorical data (Classification and Regression Trees)\n",
|
||
"\n",
|
||
"* Can model nonlinear relationships\n",
|
||
"\n",
|
||
"* Can model interactions between the different descriptive features\n",
|
||
"\n",
|
||
"* Trees can be displayed graphically, and are easily interpreted even by a non-expert (especially if they are small)\n",
|
||
"\n",
|
||
"## Disadvantages\n",
|
||
"\n",
|
||
"* Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches\n",
|
||
"\n",
|
||
"* If continuous features are used the tree may become quite large and hence less interpretable\n",
|
||
"\n",
|
||
"* Decision trees are prone to overfit the training data and hence do not well generalize the data if no stopping criteria or improvements like pruning, boosting or bagging are implemented\n",
|
||
"\n",
|
||
"* Small changes in the data may lead to a completely different tree. This issue can be addressed by using ensemble methods like bagging, boosting or random forests\n",
|
||
"\n",
|
||
"* Unbalanced datasets where some target feature values occur much more frequently than others may lead to biased trees since the frequently occurring feature values are preferred over the less frequently occurring ones. \n",
|
||
"\n",
|
||
"* If the number of features is relatively large (high dimensional) and the number of instances is relatively low, the tree might overfit the data\n",
|
||
"\n",
|
||
"* Features with many levels may be preferred over features with less levels since for them it is *more easy* to split the dataset such that the sub datasets only contain pure target feature values. This issue can be addressed by preferring for instance the information gain ratio as splitting criteria over information gain\n",
|
||
"\n",
|
||
"However, by aggregating many decision trees, using methods like\n",
|
||
"bagging, random forests, and boosting, the predictive performance of\n",
|
||
"trees can be substantially improved.\n",
|
||
"\n",
|
||
"\n",
|
||
"## Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods\n",
|
||
"\n",
|
||
"As stated above and seen in many of the examples discussed here about\n",
|
||
"a single decision tree, we often end up overfitting our training\n",
|
||
"data. This normally means that we have a high variance. Can we reduce\n",
|
||
"the variance of a statistical learning method?\n",
|
||
"\n",
|
||
"This leads us to a set of different methods that can combine different\n",
|
||
"machine learning algorithms or just use one of them to construct\n",
|
||
"forests and jungles of trees, homogeneous ones or heterogenous\n",
|
||
"ones. These methods are recognized by different names which we will\n",
|
||
"try to explain here. These are\n",
|
||
"\n",
|
||
"1. Voting classifiers\n",
|
||
"\n",
|
||
"2. Bagging and Pasting\n",
|
||
"\n",
|
||
"3. Random forests\n",
|
||
"\n",
|
||
"4. Boosting methods, from adaptive to Extreme Gradient Boosting (XGBoost)\n",
|
||
"\n",
|
||
"We discuss these methods here.\n",
|
||
"\n",
|
||
"\n",
|
||
"## An Overview of Ensemble Methods\n",
|
||
"\n",
|
||
"<!-- dom:FIGURE: [DataFiles/ensembleoverview.png, width=600 frac=0.8] -->\n",
|
||
"<!-- begin figure -->\n",
|
||
"\n",
|
||
"<p></p>\n",
|
||
"<img src=\"DataFiles/ensembleoverview.png\" width=600>\n",
|
||
"\n",
|
||
"<!-- end figure -->\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"## Bagging\n",
|
||
"\n",
|
||
"The **plain** decision trees suffer from high\n",
|
||
"variance. This means that if we split the training data into two parts\n",
|
||
"at random, and fit a decision tree to both halves, the results that we\n",
|
||
"get could be quite different. In contrast, a procedure with low\n",
|
||
"variance will yield similar results if applied repeatedly to distinct\n",
|
||
"data sets; linear regression tends to have low variance, if the ratio\n",
|
||
"of $n$ to $p$ is moderately large. \n",
|
||
"\n",
|
||
"**Bootstrap aggregation**, or just **bagging**, is a\n",
|
||
"general-purpose procedure for reducing the variance of a statistical\n",
|
||
"learning method. \n",
|
||
"\n",
|
||
"\n",
|
||
"## More bagging\n",
|
||
"\n",
|
||
"Bagging typically results in improved accuracy\n",
|
||
"over prediction using a single tree. Unfortunately, however, it can be\n",
|
||
"difficult to interpret the resulting model. Recall that one of the\n",
|
||
"advantages of decision trees is the attractive and easily interpreted\n",
|
||
"diagram that results.\n",
|
||
"\n",
|
||
"However, when we bag a large number of trees, it is no longer\n",
|
||
"possible to represent the resulting statistical learning procedure\n",
|
||
"using a single tree, and it is no longer clear which variables are\n",
|
||
"most important to the procedure. Thus, bagging improves prediction\n",
|
||
"accuracy at the expense of interpretability. Although the collection\n",
|
||
"of bagged trees is much more difficult to interpret than a single\n",
|
||
"tree, one can obtain an overall summary of the importance of each\n",
|
||
"predictor using the MSE (for bagging regression trees) or the Gini\n",
|
||
"index (for bagging classification trees). In the case of bagging\n",
|
||
"regression trees, we can record the total amount that the MSE is\n",
|
||
"decreased due to splits over a given predictor, averaged over all $B$ possible\n",
|
||
"trees. A large value indicates an important predictor. Similarly, in\n",
|
||
"the context of bagging classification trees, we can add up the total\n",
|
||
"amount that the Gini index is decreased by splits over a given\n",
|
||
"predictor, averaged over all $B$ trees.\n",
|
||
"\n",
|
||
"## Simple Voting Example, head or tail"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"heads_proba = 0.51\n",
|
||
"coin_tosses = (np.random.rand(10000, 10) < heads_proba).astype(np.int32)\n",
|
||
"cumulative_heads_ratio = np.cumsum(coin_tosses, axis=0) / np.arange(1, 10001).reshape(-1, 1)\n",
|
||
"plt.figure(figsize=(8,3.5))\n",
|
||
"plt.plot(cumulative_heads_ratio)\n",
|
||
"plt.plot([0, 10000], [0.51, 0.51], \"k--\", linewidth=2, label=\"51%\")\n",
|
||
"plt.plot([0, 10000], [0.5, 0.5], \"k-\", label=\"50%\")\n",
|
||
"plt.xlabel(\"Number of coin tosses\")\n",
|
||
"plt.ylabel(\"Heads ratio\")\n",
|
||
"plt.legend(loc=\"lower right\")\n",
|
||
"plt.axis([0, 10000, 0.42, 0.58])\n",
|
||
"save_fig(\"votingsimple\")\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Using the Voting Classifier"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.model_selection import train_test_split\n",
|
||
"from sklearn.datasets import make_moons\n",
|
||
"\n",
|
||
"X, y = make_moons(n_samples=500, noise=0.30, random_state=42)\n",
|
||
"X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)\n",
|
||
"\n",
|
||
"from sklearn.ensemble import RandomForestClassifier\n",
|
||
"from sklearn.ensemble import VotingClassifier\n",
|
||
"from sklearn.linear_model import LogisticRegression\n",
|
||
"from sklearn.svm import SVC\n",
|
||
"\n",
|
||
"log_clf = LogisticRegression(solver=\"liblinear\", random_state=42)\n",
|
||
"rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)\n",
|
||
"svm_clf = SVC(gamma=\"auto\", random_state=42)\n",
|
||
"\n",
|
||
"voting_clf = VotingClassifier(\n",
|
||
" estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],\n",
|
||
" voting='hard')\n",
|
||
"\n",
|
||
"voting_clf.fit(X_train, y_train)\n",
|
||
"\n",
|
||
"from sklearn.metrics import accuracy_score\n",
|
||
"\n",
|
||
"for clf in (log_clf, rnd_clf, svm_clf, voting_clf):\n",
|
||
" clf.fit(X_train, y_train)\n",
|
||
" y_pred = clf.predict(X_test)\n",
|
||
" print(clf.__class__.__name__, accuracy_score(y_test, y_pred))\n",
|
||
"\n",
|
||
"log_clf = LogisticRegression(solver=\"liblinear\", random_state=42)\n",
|
||
"rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)\n",
|
||
"svm_clf = SVC(gamma=\"auto\", probability=True, random_state=42)\n",
|
||
"\n",
|
||
"voting_clf = VotingClassifier(\n",
|
||
" estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],\n",
|
||
" voting='soft')\n",
|
||
"voting_clf.fit(X_train, y_train)\n",
|
||
"\n",
|
||
"from sklearn.metrics import accuracy_score\n",
|
||
"\n",
|
||
"for clf in (log_clf, rnd_clf, svm_clf, voting_clf):\n",
|
||
" clf.fit(X_train, y_train)\n",
|
||
" y_pred = clf.predict(X_test)\n",
|
||
" print(clf.__class__.__name__, accuracy_score(y_test, y_pred))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Please, not the moons again! Voting and Bagging"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.model_selection import train_test_split\n",
|
||
"from sklearn.datasets import make_moons\n",
|
||
"\n",
|
||
"X, y = make_moons(n_samples=500, noise=0.30, random_state=42)\n",
|
||
"X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)\n",
|
||
"from sklearn.ensemble import RandomForestClassifier\n",
|
||
"from sklearn.ensemble import VotingClassifier\n",
|
||
"from sklearn.linear_model import LogisticRegression\n",
|
||
"from sklearn.svm import SVC\n",
|
||
"\n",
|
||
"log_clf = LogisticRegression(random_state=42)\n",
|
||
"rnd_clf = RandomForestClassifier(random_state=42)\n",
|
||
"svm_clf = SVC(random_state=42)\n",
|
||
"\n",
|
||
"voting_clf = VotingClassifier(\n",
|
||
" estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],\n",
|
||
" voting='hard')\n",
|
||
"voting_clf.fit(X_train, y_train)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 17,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.metrics import accuracy_score\n",
|
||
"\n",
|
||
"for clf in (log_clf, rnd_clf, svm_clf, voting_clf):\n",
|
||
" clf.fit(X_train, y_train)\n",
|
||
" y_pred = clf.predict(X_test)\n",
|
||
" print(clf.__class__.__name__, accuracy_score(y_test, y_pred))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 18,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"log_clf = LogisticRegression(random_state=42)\n",
|
||
"rnd_clf = RandomForestClassifier(random_state=42)\n",
|
||
"svm_clf = SVC(probability=True, random_state=42)\n",
|
||
"\n",
|
||
"voting_clf = VotingClassifier(\n",
|
||
" estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],\n",
|
||
" voting='soft')\n",
|
||
"voting_clf.fit(X_train, y_train)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 19,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.metrics import accuracy_score\n",
|
||
"\n",
|
||
"for clf in (log_clf, rnd_clf, svm_clf, voting_clf):\n",
|
||
" clf.fit(X_train, y_train)\n",
|
||
" y_pred = clf.predict(X_test)\n",
|
||
" print(clf.__class__.__name__, accuracy_score(y_test, y_pred))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Bagging Examples"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 20,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.ensemble import BaggingClassifier\n",
|
||
"from sklearn.tree import DecisionTreeClassifier\n",
|
||
"\n",
|
||
"bag_clf = BaggingClassifier(\n",
|
||
" DecisionTreeClassifier(random_state=42), n_estimators=500,\n",
|
||
" max_samples=100, bootstrap=True, n_jobs=-1, random_state=42)\n",
|
||
"bag_clf.fit(X_train, y_train)\n",
|
||
"y_pred = bag_clf.predict(X_test)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 21,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.metrics import accuracy_score\n",
|
||
"print(accuracy_score(y_test, y_pred))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 22,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"tree_clf = DecisionTreeClassifier(random_state=42)\n",
|
||
"tree_clf.fit(X_train, y_train)\n",
|
||
"y_pred_tree = tree_clf.predict(X_test)\n",
|
||
"print(accuracy_score(y_test, y_pred_tree))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 23,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from matplotlib.colors import ListedColormap\n",
|
||
"\n",
|
||
"def plot_decision_boundary(clf, X, y, axes=[-1.5, 2.5, -1, 1.5], alpha=0.5, contour=True):\n",
|
||
" x1s = np.linspace(axes[0], axes[1], 100)\n",
|
||
" x2s = np.linspace(axes[2], axes[3], 100)\n",
|
||
" x1, x2 = np.meshgrid(x1s, x2s)\n",
|
||
" X_new = np.c_[x1.ravel(), x2.ravel()]\n",
|
||
" y_pred = clf.predict(X_new).reshape(x1.shape)\n",
|
||
" custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0'])\n",
|
||
" plt.contourf(x1, x2, y_pred, alpha=0.3, cmap=custom_cmap)\n",
|
||
" if contour:\n",
|
||
" custom_cmap2 = ListedColormap(['#7d7d58','#4c4c7f','#507d50'])\n",
|
||
" plt.contour(x1, x2, y_pred, cmap=custom_cmap2, alpha=0.8)\n",
|
||
" plt.plot(X[:, 0][y==0], X[:, 1][y==0], \"yo\", alpha=alpha)\n",
|
||
" plt.plot(X[:, 0][y==1], X[:, 1][y==1], \"bs\", alpha=alpha)\n",
|
||
" plt.axis(axes)\n",
|
||
" plt.xlabel(r\"$x_1$\", fontsize=18)\n",
|
||
" plt.ylabel(r\"$x_2$\", fontsize=18, rotation=0)\n",
|
||
"plt.figure(figsize=(11,4))\n",
|
||
"plt.subplot(121)\n",
|
||
"plot_decision_boundary(tree_clf, X, y)\n",
|
||
"plt.title(\"Decision Tree\", fontsize=14)\n",
|
||
"plt.subplot(122)\n",
|
||
"plot_decision_boundary(bag_clf, X, y)\n",
|
||
"plt.title(\"Decision Trees with Bagging\", fontsize=14)\n",
|
||
"save_fig(\"baggingtree\")\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Making your own Bootstrap: Changing the Level of the Decision Tree\n",
|
||
"\n",
|
||
"Let us bring up our good old boostrap example from the linear regression lectures. We change the linerar regression algorithm with\n",
|
||
"a decision tree wth different depths and perform a bootstrap aggregate (in this case we perform as many bootstraps as data points $n$)."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 24,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"\n",
|
||
"import matplotlib.pyplot as plt\n",
|
||
"import numpy as np\n",
|
||
"from sklearn.model_selection import train_test_split\n",
|
||
"from sklearn.pipeline import make_pipeline\n",
|
||
"from sklearn.utils import resample\n",
|
||
"from sklearn.tree import DecisionTreeRegressor\n",
|
||
"\n",
|
||
"n = 100\n",
|
||
"n_boostraps = 100\n",
|
||
"maxdepth = 8\n",
|
||
"\n",
|
||
"# Make data set.\n",
|
||
"x = np.linspace(-3, 3, n).reshape(-1, 1)\n",
|
||
"y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)\n",
|
||
"error = np.zeros(maxdepth)\n",
|
||
"bias = np.zeros(maxdepth)\n",
|
||
"variance = np.zeros(maxdepth)\n",
|
||
"polydegree = np.zeros(maxdepth)\n",
|
||
"X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n",
|
||
"\n",
|
||
"from sklearn.preprocessing import StandardScaler\n",
|
||
"scaler = StandardScaler()\n",
|
||
"scaler.fit(X_train)\n",
|
||
"X_train_scaled = scaler.transform(X_train)\n",
|
||
"X_test_scaled = scaler.transform(X_test)\n",
|
||
"\n",
|
||
"# we produce a simple tree first as benchmark\n",
|
||
"simpletree = DecisionTreeRegressor(max_depth=3) \n",
|
||
"simpletree.fit(X_train_scaled, y_train)\n",
|
||
"simpleprediction = simpletree.predict(X_test_scaled)\n",
|
||
"for degree in range(1,maxdepth):\n",
|
||
" model = DecisionTreeRegressor(max_depth=degree) \n",
|
||
" y_pred = np.empty((y_test.shape[0], n_boostraps))\n",
|
||
" for i in range(n_boostraps):\n",
|
||
" x_, y_ = resample(X_train_scaled, y_train)\n",
|
||
" model.fit(x_, y_)\n",
|
||
" y_pred[:, i] = model.predict(X_test_scaled)#.ravel()\n",
|
||
"\n",
|
||
" polydegree[degree] = degree\n",
|
||
" error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )\n",
|
||
" bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )\n",
|
||
" variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )\n",
|
||
" print('Polynomial degree:', degree)\n",
|
||
" print('Error:', error[degree])\n",
|
||
" print('Bias^2:', bias[degree])\n",
|
||
" print('Var:', variance[degree])\n",
|
||
" print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))\n",
|
||
"\n",
|
||
"mse_simpletree = np.mean( np.mean((y_test - simpleprediction)**2)\n",
|
||
"plt.xlim(1,maxdepth)\n",
|
||
"plt.plot(polydegree, error, label='MSE simple tree')\n",
|
||
"plt.plot(polydegree, mse_simpletree, label='MSE for Bootstrap')\n",
|
||
"plt.plot(polydegree, bias, label='bias')\n",
|
||
"plt.plot(polydegree, variance, label='Variance')\n",
|
||
"plt.legend()\n",
|
||
"save_fig(\"baggingboot\")\n",
|
||
"plt.show()"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|