diff --git a/doc/pub/week47/html/._week47-bs000.html b/doc/pub/week47/html/._week47-bs000.html index 8424394b5..7f570d170 100644 --- a/doc/pub/week47/html/._week47-bs000.html +++ b/doc/pub/week47/html/._week47-bs000.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -221,7 +380,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs001.html b/doc/pub/week47/html/._week47-bs001.html index 7e191efec..c9186a9a7 100644 --- a/doc/pub/week47/html/._week47-bs001.html +++ b/doc/pub/week47/html/._week47-bs001.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -196,17 +355,19 @@ MathJax.Hub.Config({
    1. Basics of decision trees, classification and regression algorithms and ensemble models
    2. -
    3. Readings and Videos:
    4. - +
    5. Video on Decision trees https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn
    6. +
    7. Video on boosting methods https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai
    8. +
    9. Video on AdaBoost https://www.youtube.com/watch?v=LsK-xG1cLYA
    10. +
    11. Video on Gradient boost, part 1, parts 2-4 follow thereafter https://www.youtube.com/watch?v=3CC4N4z3GJc
    12. +
    13. Decision Trees: Rashcka et al chapter 3 pages 86-98, and chapter 7 on Ensemble methods, Voting and Bagging and Gradient Boosting. See also lecture from STK-IN4300, lecture 7 at https://www.uio.no/studier/emner/matnat/math/STK-IN4300/h20/slides/lecture_7.pdf.
    14. +
    +
  • Decision Trees: Geron's chapter 6 covers decision trees while ensemble models, voting and bagging are discussed in chapter 7. See also lecture from STK-IN4300, lecture 7. Chapter 9.2 of Hastie et al contains also a good discussion.
  • @@ -228,7 +389,7 @@ MathJax.Hub.Config({
  • 10
  • 11
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs002.html b/doc/pub/week47/html/._week47-bs002.html index 21fb9e482..9e879337f 100644 --- a/doc/pub/week47/html/._week47-bs002.html +++ b/doc/pub/week47/html/._week47-bs002.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,45 +337,27 @@ MathJax.Hub.Config({

     

     

     

    -

    Random forests

    +

    Building a tree, regression

    -

    Random forests provide an improvement over bagged trees by way of a -small tweak that decorrelates the trees. -

    - -

    As in bagging, we build a -number of decision trees on bootstrapped training samples. But when -building these decision trees, each time a split in a tree is -considered, a random sample of \( m \) predictors is chosen as split -candidates from the full set of \( p \) predictors. The split is allowed to -use only one of those \( m \) predictors. -

    - -

    A fresh sample of \( m \) predictors is -taken at each split, and typically we choose +

    There are mainly two steps

    +
      +
    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 \).
    2. +
    3. 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 \).
    4. +
    +

    How do we construct the regions \( R_1,\dots,R_J \)? In theory, the +regions could have any shape. However, we choose to divide the +predictor space into high-dimensional rectangles, or boxes, for +simplicity and for ease of interpretation of the resulting predictive +model. The goal is to find boxes \( R_1,\dots,R_J \) that minimize the +MSE, given by

    $$ -m\approx \sqrt{p}. +\sum_{j=1}^J\sum_{i\in R_j}(y_i-\overline{y}_{R_j})^2, $$ -

    In building a random forest, at -each split in the tree, the algorithm is not even allowed to consider -a majority of the available predictors. -

    - -

    The reason for this is rather clever. Suppose that there is one very -strong predictor in the data set, along with a number of other -moderately strong predictors. Then in the collection of bagged -variable importance random forest trees, most or all of the trees will -use this strong predictor in the top split. Consequently, all of the -bagged trees will look quite similar to each other. Hence the -predictions from the bagged trees will be highly correlated. -Unfortunately, averaging many highly correlated quantities does not -lead to as large of a reduction in variance as averaging many -uncorrelated quantities. In particular, this means that bagging will -not lead to a substantial reduction in variance over a single tree in -this setting. +

    where \( \overline{y}_{R_j} \) is the mean response for the training observations +within box \( j \).

    @@ -236,7 +377,7 @@ this setting.

  • 11
  • 12
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs003.html b/doc/pub/week47/html/._week47-bs003.html index 4f8bdd8b0..7e3742d21 100644 --- a/doc/pub/week47/html/._week47-bs003.html +++ b/doc/pub/week47/html/._week47-bs003.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,23 +337,22 @@ MathJax.Hub.Config({

     

     

     

    -

    Random Forest Algorithm

    -

    The algorithm described here can be applied to both classification and regression problems.

    +

    A top-down approach, recursive binary splitting

    + +

    Unfortunately, it is computationally infeasible to consider every +possible partition of the feature space into \( J \) boxes. The common +strategy is to take a top-down approach +

    + +

    The approach is top-down because it begins at the top of the tree (all +observations belong to a single region) and then successively splits +the predictor space; each split is indicated via two new branches +further down on the tree. It is greedy because at each step of the +tree-building process, the best split is made at that particular step, +rather than looking ahead and picking a split that will lead to a +better tree in some future step. +

    -

    We will grow of forest of say \( B \) trees.

    -
      -
    1. For \( b=1:B \)
    2. - -
    3. Output then the ensemble of trees \( \{T_b\}_1^{B} \) and make predictions for either a regression type of problem or a classification type of problem.
    4. -

    diff --git a/doc/pub/week47/html/._week47-bs004.html b/doc/pub/week47/html/._week47-bs004.html index 0cdcd791e..c0de946b2 100644 --- a/doc/pub/week47/html/._week47-bs004.html +++ b/doc/pub/week47/html/._week47-bs004.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,98 +337,52 @@ MathJax.Hub.Config({

     

     

     

    -

    Random Forests Compared with other Methods on the Cancer Data

    +

    Making a tree

    - -
    -
    -
    -
    -
    -
    import matplotlib.pyplot as plt
    -import numpy as np
    -from sklearn.model_selection import  train_test_split 
    -from sklearn.datasets import load_breast_cancer
    -from sklearn.svm import SVC
    -from sklearn.linear_model import LogisticRegression
    -from sklearn.tree import DecisionTreeClassifier
    -from sklearn.ensemble import BaggingClassifier
    +

    In order to implement the recursive binary splitting we start by selecting +the predictor \( x_j \) and a cutpoint \( s \) that splits the predictor space into two regions \( R_1 \) and \( R_2 \) +

    +$$ +\left\{X\vert x_j < s\right\}, +$$ -# Load the data -cancer = load_breast_cancer() +

    and

    +$$ +\left\{X\vert x_j \geq s\right\}, +$$ -X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) -#define methods -# Logistic Regression -logreg = LogisticRegression(solver='lbfgs') -# Support vector machine -svm = SVC(gamma='auto', C=100) -# Decision Trees -deep_tree_clf = DecisionTreeClassifier(max_depth=None) -#Scale the data -from sklearn.preprocessing import StandardScaler -scaler = StandardScaler() -scaler.fit(X_train) -X_train_scaled = scaler.transform(X_train) -X_test_scaled = scaler.transform(X_test) -# Logistic Regression -logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) -# Support Vector Machine -svm.fit(X_train_scaled, y_train) -print("Test set accuracy SVM with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) -# Decision Trees -deep_tree_clf.fit(X_train_scaled, y_train) -print("Test set accuracy with Decision Trees and scaled data: {:.2f}".format(deep_tree_clf.score(X_test_scaled,y_test))) +

    so that we obtain the lowest MSE, that is

    +$$ +\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, +$$ - -from sklearn.ensemble import RandomForestClassifier -from sklearn.preprocessing import LabelEncoder -from sklearn.model_selection import cross_validate -# Data set not specificied -#Instantiate the model with 500 trees and entropy as splitting criteria -Random_Forest_model = RandomForestClassifier(n_estimators=500,criterion="entropy") -Random_Forest_model.fit(X_train_scaled, y_train) -#Cross validation -accuracy = cross_validate(Random_Forest_model,X_test_scaled,y_test,cv=10)['test_score'] -print(accuracy) -print("Test set accuracy with Random Forests and scaled data: {:.2f}".format(Random_Forest_model.score(X_test_scaled,y_test))) - - -import scikitplot as skplt -y_pred = Random_Forest_model.predict(X_test_scaled) -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) -plt.show() -y_probas = Random_Forest_model.predict_proba(X_test_scaled) -skplt.metrics.plot_roc(y_test, y_probas) -plt.show() -skplt.metrics.plot_cumulative_gain(y_test, y_probas) -plt.show() -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Recall that the cumulative gains curve shows the percentage of the -overall number of cases in a given category gained by targeting a -percentage of the total number of cases. +

    which we want to minimize by considering all predictors +\( x_1,x_2,\dots,x_p \). We consider also all possible values of \( s \) for +each predictor. These values could be determined by randomly assigned +numbers or by starting at the midpoint and then proceed till we find +an optimal value.

    -

    Similarly, the receiver operating characteristic curve, or ROC curve, -displays the diagnostic ability of a binary classifier system as its -discrimination threshold is varied. It plots the true positive rate against the false positive rate. +

    For any \( j \) and \( s \), we define the pair of half-planes where +\( \overline{y}_{R_1} \) is the mean response for the training +observations in \( R_1(j,s) \), and \( \overline{y}_{R_2} \) is the mean +response for the training observations in \( R_2(j,s) \). +

    + +

    Finding the values of \( j \) and \( s \) that minimize the above equation can be +done quite quickly, especially when the number of features \( p \) is not +too large. +

    + +

    Next, we repeat the process, looking +for the best predictor and best cutpoint in order to split the data +further so as to minimize the MSE within each of the resulting +regions. However, this time, instead of splitting the entire predictor +space, we split one of the two previously identified regions. We now +have three regions. Again, we look to split one of these three regions +further, so as to minimize the MSE. The process continues until a +stopping criterion is reached; for instance, we may continue until no +region contains more than five observations.

    @@ -291,7 +404,7 @@ discrimination threshold is varied. It plots the true positive rate against the

  • 13
  • 14
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs005.html b/doc/pub/week47/html/._week47-bs005.html index a201413e7..0c3c57bb3 100644 --- a/doc/pub/week47/html/._week47-bs005.html +++ b/doc/pub/week47/html/._week47-bs005.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -177,59 +336,24 @@ MathJax.Hub.Config({

     

     

     

    - -

    Compare Bagging on Trees with Random Forests

    + +

    Pruning the tree

    - -
    -
    -
    -
    -
    -
    bag_clf = BaggingClassifier(
    -    DecisionTreeClassifier(splitter="random", max_leaf_nodes=16, random_state=42),
    -    n_estimators=500, max_samples=1.0, bootstrap=True, n_jobs=-1, random_state=42)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    bag_clf.fit(X_train, y_train)
    -y_pred = bag_clf.predict(X_test)
    -from sklearn.ensemble import RandomForestClassifier
    -rnd_clf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=16, n_jobs=-1, random_state=42)
    -rnd_clf.fit(X_train, y_train)
    -y_pred_rf = rnd_clf.predict(X_test)
    -np.sum(y_pred == y_pred_rf) / len(y_pred) 
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    The above procedure is rather straightforward, but leads often to +overfitting and unnecessarily large and complicated trees. The basic +idea is to grow a large tree \( T_0 \) and then prune it back in order to +obtain a subtree. A smaller tree with fewer splits (fewer regions) can +lead to smaller variance and better interpretation at the cost of a +little more bias. +

    +

    The so-called Cost complexity pruning algorithm gives us a +way to do just this. Rather than considering every possible subtree, +we consider a sequence of trees indexed by a nonnegative tuning +parameter \( \alpha \). +

    + +

    Read more at the following Scikit-Learn link on pruning.

    @@ -251,7 +375,7 @@ np.sum(y_pred =

  • 14
  • 15
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs006.html b/doc/pub/week47/html/._week47-bs006.html index 75826cd61..02177d080 100644 --- a/doc/pub/week47/html/._week47-bs006.html +++ b/doc/pub/week47/html/._week47-bs006.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,18 +337,34 @@ MathJax.Hub.Config({

     

     

     

    -

    Boosting, a Bird's Eye View

    +

    Cost complexity pruning

    -

    The basic idea is to combine weak classifiers in order to create a good -classifier. With a weak classifier we often intend a classifier which -produces results which are only slightly better than we would get by -random guesses. +

    For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that

    +$$ +\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T}, +$$ + +

    is as small as possible. Here \( \overline{T} \) is +the number of terminal nodes of the tree \( T \) , \( R_m \) is the +rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node.

    -

    This is done by applying in an iterative way a weak (or a standard -classifier like decision trees) to modify the data. In each iteration -we emphasize those observations which are misclassified by weighting -them with a factor. +

    The tuning parameter \( \alpha \) controls a trade-off between the subtree’s +complexity and its fit to the training data. When \( \alpha = 0 \), then the +subtree \( T \) will simply equal \( T_0 \), +because then the above equation just measures the +training error. +However, as \( \alpha \) increases, there is a price to pay for +having a tree with many terminal nodes. The above equation will +tend to be minimized for a smaller subtree. +

    + +

    It turns out that as we increase \( \alpha \) from zero +branches get pruned from the tree in a nested and predictable fashion, +so obtaining the whole sequence of subtrees as a function of \( \alpha \) is +easy. We can select a value of \( \alpha \) using a validation set or using +cross-validation. We then return to the full data set and obtain the +subtree corresponding to \( \alpha \).

    @@ -213,7 +388,7 @@ them with a factor.

  • 15
  • 16
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs007.html b/doc/pub/week47/html/._week47-bs007.html index 3ec09622d..5e0efdf60 100644 --- a/doc/pub/week47/html/._week47-bs007.html +++ b/doc/pub/week47/html/._week47-bs007.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,52 +337,26 @@ MathJax.Hub.Config({

     

     

     

    -

    What is boosting? Additive Modelling/Iterative Fitting

    +

    Schematic Regression Procedure

    -

    Boosting is a way of fitting an additive expansion in a set of -elementary basis functions like for example some simple polynomials. -Assume for example that we have a function -

    -$$ -f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m), -$$ +
    +
    + -

    where \( \beta_m \) are the expansion parameters to be determined in a -minimization process and \( b(x;\gamma_m) \) are some simple functions of -the multivariable parameter \( x \) which is characterized by the -parameters \( \gamma_m \). -

    +
      +
    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.
    2. +
    3. Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \( \alpha \).
    4. +
    5. 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:
    6. +
        +
      • repeat steps 1 and 2 on all but the \( k \)-th fold of the training data.
      • +
      • Then we valuate the mean squared prediction error on the data in the left-out \( k \)-th fold, as a function of \( \alpha \).
      • +
      • Finally we average the results for each value of \( \alpha \), and pick \( \alpha \) to minimize the average error.
      • +
      +
    7. Return the subtree from Step 2 that corresponds to the chosen value of \( \alpha \).
    8. +
    +
    +
    -

    As an example, consider the Sigmoid function we used in logistic -regression. In that case, we can translate the function -\( b(x;\gamma_m) \) into the Sigmoid function -

    - -$$ -\sigma(t) = \frac{1}{1+\exp{(-t)}}, -$$ - -

    where \( t=\gamma_0+\gamma_1 x \) and the parameters \( \gamma_0 \) and -\( \gamma_1 \) were determined by the Logistic Regression fitting -algorithm. -

    - -

    As another example, consider the cost function we defined for linear regression

    -$$ -C(\boldsymbol{y},\boldsymbol{f}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-f(x_i))^2. -$$ - -

    In this case the function \( f(x) \) was replaced by the design matrix -\( \boldsymbol{X} \) and the unknown linear regression parameters \( \boldsymbol{\beta} \), -that is \( \boldsymbol{f}=\boldsymbol{X}\boldsymbol{\beta} \). In linear regression we can -simply invert a matrix and obtain the parameters \( \beta \) by -

    - -$$ -\boldsymbol{\beta}=\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. -$$ - -

    In iterative fitting or additive modeling, we minimize the cost function with respect to the parameters \( \beta_m \) and \( \gamma_m \).

    @@ -247,7 +380,7 @@ $$

  • 16
  • 17
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs008.html b/doc/pub/week47/html/._week47-bs008.html index d91ce1404..41b8a33b8 100644 --- a/doc/pub/week47/html/._week47-bs008.html +++ b/doc/pub/week47/html/._week47-bs008.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,23 +337,20 @@ MathJax.Hub.Config({

     

     

     

    -

    Iterative Fitting, Regression and Squared-error Cost Function

    +

    A Classification Tree

    -

    The way we proceed is as follows (here we specialize to the squared-error cost function)

    - -
      -
    1. Establish a cost function, here \( {\cal C}(\boldsymbol{y},\boldsymbol{f}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-f_M(x_i))^2 \) with \( f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m) \).
    2. -
    3. Initialize with a guess \( f_0(x) \). It could be one or even zero or some random numbers.
    4. -
    5. For \( m=1:M \) -
        -
      1. minimize \( \sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta b(x;\gamma))^2 \) wrt \( \gamma \) and \( \beta \)
      2. -
      3. This gives the optimal values \( \beta_m \) and \( \gamma_m \)
      4. -
      5. Determine then the new values \( f_m(x)=f_{m-1}(x) +\beta_m b(x;\gamma_m) \)
      6. -
      -
    -

    We could use any of the algorithms we have discussed till now. If we -use trees, \( \gamma \) parameterizes the split variables and split points -at the internal nodes, and the predictions at the terminal nodes. +

    A classification tree is very similar to a regression tree, except +that it is used to predict a qualitative response rather than a +quantitative one. Recall that for a regression tree, the predicted +response for an observation is given by the mean response of the +training observations that belong to the same terminal node. In +contrast, for a classification tree, we predict that each observation +belongs to the most commonly occurring class of training observations +in the region to which it belongs. In interpreting the results of a +classification tree, we are often interested not only in the class +prediction corresponding to a particular terminal node region, but +also in the class proportions among the training observations that +fall into that region.

    @@ -220,7 +376,7 @@ at the internal nodes, and the predictions at the terminal nodes.

  • 17
  • 18
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs009.html b/doc/pub/week47/html/._week47-bs009.html index 89d58e251..55e722c2c 100644 --- a/doc/pub/week47/html/._week47-bs009.html +++ b/doc/pub/week47/html/._week47-bs009.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,46 +337,25 @@ MathJax.Hub.Config({

     

     

     

    -

    Squared-Error Example and Iterative Fitting

    +

    Growing a classification tree

    -

    To better understand what happens, let us develop the steps for the iterative fitting using the above squared error function.

    - -

    For simplicity we assume also that our functions \( b(x;\gamma)=1+\gamma x \).

    - -

    This means that for every iteration \( m \), we need to optimize

    - -$$ -(\beta_m,\gamma_m) = \mathrm{argmin}_{\beta,\lambda}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta b(x;\gamma))^2=\sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta(1+\gamma x_i))^2. -$$ - -

    We start our iteration by simply setting \( f_0(x)=0 \). -Taking the derivatives with respect to \( \beta \) and \( \gamma \) we obtain -

    -$$ -\frac{\partial {\cal C}}{\partial \beta} = -2\sum_{i}(1+\gamma x_i)(y_i-\beta(1+\gamma x_i))=0, -$$ - -

    and

    -$$ -\frac{\partial {\cal C}}{\partial \gamma} =-2\sum_{i}\beta x_i(y_i-\beta(1+\gamma x_i))=0. -$$ - -

    We can then rewrite these equations as (defining \( \boldsymbol{w}=\boldsymbol{e}+\gamma \boldsymbol{x}) \) with \( \boldsymbol{e} \) being the unit vector)

    -$$ -\gamma \boldsymbol{w}^T(\boldsymbol{y}-\beta\gamma \boldsymbol{w})=0, -$$ - -

    which gives us \( \beta = \boldsymbol{w}^T\boldsymbol{y}/(\boldsymbol{w}^T\boldsymbol{w}) \). Similarly we have

    -$$ -\beta\gamma \boldsymbol{x}^T(\boldsymbol{y}-\beta(1+\gamma \boldsymbol{x}))=0, -$$ - -

    which leads to \( \gamma =(\boldsymbol{x}^T\boldsymbol{y}-\beta\boldsymbol{x}^T\boldsymbol{e})/(\beta\boldsymbol{x}^T\boldsymbol{x}) \). Inserting -for \( \beta \) gives us an equation for \( \gamma \). This is a non-linear equation in the unknown \( \gamma \) and has to be solved numerically. +

    The task of growing a +classification tree is quite similar to the task of growing a +regression tree. Just as in the regression setting, we use recursive +binary splitting to grow a classification tree. However, in the +classification setting, the MSE cannot be used as a criterion for making +the binary splits. A natural alternative to MSE is the classification +error rate. Since we plan to assign an observation in a given region +to the most commonly occurring error rate class of training +observations in that region, the classification error rate is simply +the fraction of the training observations in that region that do not +belong to the most common class.

    -

    The solution to these two equations gives us in turn \( \beta_1 \) and \( \gamma_1 \) leading to the new expression for \( f_1(x) \) as -\( f_1(x) = \beta_1(1+\gamma_1x) \). Doing this \( M \) times results in our final estimate for the function \( f \). +

    When building a classification tree, either the Gini index or the +entropy are typically used to evaluate the quality of a particular +split, since these two approaches are more sensitive to node purity +than is the classification error rate.

    @@ -244,7 +382,7 @@ for \( \beta \) gives us an equation for \( \gamma \). This is a non-linear equa

  • 18
  • 19
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs010.html b/doc/pub/week47/html/._week47-bs010.html index 6c5cfced6..b4ed388e5 100644 --- a/doc/pub/week47/html/._week47-bs010.html +++ b/doc/pub/week47/html/._week47-bs010.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,34 +337,46 @@ MathJax.Hub.Config({

     

     

     

    -

    Iterative Fitting, Classification and AdaBoost

    +

    Classification tree, how to split nodes

    -

    Let us consider a binary classification problem with two outcomes \( y_i \in \{-1,1\} \) and \( i=0,1,2,\dots,n-1 \) as our set of -observations. We define a classification function \( G(x) \) which produces a prediction taking one or the other of the two values -\( \{-1,1\} \). +

    If our targets are the outcome of a classification process that takes +for example \( k=1,2,\dots,K \) values, the only thing we need to think of +is to set up the splitting criteria for each node.

    -

    The error rate of the training sample is then

    - -$$ -\mathrm{\overline{err}}=\frac{1}{n} \sum_{i=0}^{n-1} I(y_i\ne G(x_i)). -$$ - -

    The iterative procedure starts with defining a weak classifier whose -error rate is barely better than random guessing. The iterative -procedure in boosting is to sequentially apply a weak -classification algorithm to repeatedly modified versions of the data -producing a sequence of weak classifiers \( G_m(x) \). +

    We define a PDF \( p_{mk} \) that represents the number of observations of +a class \( k \) in a region \( R_m \) with \( N_m \) observations. We represent +this likelihood function in terms of the proportion \( I(y_i=k) \) of +observations of this class in the region \( R_m \) as

    -

    Here we will express our function \( f(x) \) in terms of \( G(x) \). That is

    $$ -f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m), +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i=k). $$ -

    will be a function of

    +

    We let \( p_{mk} \) represent the majority class of observations in region +\( m \). The three most common ways of splitting a node are given by +

    + + $$ -G_M(x) = \mathrm{sign} \sum_{i=1}^M \alpha_m G_m(x). +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i\ne k) = 1-p_{mk}. +$$ + + +$$ +g = \sum_{k=1}^K p_{mk}(1-p_{mk}). +$$ + + +$$ +s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}. $$ @@ -234,7 +405,7 @@ $$
  • 19
  • 20
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs011.html b/doc/pub/week47/html/._week47-bs011.html index e0504905d..990d79fb2 100644 --- a/doc/pub/week47/html/._week47-bs011.html +++ b/doc/pub/week47/html/._week47-bs011.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,29 +337,62 @@ MathJax.Hub.Config({

     

     

     

    -

    Adaptive Boosting, AdaBoost

    +

    Visualizing the Tree, Classification

    -

    In our iterative procedure we define thus

    -$$ -f_m(x) = f_{m-1}(x)+\beta_mG_m(x). -$$ + +
    +
    +
    +
    +
    +
    import os
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.metrics import confusion_matrix
    +from sklearn.tree import export_graphviz
     
    -

    The simplest possible cost function which leads (also simple from a computational point of view) to the AdaBoost algorithm is the -exponential cost/loss function defined as -

    -$$ -C(\boldsymbol{y},\boldsymbol{f}) = \sum_{i=0}^{n-1}\exp{(-y_i(f_{m-1}(x_i)+\beta G(x_i))}. -$$ +from IPython.display import Image +from pydot import graph_from_dot_data +import pandas as pd +import numpy as np -

    We optimize \( \beta \) and \( G \) for each value of \( m=1:M \) as we did in the regression case. -This is normally done in two steps. Let us however first rewrite the cost function as -

    -$$ -C(\boldsymbol{y},\boldsymbol{f}) = \sum_{i=0}^{n-1}w_i^{m}\exp{(-y_i\beta G(x_i))}, -$$ +cancer = load_breast_cancer() +X = pd.DataFrame(cancer.data, columns=cancer.feature_names) +print(X) +y = pd.Categorical.from_codes(cancer.target, cancer.target_names) +y = pd.get_dummies(y) +print(y) +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) +tree_clf = DecisionTreeClassifier(max_depth=5) +tree_clf.fit(X_train, y_train) + +export_graphviz( + tree_clf, + out_file="DataFiles/cancer.dot", + feature_names=cancer.feature_names, + class_names=cancer.target_names, + rounded=True, + filled=True +) +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png' +os.system(cmd) +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    where we have defined \( w_i^m= \exp{(-y_if_{m-1}(x_i))} \).

    @@ -227,7 +419,7 @@ $$

  • 20
  • 21
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs012.html b/doc/pub/week47/html/._week47-bs012.html index c9c860c1e..4ffb6eb87 100644 --- a/doc/pub/week47/html/._week47-bs012.html +++ b/doc/pub/week47/html/._week47-bs012.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,44 +337,52 @@ MathJax.Hub.Config({

     

     

     

    -

    Building up AdaBoost

    +

    Visualizing the Tree, The Moons

    -

    First, for any \( \beta > 0 \), we optimize \( G \) by setting

    -$$ -G_m(x) = \mathrm{sign} \sum_{i=0}^{n-1} w_i^m I(y_i \ne G_(x_i)), -$$ + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.datasets import make_moons
    +from sklearn.tree import export_graphviz
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import os
     
    -

    which is the classifier that minimizes the weighted error rate in predicting \( y \).

    +np.random.seed(42) +X, y = make_moons(n_samples=100, noise=0.25, random_state=53) +X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0) +tree_clf = DecisionTreeClassifier(max_depth=5) +tree_clf.fit(X_train, y_train) -

    We can do this by rewriting

    -$$ -\exp{-(\beta)}\sum_{y_i=G(x_i)}w_i^m+\exp{(\beta)}\sum_{y_i\ne G(x_i)}w_i^m, -$$ - -

    which can be rewritten as

    -$$ -(\exp{(\beta)}-\exp{-(\beta)})\sum_{i=0}^{n-1}w_i^mI(y_i\ne G(x_i))+\exp{(-\beta)}\sum_{i=0}^{n-1}w_i^m=0, -$$ - -

    which leads to

    -$$ -\beta_m = \frac{1}{2}\log{\frac{1-\mathrm{\overline{err}}}{\mathrm{\overline{err}}}}, -$$ - -

    where we have redefined the error as

    -$$ -\mathrm{\overline{err}}_m=\frac{1}{n}\frac{\sum_{i=0}^{n-1}w_i^mI(y_i\ne G(x_i)}{\sum_{i=0}^{n-1}w_i^m}, -$$ - -

    which leads to an update of

    -$$ -f_m(x) = f_{m-1}(x) +\beta_m G_m(x). -$$ - -

    This leads to the new weights

    -$$ -w_i^{m+1} = w_i^m \exp{(-y_i\beta_m G_m(x_i))} -$$ +export_graphviz( + tree_clf, + out_file="DataFiles/moons.dot", + rounded=True, + filled=True +) +cmd = 'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png' +os.system(cmd) +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -243,7 +410,7 @@ $$

  • 21
  • 22
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs013.html b/doc/pub/week47/html/._week47-bs013.html index 6273228a0..57934981c 100644 --- a/doc/pub/week47/html/._week47-bs013.html +++ b/doc/pub/week47/html/._week47-bs013.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,23 +337,39 @@ MathJax.Hub.Config({

     

     

     

    -

    Adaptive boosting: AdaBoost, Basic Algorithm

    +

    Other ways of visualizing the trees

    -

    The algorithm here is rather straightforward. Assume that our weak -classifier is a decision tree and we consider a binary set of outputs -with \( y_i \in \{-1,1\} \) and \( i=0,1,2,\dots,n-1 \) as our set of -observations. Our design matrix is given in terms of the -feature/predictor vectors -\( \boldsymbol{X}=[\boldsymbol{x}_0\boldsymbol{x}_1\dots\boldsymbol{x}_{p-1}] \). Finally, we define also a -classifier determined by our data via a function \( G(x) \). This function tells us how well we are able to classify our outputs/targets \( \boldsymbol{y} \). -

    +

    Scikit-Learn has also another way to visualize the trees which is very useful, here with the Iris data.

    -

    We have already defined the misclassification error \( \mathrm{err} \) as

    -$$ -\mathrm{err}=\frac{1}{n}\sum_{i=0}^{n-1}I(y_i\ne G(x_i)), -$$ -

    where the function \( I() \) is one if we misclassify and zero if we classify correctly.

    + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn import tree
    +X, y = load_iris(return_X_y=True)
    +tree_clf = tree.DecisionTreeClassifier()
    +tree_clf = tree_clf.fit(X, y)
    +# and then plot the tree
    +tree.plot_tree(tree_clf) 
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -221,7 +396,7 @@ $$

  • 22
  • 23
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs014.html b/doc/pub/week47/html/._week47-bs014.html index 30fcd1d72..087447615 100644 --- a/doc/pub/week47/html/._week47-bs014.html +++ b/doc/pub/week47/html/._week47-bs014.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,37 +337,42 @@ MathJax.Hub.Config({

     

     

     

    -

    Basic Steps of AdaBoost

    +

    Printing out as text

    -

    With the above definitions we are now ready to set up the algorithm for AdaBoost. -The basic idea is to set up weights which will be used to scale the correctly classified and the misclassified cases. +

    Alternatively, the tree can also be exported in textual format with the function exporttext. +This method doesn’t require the installation of external libraries and is more compact:

    -
      -
    1. We start by initializing all weights to \( w_i = 1/n \), with \( i=0,1,2,\dots n-1 \). It is easy to see that we must have \( \sum_{i=0}^{n-1}w_i = 1 \).
    2. -
    3. We rewrite the misclassification error as
    4. -
    -$$ -\mathrm{\overline{err}}_m=\frac{\sum_{i=0}^{n-1}w_i^m I(y_i\ne G(x_i))}{\sum_{i=0}^{n-1}w_i}, -$$ -
      -
    1. Then we start looping over all attempts at classifying, namely we start an iterative process for \( m=1:M \), where \( M \) is the final number of classifications. Our given classifier could for example be a plain decision tree. -
        -
      1. Fit then a given classifier to the training set using the weights \( w_i \).
      2. -
      3. Compute then \( \mathrm{err} \) and figure out which events are classified properly and which are classified wrongly.
      4. -
      5. Define a quantity \( \alpha_{m} = \log{(1-\mathrm{\overline{err}}_m)/\mathrm{\overline{err}}_m} \)
      6. -
      7. Set the new weights to \( w_i = w_i\times \exp{(\alpha_m I(y_i\ne G(x_i)} \).
      8. -
      -
    2. Compute the new classifier \( G(x)= \sum_{i=0}^{n-1}\alpha_m I(y_i\ne G(x_i) \).
    3. -
    -

    For the iterations with \( m \le 2 \) the weights are modified -individually at each steps. The observations which were misclassified -at iteration \( m-1 \) have a weight which is larger than those which were -classified properly. As this proceeds, the observations which were -difficult to classifiy correctly are given a larger influence. Each -new classification step \( m \) is then forced to concentrate on those -observations that are missed in the previous iterations. -

    + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.tree import export_text
    +iris = load_iris()
    +decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
    +decision_tree = decision_tree.fit(iris.data, iris.target)
    +r = export_text(decision_tree, feature_names=iris['feature_names'])
    +print(r)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -235,7 +399,7 @@ observations that are missed in the previous iterations.

  • 23
  • 24
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs015.html b/doc/pub/week47/html/._week47-bs015.html index 6c05168f6..c0695c9a1 100644 --- a/doc/pub/week47/html/._week47-bs015.html +++ b/doc/pub/week47/html/._week47-bs015.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,46 +337,18 @@ MathJax.Hub.Config({

     

     

     

    -

    AdaBoost Examples

    - -

    Using Scikit-Learn it is easy to apply the adaptive boosting algorithm, as done here.

    - - - -
    -
    -
    -
    -
    -
    from sklearn.ensemble import AdaBoostClassifier
    -
    -ada_clf = AdaBoostClassifier(
    -    DecisionTreeClassifier(max_depth=2), n_estimators=200,
    -    algorithm="SAMME.R", learning_rate=0.01, random_state=42)
    -ada_clf.fit(X_train, y_train)
    -y_pred = ada_clf.predict(X_test)
    -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
    -plt.show()
    -y_probas = ada_clf.predict_proba(X_test)
    -skplt.metrics.plot_roc(y_test, y_probas)
    -plt.show()
    -skplt.metrics.plot_cumulative_gain(y_test, y_probas)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    Algorithms for Setting up Decision Trees

    +

    Two algorithms stand out in the set up of decision trees:

    +
      +
    1. The CART (Classification And Regression Tree) algorithm for both classification and regression
    2. +
    3. The ID3 algorithm based on the computation of the information gain for classification
    4. +
    +

    We discuss both algorithms with applications here. The popular library +Scikit-Learn uses the CART algorithm. For classification problems +you can use either the gini index or the entropy to split a tree +in two branches. +

    @@ -243,6 +374,8 @@ plt.show()

  • 23
  • 24
  • 25
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs016.html b/doc/pub/week47/html/._week47-bs016.html index 697ba07d3..92d2e75a6 100644 --- a/doc/pub/week47/html/._week47-bs016.html +++ b/doc/pub/week47/html/._week47-bs016.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,16 +337,29 @@ MathJax.Hub.Config({

     

     

     

    -

    Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent

    +

    The CART algorithm for Classification

    -

    Gradient boosting is again a similar technique to Adaptive boosting, -it combines so-called weak classifiers or regressors into a strong -method via a series of iterations. +

    For classification, the CART algorithm splits the data set in two subsets using a single feature \( k \) and a threshold \( t_k \). +This could be for example a threshold set by a number below a certain circumference of a malign tumor.

    -

    In order to understand the method, let us illustrate its basics by -bringing back the essential steps in linear regression, where our cost -function was the least squares function. +

    How do we find these two quantities? +We search for the pair \( (k,t_k) \) that produces the purest subset using for example the gini factor \( G \). +The cost function it tries to minimize is then +

    +$$ +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}G_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}G_{\mathrm{right}}, +$$ + +

    where \( G_{\mathrm{left/right}} \) measures the impurity of the left/right subset and \( m_{\mathrm{left/right}} \) + is the number of instances in the left/right subset +

    + +

    Once it has successfully split the training set in two, it splits the subsets using the same logic, then the subsubsets +and so on, recursively. It stops recursing once it reaches the maximum depth (defined by the +\( max\_depth \) hyperparameter), or if it cannot find a split that will reduce impurity. A few other +hyperparameters control additional stopping conditions such as the \( min\_samples\_split \), +\( min\_samples\_leaf \), \( min\_weight\_fraction\_leaf \), and \( max\_leaf\_nodes \).

    @@ -213,6 +385,9 @@ function was the least squares function.

  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs017.html b/doc/pub/week47/html/._week47-bs017.html index 0b3c0d43a..02ce7dbd8 100644 --- a/doc/pub/week47/html/._week47-bs017.html +++ b/doc/pub/week47/html/._week47-bs017.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,36 +337,31 @@ MathJax.Hub.Config({

     

     

     

    -

    The Squared-Error again! Steepest Descent

    +

    The CART algorithm for Regression

    -

    We start again with our cost function \( {\cal C}(\boldsymbol{y}m\boldsymbol{f})=\sum_{i=0}^{n-1}{\cal L}(y_i, f(x_i)) \) where we want to minimize -This means that for every iteration, we need to optimize +

    The CART algorithm for regression works is similar to the one for classification except that instead of trying to split the +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

    - $$ -(\hat{\boldsymbol{f}}) = \mathrm{argmin}_{\boldsymbol{f}}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i-f(x_i))^2. +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}\mathrm{MSE}_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}\mathrm{MSE}_{\mathrm{right}}. $$ -

    We define a real function \( h_m(x) \) that defines our final function \( f_M(x) \) as

    +

    Here the MSE for a specific node is defined as

    $$ -f_M(x) = \sum_{m=0}^M h_m(x). +\mathrm{MSE}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}(\overline{y}_{\mathrm{node}}-y_i)^2, $$ -

    In the steepest decent approach we approximate \( h_m(x) = -\rho_m g_m(x) \), where \( \rho_m \) is a scalar and \( g_m(x) \) the gradient defined as

    +

    with

    $$ -g_m(x_i) = \left[ \frac{\partial {\cal L}(y_i, f(x_i))}{\partial f(x_i)}\right]_{f(x_i)=f_{m-1}(x_i)}. +\overline{y}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}y_i, $$ -

    With the new gradient we can update \( f_m(x) = f_{m-1}(x) -\rho_m g_m(x) \). Using the above squared-error function we see that -the gradient is \( g_m(x_i) = -2(y_i-f(x_i)) \). +

    the mean value of all observations in a specific node.

    + +

    Without any regularization, the regression task for decision trees, +just like for classification tasks, is prone to overfitting.

    -

    Choosing \( f_0(x)=0 \) we obtain \( g_m(x) = -2y_i \) and inserting this into the minimization problem for the cost function we have

    -$$ -(\rho_1) = \mathrm{argmin}_{\rho}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i+2\rho y_i)^2. -$$ - -

    diff --git a/doc/pub/week47/html/._week47-bs018.html b/doc/pub/week47/html/._week47-bs018.html index 82d36ba70..ffed59296 100644 --- a/doc/pub/week47/html/._week47-bs018.html +++ b/doc/pub/week47/html/._week47-bs018.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,19 +337,13 @@ MathJax.Hub.Config({

     

     

     

    -

    Steepest Descent Example

    +

    Why binary splits?

    -

    Optimizing with respect to \( \rho \) we obtain (taking the derivative) that \( \rho_1 = -1/2 \). We have then that

    -$$ -f_1(x) = f_{0}(x) -\rho_1 g_1(x)=-y_i. -$$ - -

    We can then proceed and compute

    -$$ -g_2(x_i) = \left[ \frac{\partial {\cal L}(y_i, f(x_i))}{\partial f(x_i)}\right]_{f(x_i)=f_{1}(x_i)=y_i}=-4y_i, -$$ - -

    and find a new value for \( \rho_2=-1/2 \) and continue till we have reached \( m=M \). We can modify the steepest descent method, or steepest boosting, by introducing what is called gradient boosting.

    +

    It is custom to split to a tree uising binary splits. The reason is +that multiway splits fragment the data too quickly, leaving +insufficient data at the next level down. Multiway splits can be +achieved by a series of binary split and this is normally preferred. +

    @@ -213,6 +366,11 @@ $$

  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • 27
  • +
  • 28
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs019.html b/doc/pub/week47/html/._week47-bs019.html index 6a451c077..042b66bbe 100644 --- a/doc/pub/week47/html/._week47-bs019.html +++ b/doc/pub/week47/html/._week47-bs019.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,28 +337,22 @@ MathJax.Hub.Config({

     

     

     

    -

    Gradient Boosting, algorithm

    +

    Computing a Tree using the Gini Index

    -

    Steepest descent is however not much used, since it only optimizes \( f \) at a fixed set of \( n \) points, -so we do not learn a function that can generalize. However, we can modify the algorithm by -fitting a weak learner to approximate the negative gradient signal. +

    Consider the following example with attributes/features and two +possible outcomes (classes) for each attribute. Assume we wish to find some +correlations between the average grade of a student as function of the +number of hours studied and hours slept. We want also to correlate the +grade in a given course with the general trend, whether the students +recently has gotten grades below average or above.

    -

    Suppose we have a cost function \( C(f)=\sum_{i=0}^{n-1}L(y_i, f(x_i)) \) where \( y_i \) is our target and \( f(x_i) \) the function which is meant to model \( y_i \). The above cost function could be our standard squared-error function

    -$$ -C(\boldsymbol{y},\boldsymbol{f})=\sum_{i=0}^{n-1}(y_i-f(x_i))^2. -$$ - -

    The way we proceed in an iterative fashion is to

    +

    We have three features/attributes

      -
    1. Initialize our estimate \( f_0(x) \).
    2. -
    3. For \( m=1:M \), we -
        -
      1. compute the negative gradient vector \( \boldsymbol{u}_m = -\partial C(\boldsymbol{y},\boldsymbol{f})/\partial \boldsymbol{f}(x) \) at \( f(x) = f_{m-1}(x) \);
      2. -
      3. fit the so-called base-learner to the negative gradient \( h_m(u_m,x) \);
      4. -
      5. update the estimate \( f_m(x) = f_{m-1}(x)+h_m(u_m,x) \);
      6. -
      -
    4. The final estimate is then \( f_M(x) = \sum_{m=1}^M h_m(u_m,x) \).
    5. +
    6. Trend of average grades before present course, classified as either below or above the average grade of the whole class
    7. +
    8. The number of hours studies, classified again as either higher (more than 3 hours per day) or lower . Here we have used a standard for one \( ECTS \) which is scaled to 25-30 hours of work for a semester which lasts 18 weeks, with 15 weeks of lectures and 3 weeks for exams, assuming a total of 30 ECTS per semester.
    9. +
    10. The number of hours slept as high for more than \( 8 \) hours and below for less than 8 hours of sleep, classified again as either high or low
    11. +
    12. The final grade whether it is above or below average

    @@ -221,6 +374,12 @@ $$

  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • 27
  • +
  • 28
  • +
  • 29
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs020.html b/doc/pub/week47/html/._week47-bs020.html index 52c2cf3d0..52c6fcc08 100644 --- a/doc/pub/week47/html/._week47-bs020.html +++ b/doc/pub/week47/html/._week47-bs020.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,70 +337,29 @@ MathJax.Hub.Config({

     

     

     

    -

    Gradient Boosting, Examples of Regression

    - - -
    -
    -
    -
    -
    -
    import matplotlib.pyplot as plt
    -import numpy as np
    -from sklearn.model_selection import train_test_split
    -from sklearn.ensemble import GradientBoostingRegressor
    -import scikitplot as skplt
    -from sklearn.metrics import mean_squared_error
    -
    -n = 100
    -maxdegree = 6
    -
    -# Make data set.
    -x = np.linspace(-3, 3, n).reshape(-1, 1)
    -y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
    -
    -error = np.zeros(maxdegree)
    -bias = np.zeros(maxdegree)
    -variance = np.zeros(maxdegree)
    -polydegree = np.zeros(maxdegree)
    -X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    -
    -for degree in range(1,maxdegree):
    -    model = GradientBoostingRegressor(max_depth=degree, n_estimators=100, learning_rate=1.0)  
    -    model.fit(X_train,y_train)
    -    y_pred = model.predict(X_test)
    -    polydegree[degree] = degree
    -    error[degree] = np.mean( np.mean((y_test - y_pred)**2) )
    -    bias[degree] = np.mean( (y_test - np.mean(y_pred))**2 )
    -    variance[degree] = np.mean( np.var(y_pred) )
    -    print('Max depth:', degree)
    -    print('Error:', error[degree])
    -    print('Bias^2:', bias[degree])
    -    print('Var:', variance[degree])
    -    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
    -
    -plt.xlim(1,maxdegree-1)
    -plt.plot(polydegree, error, label='Error')
    -plt.plot(polydegree, bias, label='bias')
    -plt.plot(polydegree, variance, label='Variance')
    -plt.legend()
    -save_fig("gdregression")
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    The Table

    +
    +
    + + + + + + + + + + + + + + + + +
    Grade Trend Hours slept Hours Studied Grade
    Above Low High Above
    Below High Low Below
    Above Low High Above
    Above High High Above
    Below Low High Below
    Above Low Low Below
    Below High High Below
    Below Low High Below
    Above Low Low Below
    Above High High Above
    +
    +

    @@ -262,6 +380,13 @@ plt.show()

  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • 27
  • +
  • 28
  • +
  • 29
  • +
  • 30
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs021.html b/doc/pub/week47/html/._week47-bs021.html index d40820fae..9f9c835ab 100644 --- a/doc/pub/week47/html/._week47-bs021.html +++ b/doc/pub/week47/html/._week47-bs021.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,67 +337,17 @@ MathJax.Hub.Config({

     

     

     

    -

    Gradient Boosting, Classification Example

    +

    Computing the various Gini Indices

    - -
    -
    -
    -
    -
    -
    import matplotlib.pyplot as plt
    -import numpy as np
    -from sklearn.model_selection import  train_test_split 
    -from sklearn.datasets import load_breast_cancer
    -import scikitplot as skplt
    -from sklearn.ensemble import GradientBoostingClassifier
    -from sklearn.model_selection import cross_validate
    +

    In computations we will translate all classes into numbers. Being +these binary classes, they can easily be split into ones and zeros. +

    -# Load the data -cancer = load_breast_cancer() - -X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) -#now scale the data -from sklearn.preprocessing import StandardScaler -scaler = StandardScaler() -scaler.fit(X_train) -X_train_scaled = scaler.transform(X_train) -X_test_scaled = scaler.transform(X_test) - -gd_clf = GradientBoostingClassifier(max_depth=3, n_estimators=100, learning_rate=1.0) -gd_clf.fit(X_train_scaled, y_train) -#Cross validation -accuracy = cross_validate(gd_clf,X_test_scaled,y_test,cv=10)['test_score'] -print(accuracy) -print("Test set accuracy with Gradient boosting and scaled data: {:.2f}".format(gd_clf.score(X_test_scaled,y_test))) - -import scikitplot as skplt -y_pred = gd_clf.predict(X_test_scaled) -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) -save_fig("gdclassiffierconfusion") -plt.show() -y_probas = gd_clf.predict_proba(X_test_scaled) -skplt.metrics.plot_roc(y_test, y_probas) -save_fig("gdclassiffierroc") -plt.show() -skplt.metrics.plot_cumulative_gain(y_test, y_probas) -save_fig("gdclassiffiercgain") -plt.show() -
    + -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    @@ -260,6 +369,14 @@ plt.show()
  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • 27
  • +
  • 28
  • +
  • 29
  • +
  • 30
  • +
  • 31
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs022.html b/doc/pub/week47/html/._week47-bs022.html index 50eac0561..6a197fae6 100644 --- a/doc/pub/week47/html/._week47-bs022.html +++ b/doc/pub/week47/html/._week47-bs022.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,22 +337,90 @@ MathJax.Hub.Config({

     

     

     

    -

    XGBoost: Extreme Gradient Boosting

    +

    A possible code using Scikit-Learn

    -

    XGBoost or Extreme Gradient -Boosting, is an optimized distributed gradient boosting library -designed to be highly efficient, flexible and portable. It implements -machine learning algorithms under the Gradient Boosting -framework. XGBoost provides a parallel tree boosting that solve many -data science problems in a fast and accurate way. See the article by Chen and Guestrin. -

    -

    The authors design and build a highly scalable end-to-end tree -boosting system. It has a theoretically justified weighted quantile -sketch for efficient proposal calculation. It introduces a novel sparsity-aware algorithm for parallel tree learning and an effective cache-aware block structure for out-of-core tree learning. -

    + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +infile = open(data_path("grades.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +grades = pd.read_csv(infile)
    +grades = pd.DataFrame(grades)
    +display(grades)
    +# Features and targets
    +X = grades.loc[:, grades.columns != 'Grade'].values
    +y = grades.loc[:, grades.columns == 'Grade'].values
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/grade.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    It is now the algorithm which wins essentially all ML competitions!!!

    @@ -212,6 +439,15 @@ sketch for efficient proposal calculation. It introduces a novel sparsity-aware

  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • 27
  • +
  • 28
  • +
  • 29
  • +
  • 30
  • +
  • 31
  • +
  • 32
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs023.html b/doc/pub/week47/html/._week47-bs023.html index b4a811019..b336782cd 100644 --- a/doc/pub/week47/html/._week47-bs023.html +++ b/doc/pub/week47/html/._week47-bs023.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,71 +337,46 @@ MathJax.Hub.Config({

     

     

     

    -

    Regression Case

    +

    Further example: Computing the Gini index

    +

    The next example we will look at is a classical one in many Machine +Learning applications. Based on various meteorological features, we +have several so-called attributes which decide whether we at the end +will do some outdoor activity like skiing, going for a bike ride etc +etc. The table here contains the feautures outlook, temperature, +humidity and wind. The target or output is whether we ride +(True=1) or whether we do something else that day (False=0). The +attributes for each feature are then sunny, overcast and rain for the +outlook, hot, cold and mild for temperature, high and normal for +humidity and weak and strong for wind. +

    - -
    -
    -
    -
    -
    -
    import matplotlib.pyplot as plt
    -import numpy as np
    -from sklearn.model_selection import train_test_split
    -import xgboost as xgb
    -import scikitplot as skplt
    -from sklearn.metrics import mean_squared_error
    -
    -n = 100
    -maxdegree = 6
    -
    -# Make data set.
    -x = np.linspace(-3, 3, n).reshape(-1, 1)
    -y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
    -
    -error = np.zeros(maxdegree)
    -bias = np.zeros(maxdegree)
    -variance = np.zeros(maxdegree)
    -polydegree = np.zeros(maxdegree)
    -X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    -
    -for degree in range(maxdegree):
    -    model =  xgb.XGBRegressor(objective ='reg:squarederror', colsaobjective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.1,max_depth = degree, alpha = 10, n_estimators = 200)
    -
    -    model.fit(X_train,y_train)
    -    y_pred = model.predict(X_test)
    -    polydegree[degree] = degree
    -    error[degree] = np.mean( np.mean((y_test - y_pred)**2) )
    -    bias[degree] = np.mean( (y_test - np.mean(y_pred))**2 )
    -    variance[degree] = np.mean( np.var(y_pred) )
    -    print('Max depth:', degree)
    -    print('Error:', error[degree])
    -    print('Bias^2:', bias[degree])
    -    print('Var:', variance[degree])
    -    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
    -
    -plt.xlim(1,maxdegree-1)
    -plt.plot(polydegree, error, label='Error')
    -plt.plot(polydegree, bias, label='bias')
    -plt.plot(polydegree, variance, label='Variance')
    -plt.legend()
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - +

    The table here summarizes the various attributes and

    +
    +
    + + + + + + + + + + + + + + + + + + + + +
    Day Outlook Temperature Humidity Wind Ride
    1 Sunny Hot High Weak 0
    2 Sunny Hot High Strong 1
    3 Overcast Hot High Weak 1
    4 Rain Mild High Weak 1
    5 Rain Cool Normal Weak 1
    6 Rain Cool Normal Strong 0
    7 Overcast Cool Normal Strong 1
    8 Sunny Mild High Weak 0
    9 Sunny Cool Normal Weak 1
    10 Rain Mild Normal Weak 1
    11 Sunny Mild Normal Strong 1
    12 Overcast Mild High Strong 1
    13 Overcast Hot Normal Weak 1
    14 Rain Mild High Strong 0
    +
    +

    @@ -260,6 +394,16 @@ plt.show()

  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • 27
  • +
  • 28
  • +
  • 29
  • +
  • 30
  • +
  • 31
  • +
  • 32
  • +
  • 33
  • +
  • ...
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs024.html b/doc/pub/week47/html/._week47-bs024.html index f91c47d10..e01de6b67 100644 --- a/doc/pub/week47/html/._week47-bs024.html +++ b/doc/pub/week47/html/._week47-bs024.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -178,9 +337,8 @@ MathJax.Hub.Config({

     

     

     

    -

    Xgboost on the Cancer Data

    +

    Simple Python Code to read in Data and perform Classification

    -

    As you will see from the confusion matrix below, XGBoots does an excellent job on the Wisconsin cancer data and outperforms essentially all agorithms we have discussed till now.

    @@ -188,57 +346,73 @@ MathJax.Hub.Config({
    -
    import matplotlib.pyplot as plt
    +  
    # Common imports
     import numpy as np
    -from sklearn.model_selection import  train_test_split 
    -from sklearn.datasets import load_breast_cancer
    -from sklearn.preprocessing import LabelEncoder
    -from sklearn.model_selection import cross_validate
    -import scikitplot as skplt
    -import xgboost as xgb
    -# Load the data
    -cancer = load_breast_cancer()
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
     
    -X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    -print(X_train.shape)
    -print(X_test.shape)
    -#now scale the data
    -from sklearn.preprocessing import StandardScaler
    -scaler = StandardScaler()
    -scaler.fit(X_train)
    -X_train_scaled = scaler.transform(X_train)
    -X_test_scaled = scaler.transform(X_test)
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
     
    -xg_clf = xgb.XGBClassifier()
    -xg_clf.fit(X_train_scaled,y_train)
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
     
    -y_test = xg_clf.predict(X_test_scaled)
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
     
    -print("Test set accuracy with Gradient Boosting and scaled data: {:.2f}".format(xg_clf.score(X_test_scaled,y_test)))
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
     
    -import scikitplot as skplt
    -y_pred = xg_clf.predict(X_test_scaled)
    -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
    -save_fig("xdclassiffierconfusion")
    -plt.show()
    -y_probas = xg_clf.predict_proba(X_test_scaled)
    -skplt.metrics.plot_roc(y_test, y_probas)
    -save_fig("xdclassiffierroc")
    -plt.show()
    -skplt.metrics.plot_cumulative_gain(y_test, y_probas)
    -save_fig("gdclassiffiercgain")
    -plt.show()
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
     
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
     
    -xgb.plot_tree(xg_clf,num_trees=0)
    -plt.rcParams['figure.figsize'] = [50, 10]
    -save_fig("xgtree")
    -plt.show()
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
     
    -xgb.plot_importance(xg_clf)
    -plt.rcParams['figure.figsize'] = [5, 5]
    -save_fig("xgparams")
    -plt.show()
    +infile = open(data_path("rideclass.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +ridedata = pd.read_csv(infile,names = ('Outlook','Temperature','Humidity','Wind','Ride'))
    +ridedata = pd.DataFrame(ridedata)
    +
    +# Features and targets
    +X = ridedata.loc[:, ridedata.columns != 'Ride'].values
    +y = ridedata.loc[:, ridedata.columns == 'Ride'].values
    +
    +# Create the encoder.
    +encoder = OneHotEncoder(handle_unknown="ignore")
    +# Assume for simplicity all features are categorical.
    +encoder.fit(X)    
    +# Apply the encoder.
    +X = encoder.transform(X)
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/ride.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
    +os.system(cmd)
     
    @@ -270,6 +444,18 @@ plt.show()
  • 23
  • 24
  • 25
  • +
  • 26
  • +
  • 27
  • +
  • 28
  • +
  • 29
  • +
  • 30
  • +
  • 31
  • +
  • 32
  • +
  • 33
  • +
  • 34
  • +
  • ...
  • +
  • 66
  • +
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs025.html b/doc/pub/week47/html/._week47-bs025.html index cfc8c54ed..78b2aec07 100644 --- a/doc/pub/week47/html/._week47-bs025.html +++ b/doc/pub/week47/html/._week47-bs025.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,7 +337,98 @@ MathJax.Hub.Config({

     

     

     

    -

    Summary of course

    +

    Computing the Gini Factor

    + +

    The above functions (gini, entropy and misclassification error) are +important components of the so-called CART algorithm. We will discuss +this algorithm below after we have discussed the information gain +algorithm ID3. +

    + +

    In the example here we have converted all our attributes into numerical values \( 0,1,2 \) etc.

    + + + +
    +
    +
    +
    +
    +
    # Split a dataset based on an attribute and an attribute value
    +def test_split(index, value, dataset):
    +	left, right = list(), list()
    +	for row in dataset:
    +		if row[index] < value:
    +			left.append(row)
    +		else:
    +			right.append(row)
    +	return left, right
    + 
    +# Calculate the Gini index for a split dataset
    +def gini_index(groups, classes):
    +	# count all samples at split point
    +	n_instances = float(sum([len(group) for group in groups]))
    +	# sum weighted Gini index for each group
    +	gini = 0.0
    +	for group in groups:
    +		size = float(len(group))
    +		# avoid divide by zero
    +		if size == 0:
    +			continue
    +		score = 0.0
    +		# score the group based on the score for each class
    +		for class_val in classes:
    +			p = [row[-1] for row in group].count(class_val) / size
    +			score += p * p
    +		# weight the group score by its relative size
    +		gini += (1.0 - score) * (size / n_instances)
    +	return gini
    +
    +# Select the best split point for a dataset
    +def get_split(dataset):
    +	class_values = list(set(row[-1] for row in dataset))
    +	b_index, b_value, b_score, b_groups = 999, 999, 999, None
    +	for index in range(len(dataset[0])-1):
    +		for row in dataset:
    +			groups = test_split(index, row[index], dataset)
    +			gini = gini_index(groups, class_values)
    +			print('X%d < %.3f Gini=%.3f' % ((index+1), row[index], gini))
    +			if gini < b_score:
    +				b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    +	return {'index':b_index, 'value':b_value, 'groups':b_groups}
    + 
    +dataset = [[0,0,0,0,0],
    +            [0,0,0,1,1],
    +            [1,0,0,0,1],
    +            [2,1,0,0,1],
    +            [2,2,1,0,1],
    +            [2,2,1,1,0],
    +            [1,2,1,1,1],
    +            [0,1,0,0,0],
    +            [0,2,1,0,1],
    +            [2,1,1,0,1],
    +            [0,1,1,1,1],
    +            [1,1,0,1,1],
    +            [1,0,1,0,1],
    +            [2,1,0,1,0]]
    +
    +split = get_split(dataset)
    +print('Split: [X%d < %.3f]' % ((split['index']+1), split['value']))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -402,7 +455,7 @@ MathJax.Hub.Config({

  • 34
  • 35
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs026.html b/doc/pub/week47/html/._week47-bs026.html index 5071e3f77..2a73990e8 100644 --- a/doc/pub/week47/html/._week47-bs026.html +++ b/doc/pub/week47/html/._week47-bs026.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,12 +337,58 @@ MathJax.Hub.Config({

     

     

     

    -

    What? Me worry? No final exam in this course!

    -

    -
    -

    -
    -

    +

    Regression trees

    + + +
    +
    +
    +
    +
    +
    # Quadratic training set + noise
    +np.random.seed(42)
    +m = 200
    +X = np.random.rand(m, 1)
    +y = 4 * (X - 0.5) ** 2
    +y = y + np.random.randn(m, 1) / 10
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42)
    +tree_reg.fit(X, y)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -407,7 +415,7 @@ MathJax.Hub.Config({

  • 35
  • 36
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs027.html b/doc/pub/week47/html/._week47-bs027.html index 1a2bf03a4..547da5cde 100644 --- a/doc/pub/week47/html/._week47-bs027.html +++ b/doc/pub/week47/html/._week47-bs027.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,14 +337,114 @@ MathJax.Hub.Config({

     

     

     

    - +

    Final regressor code

    -

    Artificial intelligence is built upon integrated machine learning -algorithms as discussed in this course, which in turn are fundamentally rooted in optimization and -statistical learning. -

    + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg1 = DecisionTreeRegressor(random_state=42, max_depth=2)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, max_depth=3)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +def plot_regression_predictions(tree_reg, X, y, axes=[0, 1, -0.2, 1], ylabel="$y$"):
    +    x1 = np.linspace(axes[0], axes[1], 500).reshape(-1, 1)
    +    y_pred = tree_reg.predict(x1)
    +    plt.axis(axes)
    +    plt.xlabel("$x_1$", fontsize=18)
    +    if ylabel:
    +        plt.ylabel(ylabel, fontsize=18, rotation=0)
    +    plt.plot(X, y, "b.")
    +    plt.plot(x1, y_pred, "r.-", linewidth=2, label=r"$\hat{y}$")
    +
    +plt.figure(figsize=(11, 4))
    +plt.subplot(121)
    +plot_regression_predictions(tree_reg1, X, y)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +plt.text(0.21, 0.65, "Depth=0", fontsize=15)
    +plt.text(0.01, 0.2, "Depth=1", fontsize=13)
    +plt.text(0.65, 0.8, "Depth=1", fontsize=13)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("max_depth=2", fontsize=14)
    +
    +plt.subplot(122)
    +plot_regression_predictions(tree_reg2, X, y, ylabel=None)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +for split in (0.0458, 0.1298, 0.2873, 0.9040):
    +    plt.plot([split, split], [-0.2, 1], "k:", linewidth=1)
    +plt.text(0.3, 0.5, "Depth=2", fontsize=13)
    +plt.title("max_depth=3", fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    tree_reg1 = DecisionTreeRegressor(random_state=42)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +x1 = np.linspace(0, 1, 500).reshape(-1, 1)
    +y_pred1 = tree_reg1.predict(x1)
    +y_pred2 = tree_reg2.predict(x1)
    +
    +plt.figure(figsize=(11, 4))
    +
    +plt.subplot(121)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred1, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.ylabel("$y$", fontsize=18, rotation=0)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("No restrictions", fontsize=14)
    +
    +plt.subplot(122)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred2, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.title("min_samples_leaf={}".format(tree_reg2.min_samples_leaf), fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    Can we have Artificial Intelligence without Machine Learning? See this post for inspiration.

    @@ -409,7 +471,7 @@ statistical learning.

  • 36
  • 37
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs028.html b/doc/pub/week47/html/._week47-bs028.html index 52863da6e..74964b36e 100644 --- a/doc/pub/week47/html/._week47-bs028.html +++ b/doc/pub/week47/html/._week47-bs028.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,25 +337,17 @@ MathJax.Hub.Config({

     

     

     

    -

    Going back to the beginning of the semester

    - -

    Traditionally the field of machine learning has had its main focus on -predictions and correlations. These concepts outline in some sense -the difference between machine learning and what is normally called -Bayesian statistics or Bayesian inference. -

    - -

    In machine learning and prediction based tasks, we are often -interested in developing algorithms that are capable of learning -patterns from given data in an automated fashion, and then using these -learned patterns to make predictions or assessments of newly given -data. In many cases, our primary concern is the quality of the -predictions or assessments, and we are less concerned with the -underlying patterns that were learned in order to make these -predictions. This leads to what normally has been labeled as a -frequentist approach. -

    +

    Pros and cons of trees, pros

    +
      +
    • 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)
    • +
    • Trees are very easy to explain to people. In fact, they are even easier to explain than linear regression!
    • +
    • No feature normalization needed
    • +
    • Tree models can handle both continuous and categorical data (Classification and Regression Trees)
    • +
    • Can model nonlinear relationships
    • +
    • Can model interactions between the different descriptive features
    • +
    • Trees can be displayed graphically, and are easily interpreted even by a non-expert (especially if they are small)
    • +

      @@ -419,7 +373,7 @@ frequentist approach.
    • 37
    • 38
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs029.html b/doc/pub/week47/html/._week47-bs029.html index 9fc7b0e71..e163c2e9f 100644 --- a/doc/pub/week47/html/._week47-bs029.html +++ b/doc/pub/week47/html/._week47-bs029.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,27 +337,20 @@ MathJax.Hub.Config({

     

     

     

    -

    Not so sharp distinctions

    +

    Disadvantages

    -

    You should keep in mind that the division between a traditional -frequentist approach with focus on predictions and correlations only -and a Bayesian approach with an emphasis on estimations and -causations, is not that sharp. Machine learning can be frequentist -with ensemble methods (EMB) as examples and Bayesian with Gaussian -Processes as examples. -

    - -

    If one views ML from a statistical learning -perspective, one is then equally interested in estimating errors as -one is in finding correlations and making predictions. It is important -to keep in mind that the frequentist and Bayesian approaches differ -mainly in their interpretations of probability. In the frequentist -world, we can only assign probabilities to repeated random -phenomena. From the observations of these phenomena, we can infer the -probability of occurrence of a specific event. In Bayesian -statistics, we assign probabilities to specific events and the -probability represents the measure of belief/confidence for that -event. The belief can be updated in the light of new evidence. +

      +
    • Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches
    • +
    • If continuous features are used the tree may become quite large and hence less interpretable
    • +
    • 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
    • +
    • 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
    • +
    • 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.
    • +
    • If the number of features is relatively large (high dimensional) and the number of instances is relatively low, the tree might overfit the data
    • +
    • 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
    • +
    +

    However, by aggregating many decision trees, using methods like +bagging, random forests, and boosting, the predictive performance of +trees can be substantially improved.

    @@ -423,7 +378,7 @@ event. The belief can be updated in the light of new evidence.

  • 38
  • 39
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs030.html b/doc/pub/week47/html/._week47-bs030.html index e3fbe6cc5..154390828 100644 --- a/doc/pub/week47/html/._week47-bs030.html +++ b/doc/pub/week47/html/._week47-bs030.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,14 +337,29 @@ MathJax.Hub.Config({

     

     

     

    -

    Topics we have covered this year

    +

    Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods

    -

    The course has two central parts

    +

    As stated above and seen in many of the examples discussed here about +a single decision tree, we often end up overfitting our training +data. This normally means that we have a high variance. Can we reduce +the variance of a statistical learning method? +

    + +

    This leads us to a set of different methods that can combine different +machine learning algorithms or just use one of them to construct +forests and jungles of trees, homogeneous ones or heterogenous +ones. These methods are recognized by different names which we will +try to explain here. These are +

      -
    1. Statistical analysis and optimization of data
    2. -
    3. Machine learning
    4. +
    5. Voting classifiers
    6. +
    7. Bagging and Pasting
    8. +
    9. Random forests
    10. +
    11. Boosting methods, from adaptive to Extreme Gradient Boosting (XGBoost)
    +

    We discuss these methods here.

    +

      @@ -408,7 +385,7 @@ MathJax.Hub.Config({
    • 39
    • 40
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs031.html b/doc/pub/week47/html/._week47-bs031.html index ab2ac0053..64e749897 100644 --- a/doc/pub/week47/html/._week47-bs031.html +++ b/doc/pub/week47/html/._week47-bs031.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,18 +337,14 @@ MathJax.Hub.Config({

     

     

     

    -

    Statistical analysis and optimization of data

    +

    An Overview of Ensemble Methods

    + +

    +
    +

    +
    +

    -

    The following topics have been discussed:

    -
      -
    1. Basic concepts, expectation values, variance, covariance, correlation functions and errors;
    2. -
    3. Simpler models, binomial distribution, the Poisson distribution, simple and multivariate normal distributions;
    4. -
    5. Central elements from linear algebra, matrix inversion and SVD
    6. -
    7. Gradient methods for data optimization
    8. -
    9. Estimation of errors using cross-validation, bootstrapping and jackknife methods;
    10. -
    11. Practical optimization using Singular-value decomposition and least squares for parameterizing data.
    12. -
    13. Principal Component Analysis to reduce the number of features.
    14. -

      @@ -412,7 +370,7 @@ MathJax.Hub.Config({
    • 40
    • 41
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs032.html b/doc/pub/week47/html/._week47-bs032.html index 380cebb2a..1ce0b2588 100644 --- a/doc/pub/week47/html/._week47-bs032.html +++ b/doc/pub/week47/html/._week47-bs032.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,37 +337,25 @@ MathJax.Hub.Config({

     

     

     

    -

    Machine learning

    +

    Why Voting?

    + +

    The idea behind boosting, and voting as well can be phrased as follows: +Can a group of people somehow arrive at highly +reasoned decisions, despite the weak judgement of the individual +members? +

    + +

    The aim is to create a good classifier by combining several weak classifiers. +A weak classifier is a classifier which is able to produce results that are only slightly better than guessing at random. +

    + +

    The basic approach is to apply repeatedly (in boosting this is done in an iterative way) a weak classifier to modifications of the data. +In voting we simply apply the law of large numbers while in boosting we give more weight to misclassified data in +each iteration. +

    + +

    Decision trees play an important role as our weak classifier. They serve as the basic method.

    -

    The following topics will be covered

    -
      -
    1. Linear methods for regression and classification: -
        -
      1. Ordinary Least Squares
      2. -
      3. Ridge regression
      4. -
      5. Lasso regression
      6. -
      7. Logistic regression
      8. -
      -
    2. Neural networks and deep learning: -
        -
      1. Feed Forward Neural Networks
      2. -
      3. Convolutional Neural Networks
      4. -
      5. Recurrent Neural Networks
      6. -
      -
    3. Decisions trees and ensemble methods: -
        -
      1. Decision trees
      2. -
      3. Bagging and voting
      4. -
      5. Random forests
      6. -
      7. Boosting and gradient boosting
      8. -
      -
    4. Support vector machines, not covered this year but included in notes -
        -
      1. Binary classification and multiclass classification
      2. -
      3. Kernel methods
      4. -
      5. Regression
      6. -
      -

      @@ -431,7 +381,7 @@ MathJax.Hub.Config({
    • 41
    • 42
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs033.html b/doc/pub/week47/html/._week47-bs033.html index 0263c8d27..58cd20d1e 100644 --- a/doc/pub/week47/html/._week47-bs033.html +++ b/doc/pub/week47/html/._week47-bs033.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,29 +337,33 @@ MathJax.Hub.Config({

     

     

     

    -

    Learning outcomes and overarching aims of this course

    +

    Tossing coins

    -

    The course introduces a variety of central algorithms and methods -essential for studies of data analysis and machine learning. The -course is project based and through the various projects, normally -three, you will be exposed to fundamental research problems -in these fields, with the aim to reproduce state of the art scientific -results. The students will learn to develop and structure large codes -for studying these systems, get acquainted with computing facilities -and learn to handle large scientific projects. A good scientific and -ethical conduct is emphasized throughout the course. +

    The simplest case is a so-called voting ensemble. To illustrate this, +think of yourself tossing coins with a biased outcome of 51 per cent +for heads and 49% for tails. With only few tosses, +you may not clearly see this distribution for heads and tails. However, after some +thousands of tosses, there will be a clear majority of heads. With 2000 tosses +you should see approximately 1020 heads and 980 tails. +

    + +

    We can then state that the outcome is a clear majority of heads. If +you do this ten thousand times, it is easy to see that there is a 97% +likelihood of a majority of heads. +

    + +

    Another example would be to collect all polls before an +election. Different polls may show different likelihoods for a +candidate winning with say a majority of the popular vote. The majority vote +would then consist in many polls indicating that this candidate will +actually win. +

    + +

    The example here shows how we can implement the coin tossing case, +clealry demostrating that after some tosses we see the law of large +numbers kicking in.

    -
      -
    • Understand linear methods for regression and classification;
    • -
    • Learn about neural network;
    • -
    • Learn about bagging, boosting and trees
    • -
    • Support vector machines, not covered
    • -
    • Learn about basic data analysis;
    • -
    • Be capable of extending the acquired knowledge to other systems and cases;
    • -
    • Have an understanding of central algorithms used in data analysis and machine learning;
    • -
    • Work on numerical projects to illustrate the theory. The projects play a central role and you are expected to know modern programming languages like Python or C++.
    • -

      @@ -423,7 +389,7 @@ ethical conduct is emphasized throughout the course.
    • 42
    • 43
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs034.html b/doc/pub/week47/html/._week47-bs034.html index 2318ceee2..65f6684ee 100644 --- a/doc/pub/week47/html/._week47-bs034.html +++ b/doc/pub/week47/html/._week47-bs034.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,19 +337,67 @@ MathJax.Hub.Config({

     

     

     

    -

    Perspective on Machine Learning

    +

    Standard imports first

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -
      -
    1. Rapidly emerging application area
    2. -
    3. Experiment AND theory are evolving in many many fields. Still many low-hanging fruits.
    4. -
    5. Requires education/retraining for more widespread adoption
    6. -
    7. A lot of “word-of-mouth” development methods
    8. -
    -

    Huge amounts of data sets require automation, classical analysis tools often inadequate. -High energy physics hit this wall in the 90’s. -In 2009 single top quark production was determined via Boosted decision trees, Bayesian -Neural Networks, etc.. Similarly, the search for Higgs was a statistical learning tour de force. See this link on Kaggle.com. -

    @@ -414,7 +424,7 @@ Neural Networks, etc.. Similarly, the search for Higgs was a statistical lea

  • 43
  • 44
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs035.html b/doc/pub/week47/html/._week47-bs035.html index d0f2e0aa5..3756133e1 100644 --- a/doc/pub/week47/html/._week47-bs035.html +++ b/doc/pub/week47/html/._week47-bs035.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,17 +337,52 @@ MathJax.Hub.Config({

     

     

     

    -

    Machine Learning Research

    +

    Simple Voting Example, head or tail

    + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import matplotlib
    +import matplotlib.pyplot as plt
    +from matplotlib.colors import ListedColormap
    +plt.rcParams['axes.labelsize'] = 14
    +plt.rcParams['xtick.labelsize'] = 12
    +plt.rcParams['ytick.labelsize'] = 12
    +
    +heads_proba = 0.51
    +coin_tosses = (np.random.rand(10000, 10) < heads_proba).astype(np.int32)
    +cumulative_heads_ratio = np.cumsum(coin_tosses, axis=0) / np.arange(1, 10001).reshape(-1, 1)
    +plt.figure(figsize=(8,3.5))
    +plt.plot(cumulative_heads_ratio)
    +plt.plot([0, 10000], [0.51, 0.51], "k--", linewidth=2, label="51%")
    +plt.plot([0, 10000], [0.5, 0.5], "k-", label="50%")
    +plt.xlabel("Number of coin tosses")
    +plt.ylabel("Heads ratio")
    +plt.legend(loc="lower right")
    +plt.axis([0, 10000, 0.42, 0.58])
    +save_fig("votingsimple")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + -

    Where to find recent results:

    -
      -
    1. Conference proceedings, arXiv and blog posts!
    2. -
    3. NIPS: Neural Information Processing Systems
    4. -
    5. ICLR: International Conference on Learning Representations
    6. -
    7. ICML: International Conference on Machine Learning
    8. -
    9. Journal of Machine Learning Research
    10. -
    11. Follow ML on ArXiv
    12. -

      @@ -411,7 +408,7 @@ MathJax.Hub.Config({
    • 44
    • 45
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs036.html b/doc/pub/week47/html/._week47-bs036.html index 412e30699..261f7cd15 100644 --- a/doc/pub/week47/html/._week47-bs036.html +++ b/doc/pub/week47/html/._week47-bs036.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,15 +337,74 @@ MathJax.Hub.Config({

     

     

     

    -

    Starting your Machine Learning Project

    +

    Using the Voting Classifier

    + +

    We can use the voting classifier on other data sets, here the exciting binary case of two distinct objects using the make moons functionality of Scikit-Learn.

    + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", probability=True, random_state=42)
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + -
      -
    1. Identify problem type: classification, regression
    2. -
    3. Consider your data carefully
    4. -
    5. Choose a simple model that fits 1. and 2.
    6. -
    7. Consider your data carefully again! Think of data representation more carefully.
    8. -
    9. Based on your results, feedback loop to earliest possible point
    10. -

      @@ -409,7 +430,7 @@ MathJax.Hub.Config({
    • 45
    • 46
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs037.html b/doc/pub/week47/html/._week47-bs037.html index 2353b722b..44c543f55 100644 --- a/doc/pub/week47/html/._week47-bs037.html +++ b/doc/pub/week47/html/._week47-bs037.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,13 +337,126 @@ MathJax.Hub.Config({

     

     

     

    -

    Choose a Model and Algorithm

    +

    Voting and Bagging

    + + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(probability=True, random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + -
      -
    1. Supervised?
    2. -
    3. Start with the simplest model that fits your problem
    4. -
    5. Start with minimal processing of data
    6. -

      @@ -407,7 +482,7 @@ MathJax.Hub.Config({
    • 46
    • 47
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs038.html b/doc/pub/week47/html/._week47-bs038.html index 347f471d4..c0e215934 100644 --- a/doc/pub/week47/html/._week47-bs038.html +++ b/doc/pub/week47/html/._week47-bs038.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,28 +337,20 @@ MathJax.Hub.Config({

     

     

     

    -

    Preparing Your Data

    +

    Bagging

    -
      -
    1. Shuffle your data
    2. -
    3. Mean center your data
    4. -
        -
      • Why?
      • -
      -
    5. Normalize the variance
    6. -
        -
      • Why?
      • -
      -
    7. Whitening
    8. -
        -
      • Decorrelates data
      • -
      • Can be hit or miss
      • -
      -
    9. When to do train/test split?
    10. -
    -

    Whitening is a decorrelation transformation that transforms a set of -random variables into a set of new random variables with identity -covariance (uncorrelated with unit variances). +

    The plain decision trees suffer from high +variance. This means that if we split the training data into two parts +at random, and fit a decision tree to both halves, the results that we +get could be quite different. In contrast, a procedure with low +variance will yield similar results if applied repeatedly to distinct +data sets; linear regression tends to have low variance, if the ratio +of \( n \) to \( p \) is moderately large. +

    + +

    Bootstrap aggregation, or just bagging, is a +general-purpose procedure for reducing the variance of a statistical +learning method.

    @@ -424,7 +378,7 @@ covariance (uncorrelated with unit variances).

  • 47
  • 48
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs039.html b/doc/pub/week47/html/._week47-bs039.html index 5d3884096..d778d1647 100644 --- a/doc/pub/week47/html/._week47-bs039.html +++ b/doc/pub/week47/html/._week47-bs039.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,20 +337,32 @@ MathJax.Hub.Config({

     

     

     

    -

    Which Activation and Weights to Choose in Neural Networks

    +

    More bagging

    + +

    Bagging typically results in improved accuracy +over prediction using a single tree. Unfortunately, however, it can be +difficult to interpret the resulting model. Recall that one of the +advantages of decision trees is the attractive and easily interpreted +diagram that results. +

    + +

    However, when we bag a large number of trees, it is no longer +possible to represent the resulting statistical learning procedure +using a single tree, and it is no longer clear which variables are +most important to the procedure. Thus, bagging improves prediction +accuracy at the expense of interpretability. Although the collection +of bagged trees is much more difficult to interpret than a single +tree, one can obtain an overall summary of the importance of each +predictor using the MSE (for bagging regression trees) or the Gini +index (for bagging classification trees). In the case of bagging +regression trees, we can record the total amount that the MSE is +decreased due to splits over a given predictor, averaged over all \( B \) possible +trees. A large value indicates an important predictor. Similarly, in +the context of bagging classification trees, we can add up the total +amount that the Gini index is decreased by splits over a given +predictor, averaged over all \( B \) trees. +

    -
      -
    1. RELU? ELU?
    2. -
    3. Sigmoid or Tanh?
    4. -
    5. Set all weights to 0?
    6. -
        -
      • Terrible idea
      • -
      -
    7. Set all weights to random values?
    8. -
        -
      • Small random values
      • -
      -

      @@ -414,7 +388,7 @@ MathJax.Hub.Config({
    • 48
    • 49
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs040.html b/doc/pub/week47/html/._week47-bs040.html index 36a3b2ef8..50fe5c9e3 100644 --- a/doc/pub/week47/html/._week47-bs040.html +++ b/doc/pub/week47/html/._week47-bs040.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,24 +337,91 @@ MathJax.Hub.Config({

     

     

     

    -

    Optimization Methods and Hyperparameters

    -
      -
    1. Stochastic gradient descent -
        -
      1. Stochastic gradient descent + momentum
      2. -
      -
    2. State-of-the-art approaches:
    3. -
        -
      • RMSProp
      • -
      • Adam
      • -
      • and more
      • -
      -
    -

    Which regularization and hyperparameters? \( L_1 \) or \( L_2 \), soft -classifiers, depths of trees and many other. Need to explore a large -set of hyperparameters and regularization methods. +

    Making your own Bootstrap: Changing the Level of the Decision Tree

    + +

    Let us bring up our good old boostrap example from the linear regression lectures. We change the linerar regression algorithm with +a decision tree wth different depths and perform a bootstrap aggregate (in this case we perform as many bootstraps as data points \( n \)).

    + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import train_test_split
    +from sklearn.pipeline import make_pipeline
    +from sklearn.utils import resample
    +from sklearn.tree import DecisionTreeRegressor
    +
    +n = 100
    +n_boostraps = 100
    +maxdepth = 8
    +
    +# Make data set.
    +x = np.linspace(-3, 3, n).reshape(-1, 1)
    +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
    +error = np.zeros(maxdepth)
    +bias = np.zeros(maxdepth)
    +variance = np.zeros(maxdepth)
    +polydegree = np.zeros(maxdepth)
    +X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    +
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +# we produce a simple tree first as benchmark
    +simpletree = DecisionTreeRegressor(max_depth=3) 
    +simpletree.fit(X_train_scaled, y_train)
    +simpleprediction = simpletree.predict(X_test_scaled)
    +for degree in range(1,maxdepth):
    +    model = DecisionTreeRegressor(max_depth=degree) 
    +    y_pred = np.empty((y_test.shape[0], n_boostraps))
    +    for i in range(n_boostraps):
    +        x_, y_ = resample(X_train_scaled, y_train)
    +        model.fit(x_, y_)
    +        y_pred[:, i] = model.predict(X_test_scaled)#.ravel()
    +
    +    polydegree[degree] = degree
    +    error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
    +    bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
    +    variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
    +    print('Polynomial degree:', degree)
    +    print('Error:', error[degree])
    +    print('Bias^2:', bias[degree])
    +    print('Var:', variance[degree])
    +    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
    + 
    +mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**2))
    +print("Simple tree:",mse_simpletree)
    +plt.xlim(1,maxdepth)
    +plt.plot(polydegree, error, label='MSE')
    +plt.plot(polydegree, bias, label='bias')
    +plt.plot(polydegree, variance, label='Variance')
    +plt.legend()
    +save_fig("baggingboot")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

      @@ -418,7 +447,7 @@ set of hyperparameters and regularization methods.
    • 49
    • 50
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs041.html b/doc/pub/week47/html/._week47-bs041.html index 0e098875d..0ff4de94b 100644 --- a/doc/pub/week47/html/._week47-bs041.html +++ b/doc/pub/week47/html/._week47-bs041.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,15 +337,47 @@ MathJax.Hub.Config({

     

     

     

    -

    Resampling

    +

    Random forests

    -

    When do we resample?

    +

    Random forests provide an improvement over bagged trees by way of a +small tweak that decorrelates the trees. +

    + +

    As in bagging, we build a +number of decision trees on bootstrapped training samples. But when +building these decision trees, each time a split in a tree is +considered, a random sample of \( m \) predictors is chosen as split +candidates from the full set of \( p \) predictors. The split is allowed to +use only one of those \( m \) predictors. +

    + +

    A fresh sample of \( m \) predictors is +taken at each split, and typically we choose +

    + +$$ +m\approx \sqrt{p}. +$$ + +

    In building a random forest, at +each split in the tree, the algorithm is not even allowed to consider +a majority of the available predictors. +

    + +

    The reason for this is rather clever. Suppose that there is one very +strong predictor in the data set, along with a number of other +moderately strong predictors. Then in the collection of bagged +variable importance random forest trees, most or all of the trees will +use this strong predictor in the top split. Consequently, all of the +bagged trees will look quite similar to each other. Hence the +predictions from the bagged trees will be highly correlated. +Unfortunately, averaging many highly correlated quantities does not +lead to as large of a reduction in variance as averaging many +uncorrelated quantities. In particular, this means that bagging will +not lead to a substantial reduction in variance over a single tree in +this setting. +

    -
      -
    1. Bootstrap
    2. -
    3. Cross-validation
    4. -
    5. Jackknife and many other
    6. -

      @@ -409,7 +403,7 @@ MathJax.Hub.Config({
    • 50
    • 51
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs042.html b/doc/pub/week47/html/._week47-bs042.html index d6b3251c5..daed092fa 100644 --- a/doc/pub/week47/html/._week47-bs042.html +++ b/doc/pub/week47/html/._week47-bs042.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,18 +337,22 @@ MathJax.Hub.Config({

     

     

     

    -

    Other courses on Data science and Machine Learning at UiO

    +

    Random Forest Algorithm

    +

    The algorithm described here can be applied to both classification and regression problems.

    +

    We will grow of forest of say \( B \) trees.

      -
    1. FYS5429 Advanced Machine Learning and Data Analysis for the Physical Sciences. Discussed deep learning and generative deep learning.
    2. -
    3. FYS5419 Quantum Computing and Quantum Machine Learning
    4. -
    5. STK2100 Machine learning and statistical methods for prediction and classification.
    6. -
    7. IN3050/IN4050 Introduction to Artificial Intelligence and Machine Learning. Introductory course in machine learning and AI with an algorithmic approach.
    8. -
    9. STK-INF3000/4000 Selected Topics in Data Science. The course provides insight into selected contemporary relevant topics within Data Science.
    10. -
    11. IN4080 Natural Language Processing. Probabilistic and machine learning techniques applied to natural language processing. o STK-IN4300 – Statistical learning methods in Data Science. An advanced introduction to statistical and machine learning. For students with a good mathematics and statistics background.
    12. -
    13. IN-STK5000 Adaptive Methods for Data-Based Decision Making. Methods for adaptive collection and processing of data based on machine learning techniques.
    14. -
    15. IN5400/INF5860 – Machine Learning for Image Analysis. An introduction to deep learning with particular emphasis on applications within Image analysis, but useful for other application areas too.
    16. -
    17. TEK5040 – Dyp læring for autonome systemer. The course addresses advanced algorithms and architectures for deep learning with neural networks. The course provides an introduction to how deep-learning techniques can be used in the construction of key parts of advanced autonomous systems that exist in physical environments and cyber environments.
    18. +
    19. For \( b=1:B \)
    20. +
        +
      • Draw a bootstrap sample from the training data organized in our \( \boldsymbol{X} \) matrix.
      • +
      • We grow then a random forest tree \( T_b \) based on the bootstrapped data by repeating the steps outlined till we reach the maximum node size is reached
      • +
          +
        1. we select \( m \le p \) variables at random from the \( p \) predictors/features
        2. +
        3. pick the best split point among the \( m \) features using for example the CART algorithm and create a new node
        4. +
        5. split the node into daughter nodes
        6. +
        +
      +
    21. Output then the ensemble of trees \( \{T_b\}_1^{B} \) and make predictions for either a regression type of problem or a classification type of problem.

    @@ -413,7 +379,7 @@ MathJax.Hub.Config({

  • 51
  • 52
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs043.html b/doc/pub/week47/html/._week47-bs043.html index b4952cc79..18989201c 100644 --- a/doc/pub/week47/html/._week47-bs043.html +++ b/doc/pub/week47/html/._week47-bs043.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,12 +337,100 @@ MathJax.Hub.Config({

     

     

     

    -

    Additional courses of interest

    +

    Random Forests Compared with other Methods on the Cancer Data

    + + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.svm import SVC
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.ensemble import BaggingClassifier
    +
    +# Load the data
    +cancer = load_breast_cancer()
    +
    +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    +print(X_train.shape)
    +print(X_test.shape)
    +#define methods
    +# Logistic Regression
    +logreg = LogisticRegression(solver='lbfgs')
    +# Support vector machine
    +svm = SVC(gamma='auto', C=100)
    +# Decision Trees
    +deep_tree_clf = DecisionTreeClassifier(max_depth=None)
    +#Scale the data
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +# Logistic Regression
    +logreg.fit(X_train_scaled, y_train)
    +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
    +# Support Vector Machine
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy SVM with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
    +# Decision Trees
    +deep_tree_clf.fit(X_train_scaled, y_train)
    +print("Test set accuracy with Decision Trees and scaled data: {:.2f}".format(deep_tree_clf.score(X_test_scaled,y_test)))
    +
    +
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.preprocessing import LabelEncoder
    +from sklearn.model_selection import cross_validate
    +# Data set not specificied
    +#Instantiate the model with 500 trees and entropy as splitting criteria
    +Random_Forest_model = RandomForestClassifier(n_estimators=500,criterion="entropy")
    +Random_Forest_model.fit(X_train_scaled, y_train)
    +#Cross validation
    +accuracy = cross_validate(Random_Forest_model,X_test_scaled,y_test,cv=10)['test_score']
    +print(accuracy)
    +print("Test set accuracy with Random Forests and scaled data: {:.2f}".format(Random_Forest_model.score(X_test_scaled,y_test)))
    +
    +
    +import scikitplot as skplt
    +y_pred = Random_Forest_model.predict(X_test_scaled)
    +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
    +plt.show()
    +y_probas = Random_Forest_model.predict_proba(X_test_scaled)
    +skplt.metrics.plot_roc(y_test, y_probas)
    +plt.show()
    +skplt.metrics.plot_cumulative_gain(y_test, y_probas)
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Recall that the cumulative gains curve shows the percentage of the +overall number of cases in a given category gained by targeting a +percentage of the total number of cases. +

    + +

    Similarly, the receiver operating characteristic curve, or ROC curve, +displays the diagnostic ability of a binary classifier system as its +discrimination threshold is varied. It plots the true positive rate against the false positive rate. +

    -
      -
    1. STK4051 Computational Statistics
    2. -
    3. STK4021 Applied Bayesian Analysis and Numerical Methods
    4. -

      @@ -406,7 +456,7 @@ MathJax.Hub.Config({
    • 52
    • 53
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs044.html b/doc/pub/week47/html/._week47-bs044.html index fa13f30c0..32a1c8ef4 100644 --- a/doc/pub/week47/html/._week47-bs044.html +++ b/doc/pub/week47/html/._week47-bs044.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,26 +337,59 @@ MathJax.Hub.Config({

     

     

     

    -

    What's the future like?

    +

    Compare Bagging on Trees with Random Forests

    + + +
    +
    +
    +
    +
    +
    bag_clf = BaggingClassifier(
    +    DecisionTreeClassifier(splitter="random", max_leaf_nodes=16, random_state=42),
    +    n_estimators=500, max_samples=1.0, bootstrap=True, n_jobs=-1, random_state=42)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    bag_clf.fit(X_train, y_train)
    +y_pred = bag_clf.predict(X_test)
    +from sklearn.ensemble import RandomForestClassifier
    +rnd_clf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=16, n_jobs=-1, random_state=42)
    +rnd_clf.fit(X_train, y_train)
    +y_pred_rf = rnd_clf.predict(X_test)
    +np.sum(y_pred == y_pred_rf) / len(y_pred) 
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    Based on multi-layer nonlinear neural networks, deep learning can -learn directly from raw data, automatically extract and abstract -features from layer to layer, and then achieve the goal of regression, -classification, or ranking. Deep learning has made breakthroughs in -computer vision, speech processing and natural language, and reached -or even surpassed human level. The success of deep learning is mainly -due to the three factors: big data, big model, and big computing. -

    -

    In the past few decades, many different architectures of deep neural -networks have been proposed, such as -

    -
      -
    1. Convolutional neural networks, which are mostly used in image and video data processing, and have also been applied to sequential data such as text processing;
    2. -
    3. Recurrent neural networks, which can process sequential data of variable length and have been widely used in natural language understanding and speech processing;
    4. -
    5. Encoder-decoder framework, which is mostly used for image or sequence generation, such as machine translation, text summarization, and image captioning.
    6. -
    7. Generative deep learning! Recent textbook by David Foster (and obviously many other ones) at https://www.oreilly.com/library/view/generative-deep-learning/9781492041931/"
    8. -

      @@ -420,7 +415,7 @@ networks have been proposed, such as
    • 53
    • 54
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs045.html b/doc/pub/week47/html/._week47-bs045.html index 731d51f8f..ebfacbdfc 100644 --- a/doc/pub/week47/html/._week47-bs045.html +++ b/doc/pub/week47/html/._week47-bs045.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,34 +337,20 @@ MathJax.Hub.Config({

     

     

     

    -

    Types of Machine Learning, a repetition

    +

    Boosting, a Bird's Eye View

    -
    -
    - -

    The approaches to machine learning are many, but are often split into two main categories. -In supervised learning we know the answer to a problem, -and let the computer deduce the logic behind it. On the other hand, unsupervised learning -is a method for finding patterns and relationship in data sets without any prior knowledge of the system. -Some authours also operate with a third category, namely reinforcement learning. This is a paradigm -of learning inspired by behavioural psychology, where learning is achieved by trial-and-error, -solely from rewards and punishment. +

    The basic idea is to combine weak classifiers in order to create a good +classifier. With a weak classifier we often intend a classifier which +produces results which are only slightly better than we would get by +random guesses.

    -

    Another way to categorize machine learning tasks is to consider the desired output of a system. -Some of the most common tasks are: +

    This is done by applying in an iterative way a weak (or a standard +classifier like decision trees) to modify the data. In each iteration +we emphasize those observations which are misclassified by weighting +them with a factor.

    -
      -
    • Classification: Outputs are divided into two or more classes. The goal is to produce a model that assigns inputs into one of these classes. An example is to identify digits based on pictures of hand-written ones. Classification is typically supervised learning.
    • -
    • Regression: Finding a functional relationship between an input data set and a reference data set. The goal is to construct a function that maps input data to continuous output values.
    • -
    • Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning.
    • -
    • Other unsupervised learning algortihms like Boltzmann machines
    • -
    -
    -
    - -

      @@ -428,7 +376,7 @@ Some of the most common tasks are:
    • 54
    • 55
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs046.html b/doc/pub/week47/html/._week47-bs046.html index 3b6ca98b0..cdbce2069 100644 --- a/doc/pub/week47/html/._week47-bs046.html +++ b/doc/pub/week47/html/._week47-bs046.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,15 +337,52 @@ MathJax.Hub.Config({

     

     

     

    -

    Why Boltzmann machines?

    +

    What is boosting? Additive Modelling/Iterative Fitting

    -

    What is known as restricted Boltzmann Machines (RMB) have received a lot of attention lately. -One of the major reasons is that they can be stacked layer-wise to build deep neural networks that capture complicated statistics. +

    Boosting is a way of fitting an additive expansion in a set of +elementary basis functions like for example some simple polynomials. +Assume for example that we have a function +

    +$$ +f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m), +$$ + +

    where \( \beta_m \) are the expansion parameters to be determined in a +minimization process and \( b(x;\gamma_m) \) are some simple functions of +the multivariable parameter \( x \) which is characterized by the +parameters \( \gamma_m \).

    -

    The original RBMs had just one visible layer and a hidden layer, but recently so-called Gaussian-binary RBMs have gained quite some popularity in imaging since they are capable of modeling continuous data that are common to natural images.

    +

    As an example, consider the Sigmoid function we used in logistic +regression. In that case, we can translate the function +\( b(x;\gamma_m) \) into the Sigmoid function +

    -

    Furthermore, they have been used to solve complicated quantum mechanical many-particle problems or classical statistical physics problems like the Ising and Potts classes of models.

    +$$ +\sigma(t) = \frac{1}{1+\exp{(-t)}}, +$$ + +

    where \( t=\gamma_0+\gamma_1 x \) and the parameters \( \gamma_0 \) and +\( \gamma_1 \) were determined by the Logistic Regression fitting +algorithm. +

    + +

    As another example, consider the cost function we defined for linear regression

    +$$ +C(\boldsymbol{y},\boldsymbol{f}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-f(x_i))^2. +$$ + +

    In this case the function \( f(x) \) was replaced by the design matrix +\( \boldsymbol{X} \) and the unknown linear regression parameters \( \boldsymbol{\beta} \), +that is \( \boldsymbol{f}=\boldsymbol{X}\boldsymbol{\beta} \). In linear regression we can +simply invert a matrix and obtain the parameters \( \beta \) by +

    + +$$ +\boldsymbol{\beta}=\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +$$ + +

    In iterative fitting or additive modeling, we minimize the cost function with respect to the parameters \( \beta_m \) and \( \gamma_m \).

    @@ -410,7 +409,7 @@ One of the major reasons is that they can be stacked layer-wise to build deep ne

  • 55
  • 56
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs047.html b/doc/pub/week47/html/._week47-bs047.html index 511857826..d2c8a74e2 100644 --- a/doc/pub/week47/html/._week47-bs047.html +++ b/doc/pub/week47/html/._week47-bs047.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,19 +337,25 @@ MathJax.Hub.Config({

     

     

     

    -

    Boltzmann Machines

    +

    Iterative Fitting, Regression and Squared-error Cost Function

    -

    Why use a generative model rather than the more well known discriminative deep neural networks (DNN)? Simplest approach to generative deep learning.

    +

    The way we proceed is as follows (here we specialize to the squared-error cost function)

    -
      -
    • Discriminitave methods have several limitations: They are mainly supervised learning methods, thus requiring labeled data. And there are tasks they cannot accomplish, like drawing new examples from an unknown probability distribution.
    • -
    • A generative model can learn to represent and sample from a probability distribution. The core idea is to learn a parametric model of the probability distribution from which the training data was drawn. As an example +
        +
      1. Establish a cost function, here \( {\cal C}(\boldsymbol{y},\boldsymbol{f}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-f_M(x_i))^2 \) with \( f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m) \).
      2. +
      3. Initialize with a guess \( f_0(x) \). It could be one or even zero or some random numbers.
      4. +
      5. For \( m=1:M \)
          -
        1. A model for images could learn to draw new examples of cats and dogs, given a training dataset of images of cats and dogs.
        2. -
        3. Generate a sample of an ordered or disordered phase, having been given samples of such phases.
        4. -
        5. Model the trial function for Monte Carlo calculations.
        6. +
        7. minimize \( \sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta b(x;\gamma))^2 \) wrt \( \gamma \) and \( \beta \)
        8. +
        9. This gives the optimal values \( \beta_m \) and \( \gamma_m \)
        10. +
        11. Determine then the new values \( f_m(x)=f_{m-1}(x) +\beta_m b(x;\gamma_m) \)
        -
    + +

    We could use any of the algorithms we have discussed till now. If we +use trees, \( \gamma \) parameterizes the split variables and split points +at the internal nodes, and the predictions at the terminal nodes. +

    +

      @@ -413,7 +381,7 @@ MathJax.Hub.Config({
    • 56
    • 57
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs048.html b/doc/pub/week47/html/._week47-bs048.html index 4cb8826db..d2843927a 100644 --- a/doc/pub/week47/html/._week47-bs048.html +++ b/doc/pub/week47/html/._week47-bs048.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,14 +337,47 @@ MathJax.Hub.Config({

     

     

     

    -

    Some similarities and differences from DNNs

    +

    Squared-Error Example and Iterative Fitting

    -
      -
    1. Both use gradient-descent based learning procedures for minimizing cost functions
    2. -
    3. Energy based models don't use backpropagation and automatic differentiation for computing gradients, instead turning to Markov Chain Monte Carlo methods.
    4. -
    5. DNNs often have several hidden layers. A restricted Boltzmann machine has only one hidden layer, however several RBMs can be stacked to make up Deep Belief Networks, of which they constitute the building blocks.
    6. -
    -

    History: The RBM was developed by amongst others Geoffrey Hinton, called by some the "Godfather of Deep Learning", working with the University of Toronto and Google.

    +

    To better understand what happens, let us develop the steps for the iterative fitting using the above squared error function.

    + +

    For simplicity we assume also that our functions \( b(x;\gamma)=1+\gamma x \).

    + +

    This means that for every iteration \( m \), we need to optimize

    + +$$ +(\beta_m,\gamma_m) = \mathrm{argmin}_{\beta,\lambda}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta b(x;\gamma))^2=\sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta(1+\gamma x_i))^2. +$$ + +

    We start our iteration by simply setting \( f_0(x)=0 \). +Taking the derivatives with respect to \( \beta \) and \( \gamma \) we obtain +

    +$$ +\frac{\partial {\cal C}}{\partial \beta} = -2\sum_{i}(1+\gamma x_i)(y_i-\beta(1+\gamma x_i))=0, +$$ + +

    and

    +$$ +\frac{\partial {\cal C}}{\partial \gamma} =-2\sum_{i}\beta x_i(y_i-\beta(1+\gamma x_i))=0. +$$ + +

    We can then rewrite these equations as (defining \( \boldsymbol{w}=\boldsymbol{e}+\gamma \boldsymbol{x}) \) with \( \boldsymbol{e} \) being the unit vector)

    +$$ +\gamma \boldsymbol{w}^T(\boldsymbol{y}-\beta\gamma \boldsymbol{w})=0, +$$ + +

    which gives us \( \beta = \boldsymbol{w}^T\boldsymbol{y}/(\boldsymbol{w}^T\boldsymbol{w}) \). Similarly we have

    +$$ +\beta\gamma \boldsymbol{x}^T(\boldsymbol{y}-\beta(1+\gamma \boldsymbol{x}))=0, +$$ + +

    which leads to \( \gamma =(\boldsymbol{x}^T\boldsymbol{y}-\beta\boldsymbol{x}^T\boldsymbol{e})/(\beta\boldsymbol{x}^T\boldsymbol{x}) \). Inserting +for \( \beta \) gives us an equation for \( \gamma \). This is a non-linear equation in the unknown \( \gamma \) and has to be solved numerically. +

    + +

    The solution to these two equations gives us in turn \( \beta_1 \) and \( \gamma_1 \) leading to the new expression for \( f_1(x) \) as +\( f_1(x) = \beta_1(1+\gamma_1x) \). Doing this \( M \) times results in our final estimate for the function \( f \). +

    @@ -409,7 +404,7 @@ MathJax.Hub.Config({

  • 57
  • 58
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs049.html b/doc/pub/week47/html/._week47-bs049.html index 2ee5fc93d..b66c0a110 100644 --- a/doc/pub/week47/html/._week47-bs049.html +++ b/doc/pub/week47/html/._week47-bs049.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,38 +337,35 @@ MathJax.Hub.Config({

     

     

     

    -

    Boltzmann machines (BM)

    +

    Iterative Fitting, Classification and AdaBoost

    -
    -
    - -

    A BM is what we would call an undirected probabilistic graphical model -with stochastic continuous or discrete units. +

    Let us consider a binary classification problem with two outcomes \( y_i \in \{-1,1\} \) and \( i=0,1,2,\dots,n-1 \) as our set of +observations. We define a classification function \( G(x) \) which produces a prediction taking one or the other of the two values +\( \{-1,1\} \).

    -
    -
    -
    -
    - -

    It is interpreted as a stochastic recurrent neural network where the -state of each unit(neurons/nodes) depends on the units it is connected -to. The weights in the network represent thus the strength of the -interaction between various units/nodes. -

    -
    -
    +

    The error rate of the training sample is then

    -
    -
    - -

    It turns into a Hopfield network if we choose deterministic rather -than stochastic units. In contrast to a Hopfield network, a BM is a -so-called generative model. It allows us to generate new samples from -the learned distribution. +$$ +\mathrm{\overline{err}}=\frac{1}{n} \sum_{i=0}^{n-1} I(y_i\ne G(x_i)). +$$ + +

    The iterative procedure starts with defining a weak classifier whose +error rate is barely better than random guessing. The iterative +procedure in boosting is to sequentially apply a weak +classification algorithm to repeatedly modified versions of the data +producing a sequence of weak classifiers \( G_m(x) \).

    -
    -
    + +

    Here we will express our function \( f(x) \) in terms of \( G(x) \). That is

    +$$ +f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m), +$$ + +

    will be a function of

    +$$ +G_M(x) = \mathrm{sign} \sum_{i=1}^M \alpha_m G_m(x). +$$

    @@ -434,7 +393,7 @@ the learned distribution.

  • 58
  • 59
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs050.html b/doc/pub/week47/html/._week47-bs050.html index 908e7d76d..e8a69da24 100644 --- a/doc/pub/week47/html/._week47-bs050.html +++ b/doc/pub/week47/html/._week47-bs050.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,35 +337,29 @@ MathJax.Hub.Config({

     

     

     

    -

    A standard BM setup

    +

    Adaptive Boosting, AdaBoost

    -
    -
    - -

    A standard BM network is divided into a set of observable and visible units \( \hat{x} \) and a set of unknown hidden units/nodes \( \hat{h} \).

    -
    -
    +

    In our iterative procedure we define thus

    +$$ +f_m(x) = f_{m-1}(x)+\beta_mG_m(x). +$$ - -
    -
    - -

    Additionally there can be bias nodes for the hidden and visible layers. These biases are normally set to \( 1 \).

    -
    -
    - - -
    -
    - -

    BMs are stackable, meaning they cwe can train a BM which serves as input to another BM. We can construct deep networks for learning complex PDFs. The layers can be trained one after another, a feature which makes them popular in deep learning

    -
    -
    - - -

    However, they are often hard to train. This leads to the introduction of so-called restricted BMs, or RBMS. -Here we take away all lateral connections between nodes in the visible layer as well as connections between nodes in the hidden layer. The network is illustrated in the figure below. +

    The simplest possible cost function which leads (also simple from a computational point of view) to the AdaBoost algorithm is the +exponential cost/loss function defined as

    +$$ +C(\boldsymbol{y},\boldsymbol{f}) = \sum_{i=0}^{n-1}\exp{(-y_i(f_{m-1}(x_i)+\beta G(x_i))}. +$$ + +

    We optimize \( \beta \) and \( G \) for each value of \( m=1:M \) as we did in the regression case. +This is normally done in two steps. Let us however first rewrite the cost function as +

    + +$$ +C(\boldsymbol{y},\boldsymbol{f}) = \sum_{i=0}^{n-1}w_i^{m}\exp{(-y_i\beta G(x_i))}, +$$ + +

    where we have defined \( w_i^m= \exp{(-y_if_{m-1}(x_i))} \).

    @@ -430,7 +386,7 @@ Here we take away all lateral connections between nodes in the visible layer as

  • 59
  • 60
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs051.html b/doc/pub/week47/html/._week47-bs051.html index 99ea01e9e..d217303d9 100644 --- a/doc/pub/week47/html/._week47-bs051.html +++ b/doc/pub/week47/html/._week47-bs051.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,13 +337,45 @@ MathJax.Hub.Config({

     

     

     

    -

    The structure of the RBM network

    +

    Building up AdaBoost

    + +

    First, for any \( \beta > 0 \), we optimize \( G \) by setting

    +$$ +G_m(x) = \mathrm{sign} \sum_{i=0}^{n-1} w_i^m I(y_i \ne G_(x_i)), +$$ + +

    which is the classifier that minimizes the weighted error rate in predicting \( y \).

    + +

    We can do this by rewriting

    +$$ +\exp{-(\beta)}\sum_{y_i=G(x_i)}w_i^m+\exp{(\beta)}\sum_{y_i\ne G(x_i)}w_i^m, +$$ + +

    which can be rewritten as

    +$$ +(\exp{(\beta)}-\exp{-(\beta)})\sum_{i=0}^{n-1}w_i^mI(y_i\ne G(x_i))+\exp{(-\beta)}\sum_{i=0}^{n-1}w_i^m=0, +$$ + +

    which leads to

    +$$ +\beta_m = \frac{1}{2}\log{\frac{1-\mathrm{\overline{err}}}{\mathrm{\overline{err}}}}, +$$ + +

    where we have redefined the error as

    +$$ +\mathrm{\overline{err}}_m=\frac{1}{n}\frac{\sum_{i=0}^{n-1}w_i^mI(y_i\ne G(x_i)}{\sum_{i=0}^{n-1}w_i^m}, +$$ + +

    which leads to an update of

    +$$ +f_m(x) = f_{m-1}(x) +\beta_m G_m(x). +$$ + +

    This leads to the new weights

    +$$ +w_i^{m+1} = w_i^m \exp{(-y_i\beta_m G_m(x_i))} +$$ -

    -
    -

    -
    -

    @@ -408,7 +402,7 @@ MathJax.Hub.Config({

  • 60
  • 61
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs052.html b/doc/pub/week47/html/._week47-bs052.html index 5d828cae7..f7f33fb67 100644 --- a/doc/pub/week47/html/._week47-bs052.html +++ b/doc/pub/week47/html/._week47-bs052.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,13 +337,24 @@ MathJax.Hub.Config({

     

     

     

    -

    The network

    +

    Adaptive boosting: AdaBoost, Basic Algorithm

    + +

    The algorithm here is rather straightforward. Assume that our weak +classifier is a decision tree and we consider a binary set of outputs +with \( y_i \in \{-1,1\} \) and \( i=0,1,2,\dots,n-1 \) as our set of +observations. Our design matrix is given in terms of the +feature/predictor vectors +\( \boldsymbol{X}=[\boldsymbol{x}_0\boldsymbol{x}_1\dots\boldsymbol{x}_{p-1}] \). Finally, we define also a +classifier determined by our data via a function \( G(x) \). This function tells us how well we are able to classify our outputs/targets \( \boldsymbol{y} \). +

    + +

    We have already defined the misclassification error \( \mathrm{err} \) as

    +$$ +\mathrm{err}=\frac{1}{n}\sum_{i=0}^{n-1}I(y_i\ne G(x_i)), +$$ + +

    where the function \( I() \) is one if we misclassify and zero if we classify correctly.

    -The network layers: -
      -
    1. A function \( \mathbf{x} \) that represents the visible layer, a vector of \( M \) elements (nodes). This layer represents both what the RBM might be given as training input, and what we want it to be able to reconstruct. This might for example be given by the pixels of an image or coefficients representing speech, or the coordinates of a quantum mechanical state function.
    2. -
    3. The function \( \mathbf{h} \) represents the hidden, or latent, layer. A vector of \( N \) elements (nodes). Also called "feature detectors".
    4. -

      @@ -407,7 +380,7 @@ MathJax.Hub.Config({
    • 61
    • 62
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs053.html b/doc/pub/week47/html/._week47-bs053.html index a8aa977d0..6a9c6d955 100644 --- a/doc/pub/week47/html/._week47-bs053.html +++ b/doc/pub/week47/html/._week47-bs053.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,22 +337,38 @@ MathJax.Hub.Config({

     

     

     

    -

    Goals

    +

    Basic Steps of AdaBoost

    -

    The goal of the hidden layer is to increase the model's expressive -power. We encode complex interactions between visible variables by -introducing additional, hidden variables that interact with visible -degrees of freedom in a simple manner, yet still reproduce the complex -correlations between visible degrees in the data once marginalized -over (integrated out). +

    With the above definitions we are now ready to set up the algorithm for AdaBoost. +The basic idea is to set up weights which will be used to scale the correctly classified and the misclassified cases. +

    +
      +
    1. We start by initializing all weights to \( w_i = 1/n \), with \( i=0,1,2,\dots n-1 \). It is easy to see that we must have \( \sum_{i=0}^{n-1}w_i = 1 \).
    2. +
    3. We rewrite the misclassification error as
    4. +
    +$$ +\mathrm{\overline{err}}_m=\frac{\sum_{i=0}^{n-1}w_i^m I(y_i\ne G(x_i))}{\sum_{i=0}^{n-1}w_i}, +$$ + +
      +
    1. Then we start looping over all attempts at classifying, namely we start an iterative process for \( m=1:M \), where \( M \) is the final number of classifications. Our given classifier could for example be a plain decision tree. +
        +
      1. Fit then a given classifier to the training set using the weights \( w_i \).
      2. +
      3. Compute then \( \mathrm{err} \) and figure out which events are classified properly and which are classified wrongly.
      4. +
      5. Define a quantity \( \alpha_{m} = \log{(1-\mathrm{\overline{err}}_m)/\mathrm{\overline{err}}_m} \)
      6. +
      7. Set the new weights to \( w_i = w_i\times \exp{(\alpha_m I(y_i\ne G(x_i)} \).
      8. +
      +
    2. Compute the new classifier \( G(x)= \sum_{i=0}^{n-1}\alpha_m I(y_i\ne G(x_i) \).
    3. +
    +

    For the iterations with \( m \le 2 \) the weights are modified +individually at each steps. The observations which were misclassified +at iteration \( m-1 \) have a weight which is larger than those which were +classified properly. As this proceeds, the observations which were +difficult to classifiy correctly are given a larger influence. Each +new classification step \( m \) is then forced to concentrate on those +observations that are missed in the previous iterations.

    -The network parameters, to be optimized/learned: -
      -
    1. \( \mathbf{a} \) represents the visible bias, a vector of same length as \( \mathbf{x} \).
    2. -
    3. \( \mathbf{b} \) represents the hidden bias, a vector of same lenght as \( \mathbf{h} \).
    4. -
    5. \( W \) represents the interaction weights, a matrix of size \( M\times N \).
    6. -

      @@ -416,7 +394,7 @@ over (integrated out).
    • 62
    • 63
    • ...
    • -
    • 77
    • +
    • 66
    • »
    diff --git a/doc/pub/week47/html/._week47-bs054.html b/doc/pub/week47/html/._week47-bs054.html index a399776ee..d6d4cea1e 100644 --- a/doc/pub/week47/html/._week47-bs054.html +++ b/doc/pub/week47/html/._week47-bs054.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,25 +337,46 @@ MathJax.Hub.Config({

     

     

     

    -

    Joint distribution

    +

    AdaBoost Examples

    -

    The restricted Boltzmann machine is described by a Boltzmann distribution

    -$$ -\begin{align} - P_{rbm}(\mathbf{x},\mathbf{h}) = \frac{1}{Z} e^{-\frac{1}{T_0}E(\mathbf{x},\mathbf{h})}, -\tag{1} -\end{align} -$$ +

    Using Scikit-Learn it is easy to apply the adaptive boosting algorithm, as done here.

    -

    where \( Z \) is the normalization constant or partition function, defined as

    -$$ -\begin{align} - Z = \int \int e^{-\frac{1}{T_0}E(\mathbf{x},\mathbf{h})} d\mathbf{x} d\mathbf{h}. -\tag{2} -\end{align} -$$ -

    It is common to ignore \( T_0 \) by setting it to one.

    + +
    +
    +
    +
    +
    +
    from sklearn.ensemble import AdaBoostClassifier
    +
    +ada_clf = AdaBoostClassifier(
    +    DecisionTreeClassifier(max_depth=2), n_estimators=200,
    +    algorithm="SAMME.R", learning_rate=0.01, random_state=42)
    +ada_clf.fit(X_train, y_train)
    +y_pred = ada_clf.predict(X_test)
    +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
    +plt.show()
    +y_probas = ada_clf.predict_proba(X_test)
    +skplt.metrics.plot_roc(y_test, y_probas)
    +plt.show()
    +skplt.metrics.plot_cumulative_gain(y_test, y_probas)
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -420,7 +403,7 @@ $$

  • 63
  • 64
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs055.html b/doc/pub/week47/html/._week47-bs055.html index cf176985d..2a3d6dae4 100644 --- a/doc/pub/week47/html/._week47-bs055.html +++ b/doc/pub/week47/html/._week47-bs055.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,26 +337,108 @@ MathJax.Hub.Config({

     

     

     

    -

    Network Elements, the energy function

    +

    Making an ADAboost code yourself

    -

    The function \( E(\mathbf{x},\mathbf{h}) \) gives the energy of a -configuration (pair of vectors) \( (\mathbf{x}, \mathbf{h}) \). The lower -the energy of a configuration, the higher the probability of it. This -function also depends on the parameters \( \mathbf{a} \), \( \mathbf{b} \) and -\( W \). Thus, when we adjust them during the learning procedure, we are -adjusting the energy function to best fit our problem. -

    -

    An expression for the energy function is

    -$$ -E(\hat{x},\hat{h}) = -\sum_{ia}^{NA}b_i^a \alpha_i^a(x_i)-\sum_{jd}^{MD}c_j^d \beta_j^d(h_j)-\sum_{ijad}^{NAMD}b_i^a \alpha_i^a(x_i)c_j^d \beta_j^d(h_j)w_{ij}^{ad}. -$$ + +
    +
    +
    +
    +
    +
    import numpy as np
     
    -

    Here \( \beta_j^d(h_j) \) and \( \alpha_i^a(x_j) \) are so-called transfer functions that map a given input value to a desired feature value. The labels \( a \) and \( d \) denote that there can be multiple transfer functions per variable. The first sum depends only on the visible units. The second on the hidden ones. Note that there is no connection between nodes in a layer.

    +class DecisionStump: + def fit(self, X, y, weights): + m, n = X.shape + self.alpha = 0 + self.threshold = None + self.polarity = 1 -

    The quantities \( b \) and \( c \) can be interpreted as the visible and hidden biases, respectively.

    + min_error = float('inf') + + for feature in range(n): + feature_values = np.unique(X[:, feature]) + + for threshold in feature_values: + for polarity in [1, -1]: + predictions = np.ones(m) + predictions[X[:, feature] < threshold] = -1 + predictions *= polarity + + error = sum(weights[predictions != y]) + + if error < min_error: + min_error = error + self.alpha = 0.5 * np.log((1 - error) / (error + 1e-10)) + self.threshold = threshold + self.feature_index = feature + self.polarity = polarity + + def predict(self, X): + m = X.shape[0] + predictions = np.ones(m) + if self.polarity == 1: + predictions[X[:, self.feature_index] < self.threshold] = -1 + else: + predictions[X[:, self.feature_index] >= self.threshold] = -1 + return predictions + +class AdaBoost: + def fit(self, X, y, n_estimators): + m = X.shape[0] + self.alphas = [] + self.models = [] + + weights = np.ones(m) / m + + for _ in range(n_estimators): + stump = DecisionStump() + stump.fit(X, y, weights) + predictions = stump.predict(X) + + error = sum(weights[predictions != y]) + if error == 0: + break + + self.models.append(stump) + self.alphas.append(stump.alpha) + + weights *= np.exp(-stump.alpha * y * predictions) + weights /= np.sum(weights) + + def predict(self, X): + final_predictions = np.zeros(X.shape[0]) + for alpha, model in zip(self.alphas, self.models): + final_predictions += alpha * model.predict(X) + return np.sign(final_predictions) + +# Example dataset (X, y) +X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]) +y = np.array([-1, -1, -1, -1, 1, 1, 1, 1, 1, 1]) # Labels must be -1 or 1 + +# Train AdaBoost +ada = AdaBoost() +ada.fit(X, y, n_estimators=10) + +# Predictions +predictions = ada.predict(X) +print("Predictions:", predictions) +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    The connection between the nodes in the two layers is given by the weights \( w_{ij} \).

    @@ -421,7 +465,7 @@ $$

  • 64
  • 65
  • ...
  • -
  • 77
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs056.html b/doc/pub/week47/html/._week47-bs056.html index bc50edf69..05cc18feb 100644 --- a/doc/pub/week47/html/._week47-bs056.html +++ b/doc/pub/week47/html/._week47-bs056.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,39 +337,17 @@ MathJax.Hub.Config({

     

     

     

    -

    Defining different types of RBMs

    -

    There are different variants of RBMs, and the differences lie in the types of visible and hidden units we choose as well as in the implementation of the energy function \( E(\mathbf{x},\mathbf{h}) \).

    +

    Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent

    -
    -
    - - -

    RBMs were first developed using binary units in both the visible and hidden layer. The corresponding energy function is defined as follows:

    -$$ -\begin{align} - E(\mathbf{x}, \mathbf{h}) = - \sum_i^M x_i a_i- \sum_j^N b_j h_j - \sum_{i,j}^{M,N} x_i w_{ij} h_j, -\tag{3} -\end{align} -$$ - -

    where the binary values taken on by the nodes are most commonly 0 and 1.

    -
    -
    - -
    -
    - - -

    Another varient is the RBM where the visible units are Gaussian while the hidden units remain binary:

    -$$ -\begin{align} - E(\mathbf{x}, \mathbf{h}) = \sum_i^M \frac{(x_i - a_i)^2}{2\sigma_i^2} - \sum_j^N b_j h_j - \sum_{i,j}^{M,N} \frac{x_i w_{ij} h_j}{\sigma_i^2}. -\tag{4} -\end{align} -$$ -
    -
    +

    Gradient boosting is again a similar technique to Adaptive boosting, +it combines so-called weak classifiers or regressors into a strong +method via a series of iterations. +

    +

    In order to understand the method, let us illustrate its basics by +bringing back the essential steps in linear regression, where our cost +function was the least squares function. +

    @@ -433,8 +373,6 @@ $$

  • 64
  • 65
  • 66
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs057.html b/doc/pub/week47/html/._week47-bs057.html index fa2b659b8..a6205d684 100644 --- a/doc/pub/week47/html/._week47-bs057.html +++ b/doc/pub/week47/html/._week47-bs057.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,19 +337,35 @@ MathJax.Hub.Config({

     

     

     

    -

    More about RBMs

    -
      -
    1. Useful when we model continuous data (i.e., we wish \( \mathbf{x} \) to be continuous)
    2. -
    3. Requires a smaller learning rate, since there's no upper bound to the value a component might take in the reconstruction
    4. -
    -

    Other types of units include:

    -
      -
    1. Softmax and multinomial units
    2. -
    3. Gaussian visible and hidden units
    4. -
    5. Binomial units
    6. -
    7. Rectified linear units
    8. -
    -

    To read more, see Lectures on Boltzmann machines in Physics.

    +

    The Squared-Error again! Steepest Descent

    + +

    We start again with our cost function \( {\cal C}(\boldsymbol{y}m\boldsymbol{f})=\sum_{i=0}^{n-1}{\cal L}(y_i, f(x_i)) \) where we want to minimize +This means that for every iteration, we need to optimize +

    + +$$ +(\hat{\boldsymbol{f}}) = \mathrm{argmin}_{\boldsymbol{f}}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i-f(x_i))^2. +$$ + +

    We define a real function \( h_m(x) \) that defines our final function \( f_M(x) \) as

    +$$ +f_M(x) = \sum_{m=0}^M h_m(x). +$$ + +

    In the steepest decent approach we approximate \( h_m(x) = -\rho_m g_m(x) \), where \( \rho_m \) is a scalar and \( g_m(x) \) the gradient defined as

    +$$ +g_m(x_i) = \left[ \frac{\partial {\cal L}(y_i, f(x_i))}{\partial f(x_i)}\right]_{f(x_i)=f_{m-1}(x_i)}. +$$ + +

    With the new gradient we can update \( f_m(x) = f_{m-1}(x) -\rho_m g_m(x) \). Using the above squared-error function we see that +the gradient is \( g_m(x_i) = -2(y_i-f(x_i)) \). +

    + +

    Choosing \( f_0(x)=0 \) we obtain \( g_m(x) = -2y_i \) and inserting this into the minimization problem for the cost function we have

    +$$ +(\rho_1) = \mathrm{argmin}_{\rho}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i+2\rho y_i)^2. +$$ +

    @@ -412,9 +390,6 @@ MathJax.Hub.Config({

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs058.html b/doc/pub/week47/html/._week47-bs058.html index 084f5dde3..35d410eeb 100644 --- a/doc/pub/week47/html/._week47-bs058.html +++ b/doc/pub/week47/html/._week47-bs058.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,38 +337,19 @@ MathJax.Hub.Config({

     

     

     

    -

    Autoencoders: Overarching view

    +

    Steepest Descent Example

    -

    Autoencoders are artificial neural networks capable of learning -efficient representations of the input data (these representations are called codings) without -any supervision (i.e., the training set is unlabeled). These codings -typically have a much lower dimensionality than the input data, making -autoencoders useful for dimensionality reduction. -

    +

    Optimizing with respect to \( \rho \) we obtain (taking the derivative) that \( \rho_1 = -1/2 \). We have then that

    +$$ +f_1(x) = f_{0}(x) -\rho_1 g_1(x)=-y_i. +$$ -

    More importantly, autoencoders act as powerful feature detectors, and -they can be used for unsupervised pretraining of deep neural networks. -

    +

    We can then proceed and compute

    +$$ +g_2(x_i) = \left[ \frac{\partial {\cal L}(y_i, f(x_i))}{\partial f(x_i)}\right]_{f(x_i)=f_{1}(x_i)=y_i}=-4y_i, +$$ -

    Lastly, they are capable of randomly generating new data that looks -very similar to the training data; this is called a generative -model. For example, you could train an autoencoder on pictures of -faces, and it would then be able to generate new faces. Surprisingly, -autoencoders work by simply learning to copy their inputs to their -outputs. This may sound like a trivial task, but we will see that -constraining the network in various ways can make it rather -difficult. For example, you can limit the size of the internal -representation, or you can add noise to the inputs and train the -network to recover the original inputs. These constraints prevent the -autoencoder from trivially copying the inputs directly to the outputs, -which forces it to learn efficient ways of representing the data. In -short, the codings are byproducts of the autoencoder’s attempt to -learn the identity function under some constraints. -

    - -Video on autoencoders - -

    See also A. Geron's textbook, chapter 15.

    +

    and find a new value for \( \rho_2=-1/2 \) and continue till we have reached \( m=M \). We can modify the steepest descent method, or steepest boosting, by introducing what is called gradient boosting.

    @@ -430,10 +373,6 @@ learn the identity function under some constraints.

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • 68
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs059.html b/doc/pub/week47/html/._week47-bs059.html index a14ac3230..9b2a1a98d 100644 --- a/doc/pub/week47/html/._week47-bs059.html +++ b/doc/pub/week47/html/._week47-bs059.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,23 +337,29 @@ MathJax.Hub.Config({

     

     

     

    -

    Bayesian Machine Learning

    +

    Gradient Boosting, algorithm

    -

    This is an important topic if we aim at extracting a probability -distribution. This gives us also a confidence interval and error -estimates. +

    Steepest descent is however not much used, since it only optimizes \( f \) at a fixed set of \( n \) points, +so we do not learn a function that can generalize. However, we can modify the algorithm by +fitting a weak learner to approximate the negative gradient signal.

    -

    Bayesian machine learning allows us to encode our prior beliefs about -what those models should look like, independent of what the data tells -us. This is especially useful when we don’t have a ton of data to -confidently learn our model. -

    - -Video on Bayesian deep learning - -

    See also the slides here.

    +

    Suppose we have a cost function \( C(f)=\sum_{i=0}^{n-1}L(y_i, f(x_i)) \) where \( y_i \) is our target and \( f(x_i) \) the function which is meant to model \( y_i \). The above cost function could be our standard squared-error function

    +$$ +C(\boldsymbol{y},\boldsymbol{f})=\sum_{i=0}^{n-1}(y_i-f(x_i))^2. +$$ +

    The way we proceed in an iterative fashion is to

    +
      +
    1. Initialize our estimate \( f_0(x) \).
    2. +
    3. For \( m=1:M \), we +
        +
      1. compute the negative gradient vector \( \boldsymbol{u}_m = -\partial C(\boldsymbol{y},\boldsymbol{f})/\partial \boldsymbol{f}(x) \) at \( f(x) = f_{m-1}(x) \);
      2. +
      3. fit the so-called base-learner to the negative gradient \( h_m(u_m,x) \);
      4. +
      5. update the estimate \( f_m(x) = f_{m-1}(x)+h_m(u_m,x) \);
      6. +
      +
    4. The final estimate is then \( f_M(x) = \sum_{m=1}^M h_m(u_m,x) \).
    5. +

    diff --git a/doc/pub/week47/html/._week47-bs060.html b/doc/pub/week47/html/._week47-bs060.html index 7fbd2ce38..21b54ca16 100644 --- a/doc/pub/week47/html/._week47-bs060.html +++ b/doc/pub/week47/html/._week47-bs060.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,36 +337,70 @@ MathJax.Hub.Config({

     

     

     

    -

    Reinforcement Learning

    +

    Gradient Boosting, Examples of Regression

    -

    Reinforcement Learning (RL) is one of the most exciting fields of -Machine Learning today, and also one of the oldest. It has been around -since the 1950s, producing many interesting applications over the -years. -

    + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import train_test_split
    +from sklearn.ensemble import GradientBoostingRegressor
    +import scikitplot as skplt
    +from sklearn.metrics import mean_squared_error
     
    -

    It studies -how agents take actions based on trial and error, so as to maximize -some notion of cumulative reward in a dynamic system or -environment. Due to its generality, the problem has also been studied -in many other disciplines, such as game theory, control theory, -operations research, information theory, multi-agent systems, swarm -intelligence, statistics, and genetic algorithms. -

    +n = 100 +maxdegree = 6 -

    In March 2016, AlphaGo, a computer program that plays the board game -Go, beat Lee Sedol in a five-game match. This was the first time a -computer Go program had beaten a 9-dan (highest rank) professional -without handicaps. AlphaGo is based on deep convolutional neural -networks and reinforcement learning. AlphaGo’s victory was a major -milestone in artificial intelligence and it has also made -reinforcement learning a hot research area in the field of machine -learning. -

    +# Make data set. +x = np.linspace(-3, 3, n).reshape(-1, 1) +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape) -

    Lecture on Reinforcement Learning.

    +error = np.zeros(maxdegree) +bias = np.zeros(maxdegree) +variance = np.zeros(maxdegree) +polydegree = np.zeros(maxdegree) +X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2) + +for degree in range(1,maxdegree): + model = GradientBoostingRegressor(max_depth=degree, n_estimators=100, learning_rate=1.0) + model.fit(X_train,y_train) + y_pred = model.predict(X_test) + polydegree[degree] = degree + error[degree] = np.mean( np.mean((y_test - y_pred)**2) ) + bias[degree] = np.mean( (y_test - np.mean(y_pred))**2 ) + variance[degree] = np.mean( np.var(y_pred) ) + print('Max depth:', degree) + print('Error:', error[degree]) + print('Bias^2:', bias[degree]) + print('Var:', variance[degree]) + print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) + +plt.xlim(1,maxdegree-1) +plt.plot(polydegree, error, label='Error') +plt.plot(polydegree, bias, label='bias') +plt.plot(polydegree, variance, label='Variance') +plt.legend() +save_fig("gdregression") +plt.show() +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    See also A. Geron's textbook, chapter 16.

    @@ -426,12 +422,6 @@ learning.

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • 68
  • -
  • 69
  • -
  • 70
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs061.html b/doc/pub/week47/html/._week47-bs061.html index e49a622a7..cbf84520c 100644 --- a/doc/pub/week47/html/._week47-bs061.html +++ b/doc/pub/week47/html/._week47-bs061.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,19 +337,69 @@ MathJax.Hub.Config({

     

     

     

    -

    Transfer learning

    +

    Gradient Boosting, Classification Example

    -

    The goal of transfer learning is to transfer the model or knowledge -obtained from a source task to the target task, in order to resolve -the issues of insufficient training data in the target task. The -rationality of doing so lies in that usually the source and target -tasks have inter-correlations, and therefore either the features, -samples, or models in the source task might provide useful information -for us to better solve the target task. Transfer learning is a hot -research topic in recent years, with many problems still waiting to be studied. -

    + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.datasets import load_breast_cancer
    +import scikitplot as skplt
    +from sklearn.ensemble import GradientBoostingClassifier
    +from sklearn.model_selection import cross_validate
    +
    +# Load the data
    +cancer = load_breast_cancer()
    +
    +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    +print(X_train.shape)
    +print(X_test.shape)
    +#now scale the data
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +gd_clf = GradientBoostingClassifier(max_depth=3, n_estimators=100, learning_rate=1.0)  
    +gd_clf.fit(X_train_scaled, y_train)
    +#Cross validation
    +accuracy = cross_validate(gd_clf,X_test_scaled,y_test,cv=10)['test_score']
    +print(accuracy)
    +print("Test set accuracy with Gradient boosting and scaled data: {:.2f}".format(gd_clf.score(X_test_scaled,y_test)))
    +
    +import scikitplot as skplt
    +y_pred = gd_clf.predict(X_test_scaled)
    +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
    +save_fig("gdclassiffierconfusion")
    +plt.show()
    +y_probas = gd_clf.predict_proba(X_test_scaled)
    +skplt.metrics.plot_roc(y_test, y_probas)
    +save_fig("gdclassiffierroc")
    +plt.show()
    +skplt.metrics.plot_cumulative_gain(y_test, y_probas)
    +save_fig("gdclassiffiercgain")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    Lecture on transfer learning.

    @@ -408,13 +420,6 @@ research topic in recent years, with many problems still waiting to be studied.

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • 68
  • -
  • 69
  • -
  • 70
  • -
  • 71
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs062.html b/doc/pub/week47/html/._week47-bs062.html index c223e7a49..45be94403 100644 --- a/doc/pub/week47/html/._week47-bs062.html +++ b/doc/pub/week47/html/._week47-bs062.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,20 +337,22 @@ MathJax.Hub.Config({

     

     

     

    -

    Adversarial learning

    +

    XGBoost: Extreme Gradient Boosting

    -

    The conventional deep generative model has a potential problem: the -model tends to generate extreme instances to maximize the -probabilistic likelihood, which will hurt its performance. Adversarial -learning utilizes the adversarial behaviors (e.g., generating -adversarial instances or training an adversarial model) to enhance the -robustness of the model and improve the quality of the generated -data. In recent years, one of the most promising unsupervised learning -technologies, generative adversarial networks (GAN), has already been -successfully applied to image, speech, and text. +

    XGBoost or Extreme Gradient +Boosting, is an optimized distributed gradient boosting library +designed to be highly efficient, flexible and portable. It implements +machine learning algorithms under the Gradient Boosting +framework. XGBoost provides a parallel tree boosting that solve many +data science problems in a fast and accurate way. See the article by Chen and Guestrin.

    -

    Lecture on adversial learning.

    +

    The authors design and build a highly scalable end-to-end tree +boosting system. It has a theoretically justified weighted quantile +sketch for efficient proposal calculation. It introduces a novel sparsity-aware algorithm for parallel tree learning and an effective cache-aware block structure for out-of-core tree learning. +

    + +

    It is now the algorithm which wins essentially all ML competitions!!!

    @@ -408,14 +372,6 @@ successfully applied to image, speech, and text.

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • 68
  • -
  • 69
  • -
  • 70
  • -
  • 71
  • -
  • 72
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs063.html b/doc/pub/week47/html/._week47-bs063.html index 5441ef139..0262087f7 100644 --- a/doc/pub/week47/html/._week47-bs063.html +++ b/doc/pub/week47/html/._week47-bs063.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,18 +337,71 @@ MathJax.Hub.Config({

     

     

     

    -

    Dual learning

    +

    Regression Case

    + + + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import train_test_split
    +import xgboost as xgb
    +import scikitplot as skplt
    +from sklearn.metrics import mean_squared_error
    +
    +n = 100
    +maxdegree = 6
    +
    +# Make data set.
    +x = np.linspace(-3, 3, n).reshape(-1, 1)
    +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
    +
    +error = np.zeros(maxdegree)
    +bias = np.zeros(maxdegree)
    +variance = np.zeros(maxdegree)
    +polydegree = np.zeros(maxdegree)
    +X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    +
    +for degree in range(maxdegree):
    +    model =  xgb.XGBRegressor(objective ='reg:squarederror', colsaobjective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.1,max_depth = degree, alpha = 10, n_estimators = 200)
    +
    +    model.fit(X_train,y_train)
    +    y_pred = model.predict(X_test)
    +    polydegree[degree] = degree
    +    error[degree] = np.mean( np.mean((y_test - y_pred)**2) )
    +    bias[degree] = np.mean( (y_test - np.mean(y_pred))**2 )
    +    variance[degree] = np.mean( np.var(y_pred) )
    +    print('Max depth:', degree)
    +    print('Error:', error[degree])
    +    print('Bias^2:', bias[degree])
    +    print('Var:', variance[degree])
    +    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
    +
    +plt.xlim(1,maxdegree-1)
    +plt.plot(polydegree, error, label='Error')
    +plt.plot(polydegree, bias, label='bias')
    +plt.plot(polydegree, variance, label='Variance')
    +plt.legend()
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    Dual learning is a new learning paradigm, the basic idea of which is -to use the primal-dual structure between machine learning tasks to -obtain effective feedback/regularization, and guide and strengthen the -learning process, thus reducing the requirement of large-scale labeled -data for deep learning. The idea of dual learning has been applied to -many problems in machine learning, including machine translation, -image style conversion, question answering and generation, image -classification and generation, text classification and generation, -image-to-text, and text-to-image. -

    @@ -405,15 +420,6 @@ image-to-text, and text-to-image.

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • 68
  • -
  • 69
  • -
  • 70
  • -
  • 71
  • -
  • 72
  • -
  • 73
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs064.html b/doc/pub/week47/html/._week47-bs064.html index 1703c9567..792134cf4 100644 --- a/doc/pub/week47/html/._week47-bs064.html +++ b/doc/pub/week47/html/._week47-bs064.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,13 +337,82 @@ MathJax.Hub.Config({

     

     

     

    -

    Distributed machine learning

    +

    Xgboost on the Cancer Data

    + +

    As you will see from the confusion matrix below, XGBoots does an excellent job on the Wisconsin cancer data and outperforms essentially all agorithms we have discussed till now.

    + + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.preprocessing import LabelEncoder
    +from sklearn.model_selection import cross_validate
    +import scikitplot as skplt
    +import xgboost as xgb
    +# Load the data
    +cancer = load_breast_cancer()
    +
    +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    +print(X_train.shape)
    +print(X_test.shape)
    +#now scale the data
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +xg_clf = xgb.XGBClassifier()
    +xg_clf.fit(X_train_scaled,y_train)
    +
    +y_test = xg_clf.predict(X_test_scaled)
    +
    +print("Test set accuracy with Gradient Boosting and scaled data: {:.2f}".format(xg_clf.score(X_test_scaled,y_test)))
    +
    +import scikitplot as skplt
    +y_pred = xg_clf.predict(X_test_scaled)
    +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
    +save_fig("xdclassiffierconfusion")
    +plt.show()
    +y_probas = xg_clf.predict_proba(X_test_scaled)
    +skplt.metrics.plot_roc(y_test, y_probas)
    +save_fig("xdclassiffierroc")
    +plt.show()
    +skplt.metrics.plot_cumulative_gain(y_test, y_probas)
    +save_fig("gdclassiffiercgain")
    +plt.show()
    +
    +
    +xgb.plot_tree(xg_clf,num_trees=0)
    +plt.rcParams['figure.figsize'] = [50, 10]
    +save_fig("xgtree")
    +plt.show()
    +
    +xgb.plot_importance(xg_clf)
    +plt.rcParams['figure.figsize'] = [5, 5]
    +save_fig("xgparams")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    Distributed computation will speed up machine learning algorithms, -significantly improve their efficiency, and thus enlarge their -application. When distributed meets machine learning, more than just -implementing the machine learning algorithms in parallel is required. -

    @@ -399,16 +430,6 @@ implementing the machine learning algorithms in parallel is required.

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • 68
  • -
  • 69
  • -
  • 70
  • -
  • 71
  • -
  • 72
  • -
  • 73
  • -
  • 74
  • -
  • ...
  • -
  • 77
  • »
  • diff --git a/doc/pub/week47/html/._week47-bs065.html b/doc/pub/week47/html/._week47-bs065.html index 3271e7237..87a20db8f 100644 --- a/doc/pub/week47/html/._week47-bs065.html +++ b/doc/pub/week47/html/._week47-bs065.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -289,81 +262,70 @@ MathJax.Hub.Config({ Contents @@ -375,16 +337,105 @@ MathJax.Hub.Config({

     

     

     

    -

    Meta learning

    +

    Gradient boosting, making our own code for a regression case

    -

    Meta learning is an emerging research direction in machine -learning. Roughly speaking, meta learning concerns learning how to -learn, and focuses on the understanding and adaptation of the learning -itself, instead of just completing a specific learning task. That is, -a meta learner needs to be able to evaluate its own learning methods -and adjust its own learning methods according to specific learning -tasks. -

    + + +
    +
    +
    +
    +
    +
    import numpy as np
    +class DecisionTreeRegressor:
    +    def __init__(self, max_depth=3):
    +        self.max_depth = max_depth
    +        self.tree = None
    +    def fit(self, X, y):
    +        self.tree = self._grow_tree(X, y)
    +    def _grow_tree(self, X, y, depth=0):
    +        n_samples, n_features = X.shape
    +        if depth < self.max_depth:
    +            best_feature, best_threshold = self._best_split(X, y)
    +            if best_feature is not None:
    +                left_indices = X[:, best_feature] < best_threshold
    +                right_indices = X[:, best_feature] >= best_threshold
    +                left_child = self._grow_tree(X[left_indices], y[left_indices], depth + 1)
    +                right_child = self._grow_tree(X[right_indices], y[right_indices], depth + 1)
    +                return (best_feature, best_threshold, left_child, right_child)
    +        return np.mean(y)
    +    def _best_split(self, X, y):
    +        best_mse = float('inf')
    +        best_feature, best_threshold = None, None
    +        n_samples, n_features = X.shape
    +        
    +        for feature in range(n_features):
    +            thresholds = np.unique(X[:, feature])
    +            for threshold in thresholds:
    +                left_indices = X[:, feature] < threshold
    +                right_indices = X[:, feature] >= threshold
    +                if len(y[left_indices]) > 0 and len(y[right_indices]) > 0:
    +                    left_mse = np.mean((y[left_indices] - np.mean(y[left_indices])) ** 2)
    +                    right_mse = np.mean((y[right_indices] - np.mean(y[right_indices])) ** 2)
    +                    mse = (len(y[left_indices]) * left_mse + len(y[right_indices]) * right_mse) / n_samples
    +                    
    +                    if mse < best_mse:
    +                        best_mse = mse
    +                        best_feature = feature
    +                        best_threshold = threshold
    +        return best_feature, best_threshold
    +    def predict(self, X):
    +        return np.array([self._predict_sample(sample, self.tree) for sample in X])
    +    def _predict_sample(self, sample, node):
    +        if isinstance(node, tuple):
    +            feature, threshold, left_child, right_child = node
    +            if sample[feature] < threshold:
    +                return self._predict_sample(sample, left_child)
    +            else:
    +                return self._predict_sample(sample, right_child)
    +        return node
    +class GradientBoostingRegressor:
    +    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
    +        self.n_estimators = n_estimators
    +        self.learning_rate = learning_rate
    +        self.max_depth = max_depth
    +        self.models = []
    +    def fit(self, X, y):
    +        y_pred = np.zeros(y.shape)
    +        for _ in range(self.n_estimators):
    +            residuals = y - y_pred
    +            model = DecisionTreeRegressor(max_depth=self.max_depth)
    +            model.fit(X, residuals)
    +            y_pred += self.learning_rate * model.predict(X)
    +            self.models.append(model)
    +    def predict(self, X):
    +        y_pred = np.zeros(X.shape[0])
    +        for model in self.models:
    +            y_pred += self.learning_rate * model.predict(X)
    +        return y_pred
    +# Example usage
    +if __name__ == "__main__":
    +    # Sample data
    +    X = np.array([[1], [2], [3], [4], [5]])
    +    y = np.array([1.5, 1.7, 3.5, 3.7, 5.0])
    +    model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=2)
    +    model.fit(X, y)
    +    predictions = model.predict(X)
    +    print("Predictions:", predictions)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -401,18 +452,6 @@ tasks.

  • 64
  • 65
  • 66
  • -
  • 67
  • -
  • 68
  • -
  • 69
  • -
  • 70
  • -
  • 71
  • -
  • 72
  • -
  • 73
  • -
  • 74
  • -
  • 75
  • -
  • ...
  • -
  • 77
  • -
  • »
  • diff --git a/doc/pub/week47/html/week47-bs.html b/doc/pub/week47/html/week47-bs.html index 8424394b5..7f570d170 100644 --- a/doc/pub/week47/html/week47-bs.html +++ b/doc/pub/week47/html/week47-bs.html @@ -37,6 +37,116 @@ doconce format html week47.do.txt --html_style=bootstrap --pygments_html_style=d @@ -144,29 +262,70 @@ MathJax.Hub.Config({ Contents @@ -221,7 +380,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 25
  • +
  • 66
  • »
  • diff --git a/doc/pub/week47/html/week47-reveal.html b/doc/pub/week47/html/week47-reveal.html index 8065cd145..5d71e8ed2 100644 --- a/doc/pub/week47/html/week47-reveal.html +++ b/doc/pub/week47/html/week47-reveal.html @@ -210,32 +210,1658 @@ MathJax.Hub.Config({
    -Material for the lecture Monday 18 November +Plans for the lecture Monday 18 November, with video suggestions etc

    1. Basics of decision trees, classification and regression algorithms and ensemble models
    2. -

    3. Readings and Videos:
    4. -
    +
    + + +
    +

    Building a tree, regression

    + +

    There are mainly two steps

    +
      +

    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 \).
    2. + +

    3. 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 \).
    4. +
    +

    +

    How do we construct the regions \( R_1,\dots,R_J \)? In theory, the +regions could have any shape. However, we choose to divide the +predictor space into high-dimensional rectangles, or boxes, for +simplicity and for ease of interpretation of the resulting predictive +model. The goal is to find boxes \( R_1,\dots,R_J \) that minimize the +MSE, given by +

    + +

     
    +$$ +\sum_{j=1}^J\sum_{i\in R_j}(y_i-\overline{y}_{R_j})^2, +$$ +

     
    + +

    where \( \overline{y}_{R_j} \) is the mean response for the training observations +within box \( j \). +

    +
    + +
    +

    A top-down approach, recursive binary splitting

    + +

    Unfortunately, it is computationally infeasible to consider every +possible partition of the feature space into \( J \) boxes. The common +strategy is to take a top-down approach +

    + +

    The approach is top-down because it begins at the top of the tree (all +observations belong to a single region) and then successively splits +the predictor space; each split is indicated via two new branches +further down on the tree. It is greedy because at each step of the +tree-building process, the best split is made at that particular step, +rather than looking ahead and picking a split that will lead to a +better tree in some future step. +

    +
    + +
    +

    Making a tree

    + +

    In order to implement the recursive binary splitting we start by selecting +the predictor \( x_j \) and a cutpoint \( s \) that splits the predictor space into two regions \( R_1 \) and \( R_2 \) +

    +

     
    +$$ +\left\{X\vert x_j < s\right\}, +$$ +

     
    + +

    and

    +

     
    +$$ +\left\{X\vert x_j \geq s\right\}, +$$ +

     
    + +

    so that we obtain the lowest MSE, that is

    +

     
    +$$ +\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, +$$ +

     
    + +

    which we want to minimize by considering all predictors +\( x_1,x_2,\dots,x_p \). We consider also all possible values of \( s \) for +each predictor. These values could be determined by randomly assigned +numbers or by starting at the midpoint and then proceed till we find +an optimal value. +

    + +

    For any \( j \) and \( s \), we define the pair of half-planes where +\( \overline{y}_{R_1} \) is the mean response for the training +observations in \( R_1(j,s) \), and \( \overline{y}_{R_2} \) is the mean +response for the training observations in \( R_2(j,s) \). +

    + +

    Finding the values of \( j \) and \( s \) that minimize the above equation can be +done quite quickly, especially when the number of features \( p \) is not +too large. +

    + +

    Next, we repeat the process, looking +for the best predictor and best cutpoint in order to split the data +further so as to minimize the MSE within each of the resulting +regions. However, this time, instead of splitting the entire predictor +space, we split one of the two previously identified regions. We now +have three regions. Again, we look to split one of these three regions +further, so as to minimize the MSE. The process continues until a +stopping criterion is reached; for instance, we may continue until no +region contains more than five observations. +

    +
    + +
    +

    Pruning the tree

    + +

    The above procedure is rather straightforward, but leads often to +overfitting and unnecessarily large and complicated trees. The basic +idea is to grow a large tree \( T_0 \) and then prune it back in order to +obtain a subtree. A smaller tree with fewer splits (fewer regions) can +lead to smaller variance and better interpretation at the cost of a +little more bias. +

    + +

    The so-called Cost complexity pruning algorithm gives us a +way to do just this. Rather than considering every possible subtree, +we consider a sequence of trees indexed by a nonnegative tuning +parameter \( \alpha \). +

    + +

    Read more at the following Scikit-Learn link on pruning.

    +
    + +
    +

    Cost complexity pruning

    + +

    For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that

    +

     
    +$$ +\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T}, +$$ +

     
    + +

    is as small as possible. Here \( \overline{T} \) is +the number of terminal nodes of the tree \( T \) , \( R_m \) is the +rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node. +

    + +

    The tuning parameter \( \alpha \) controls a trade-off between the subtree’s +complexity and its fit to the training data. When \( \alpha = 0 \), then the +subtree \( T \) will simply equal \( T_0 \), +because then the above equation just measures the +training error. +However, as \( \alpha \) increases, there is a price to pay for +having a tree with many terminal nodes. The above equation will +tend to be minimized for a smaller subtree. +

    + +

    It turns out that as we increase \( \alpha \) from zero +branches get pruned from the tree in a nested and predictable fashion, +so obtaining the whole sequence of subtrees as a function of \( \alpha \) is +easy. We can select a value of \( \alpha \) using a validation set or using +cross-validation. We then return to the full data set and obtain the +subtree corresponding to \( \alpha \). +

    +
    + +
    +

    Schematic Regression Procedure

    + +
    +Building a Regression Tree +

    + +

      +

    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.
    2. +

    3. Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \( \alpha \).
    4. +

    5. 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:
    6. +
        + +

      • repeat steps 1 and 2 on all but the \( k \)-th fold of the training data.
      • + +

      • Then we valuate the mean squared prediction error on the data in the left-out \( k \)-th fold, as a function of \( \alpha \).
      • + +

      • Finally we average the results for each value of \( \alpha \), and pick \( \alpha \) to minimize the average error.

      +

    7. Return the subtree from Step 2 that corresponds to the chosen value of \( \alpha \).
    +
    +

    A Classification Tree

    + +

    A classification tree is very similar to a regression tree, except +that it is used to predict a qualitative response rather than a +quantitative one. Recall that for a regression tree, the predicted +response for an observation is given by the mean response of the +training observations that belong to the same terminal node. In +contrast, for a classification tree, we predict that each observation +belongs to the most commonly occurring class of training observations +in the region to which it belongs. In interpreting the results of a +classification tree, we are often interested not only in the class +prediction corresponding to a particular terminal node region, but +also in the class proportions among the training observations that +fall into that region. +

    +
    + +
    +

    Growing a classification tree

    + +

    The task of growing a +classification tree is quite similar to the task of growing a +regression tree. Just as in the regression setting, we use recursive +binary splitting to grow a classification tree. However, in the +classification setting, the MSE cannot be used as a criterion for making +the binary splits. A natural alternative to MSE is the classification +error rate. Since we plan to assign an observation in a given region +to the most commonly occurring error rate class of training +observations in that region, the classification error rate is simply +the fraction of the training observations in that region that do not +belong to the most common class. +

    + +

    When building a classification tree, either the Gini index or the +entropy are typically used to evaluate the quality of a particular +split, since these two approaches are more sensitive to node purity +than is the classification error rate. +

    +
    + +
    +

    Classification tree, how to split nodes

    + +

    If our targets are the outcome of a classification process that takes +for example \( k=1,2,\dots,K \) values, the only thing we need to think of +is to set up the splitting criteria for each node. +

    + +

    We define a PDF \( p_{mk} \) that represents the number of observations of +a class \( k \) in a region \( R_m \) with \( N_m \) observations. We represent +this likelihood function in terms of the proportion \( I(y_i=k) \) of +observations of this class in the region \( R_m \) as +

    + +

     
    +$$ +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i=k). +$$ +

     
    + +

    We let \( p_{mk} \) represent the majority class of observations in region +\( m \). The three most common ways of splitting a node are given by +

    + +
      +

    • Misclassification error
    • +
    +

    +

     
    +$$ +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i\ne k) = 1-p_{mk}. +$$ +

     
    + +

      +

    • Gini index \( g \)
    • +
    +

    +

     
    +$$ +g = \sum_{k=1}^K p_{mk}(1-p_{mk}). +$$ +

     
    + +

      +

    • Information entropy or just entropy \( s \)
    • +
    +

    +

     
    +$$ +s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}. +$$ +

     
    +

    + +
    +

    Visualizing the Tree, Classification

    + + +
    +
    +
    +
    +
    +
    import os
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.metrics import confusion_matrix
    +from sklearn.tree import export_graphviz
    +
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import numpy as np
    +
    +
    +cancer = load_breast_cancer()
    +X = pd.DataFrame(cancer.data, columns=cancer.feature_names)
    +print(X)
    +y = pd.Categorical.from_codes(cancer.target, cancer.target_names)
    +y = pd.get_dummies(y)
    +print(y)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
    +tree_clf = DecisionTreeClassifier(max_depth=5)
    +tree_clf.fit(X_train, y_train)
    +
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/cancer.dot",
    +    feature_names=cancer.feature_names,
    +    class_names=cancer.target_names,
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Visualizing the Tree, The Moons

    + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.datasets import make_moons
    +from sklearn.tree import export_graphviz
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import os
    +
    +np.random.seed(42)
    +X, y = make_moons(n_samples=100, noise=0.25, random_state=53)
    +X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0)
    +tree_clf = DecisionTreeClassifier(max_depth=5)
    +tree_clf.fit(X_train, y_train)
    +
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/moons.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Other ways of visualizing the trees

    + +

    Scikit-Learn has also another way to visualize the trees which is very useful, here with the Iris data.

    + + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn import tree
    +X, y = load_iris(return_X_y=True)
    +tree_clf = tree.DecisionTreeClassifier()
    +tree_clf = tree_clf.fit(X, y)
    +# and then plot the tree
    +tree.plot_tree(tree_clf) 
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Printing out as text

    + +

    Alternatively, the tree can also be exported in textual format with the function exporttext. +This method doesn’t require the installation of external libraries and is more compact: +

    + + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.tree import export_text
    +iris = load_iris()
    +decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
    +decision_tree = decision_tree.fit(iris.data, iris.target)
    +r = export_text(decision_tree, feature_names=iris['feature_names'])
    +print(r)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Algorithms for Setting up Decision Trees

    + +

    Two algorithms stand out in the set up of decision trees:

    +
      +

    1. The CART (Classification And Regression Tree) algorithm for both classification and regression
    2. +

    3. The ID3 algorithm based on the computation of the information gain for classification
    4. +
    +

    +

    We discuss both algorithms with applications here. The popular library +Scikit-Learn uses the CART algorithm. For classification problems +you can use either the gini index or the entropy to split a tree +in two branches. +

    +
    + +
    +

    The CART algorithm for Classification

    + +

    For classification, the CART algorithm splits the data set in two subsets using a single feature \( k \) and a threshold \( t_k \). +This could be for example a threshold set by a number below a certain circumference of a malign tumor. +

    + +

    How do we find these two quantities? +We search for the pair \( (k,t_k) \) that produces the purest subset using for example the gini factor \( G \). +The cost function it tries to minimize is then +

    +

     
    +$$ +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}G_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}G_{\mathrm{right}}, +$$ +

     
    + +

    where \( G_{\mathrm{left/right}} \) measures the impurity of the left/right subset and \( m_{\mathrm{left/right}} \) + is the number of instances in the left/right subset +

    + +

    Once it has successfully split the training set in two, it splits the subsets using the same logic, then the subsubsets +and so on, recursively. It stops recursing once it reaches the maximum depth (defined by the +\( max\_depth \) hyperparameter), or if it cannot find a split that will reduce impurity. A few other +hyperparameters control additional stopping conditions such as the \( min\_samples\_split \), +\( min\_samples\_leaf \), \( min\_weight\_fraction\_leaf \), and \( max\_leaf\_nodes \). +

    +
    + +
    +

    The CART algorithm for Regression

    + +

    The CART algorithm for regression works is similar to the one for classification except that instead of trying to split the +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 +

    +

     
    +$$ +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}\mathrm{MSE}_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}\mathrm{MSE}_{\mathrm{right}}. +$$ +

     
    + +

    Here the MSE for a specific node is defined as

    +

     
    +$$ +\mathrm{MSE}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}(\overline{y}_{\mathrm{node}}-y_i)^2, +$$ +

     
    + +

    with

    +

     
    +$$ +\overline{y}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}y_i, +$$ +

     
    + +

    the mean value of all observations in a specific node.

    + +

    Without any regularization, the regression task for decision trees, +just like for classification tasks, is prone to overfitting. +

    +
    + +
    +

    Why binary splits?

    + +

    It is custom to split to a tree uising binary splits. The reason is +that multiway splits fragment the data too quickly, leaving +insufficient data at the next level down. Multiway splits can be +achieved by a series of binary split and this is normally preferred. +

    +
    + +
    +

    Computing a Tree using the Gini Index

    + +

    Consider the following example with attributes/features and two +possible outcomes (classes) for each attribute. Assume we wish to find some +correlations between the average grade of a student as function of the +number of hours studied and hours slept. We want also to correlate the +grade in a given course with the general trend, whether the students +recently has gotten grades below average or above. +

    + +

    We have three features/attributes

    +
      +

    1. Trend of average grades before present course, classified as either below or above the average grade of the whole class
    2. + +

    3. The number of hours studies, classified again as either higher (more than 3 hours per day) or lower . Here we have used a standard for one \( ECTS \) which is scaled to 25-30 hours of work for a semester which lasts 18 weeks, with 15 weeks of lectures and 3 weeks for exams, assuming a total of 30 ECTS per semester.
    4. +

    5. The number of hours slept as high for more than \( 8 \) hours and below for less than 8 hours of sleep, classified again as either high or low
    6. +

    7. The final grade whether it is above or below average
    8. +
    +
    + +
    +

    The Table

    + + + + + + + + + + + + + + + + + +
    Grade Trend Hours slept Hours Studied Grade
    Above Low High Above
    Below High Low Below
    Above Low High Above
    Above High High Above
    Below Low High Below
    Above Low Low Below
    Below High High Below
    Below Low High Below
    Above Low Low Below
    Above High High Above
    +
    + +
    +

    Computing the various Gini Indices

    + +

    In computations we will translate all classes into numbers. Being +these binary classes, they can easily be split into ones and zeros. +

    + +
    +Gini index for Average trend +

    +

    See whiteboard notes from lecture November 11 at https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf

    +
    +
    + +
    +

    A possible code using Scikit-Learn

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +infile = open(data_path("grades.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +grades = pd.read_csv(infile)
    +grades = pd.DataFrame(grades)
    +display(grades)
    +# Features and targets
    +X = grades.loc[:, grades.columns != 'Grade'].values
    +y = grades.loc[:, grades.columns == 'Grade'].values
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/grade.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Further example: Computing the Gini index

    + +

    The next example we will look at is a classical one in many Machine +Learning applications. Based on various meteorological features, we +have several so-called attributes which decide whether we at the end +will do some outdoor activity like skiing, going for a bike ride etc +etc. The table here contains the feautures outlook, temperature, +humidity and wind. The target or output is whether we ride +(True=1) or whether we do something else that day (False=0). The +attributes for each feature are then sunny, overcast and rain for the +outlook, hot, cold and mild for temperature, high and normal for +humidity and weak and strong for wind. +

    + +

    The table here summarizes the various attributes and

    + + + + + + + + + + + + + + + + + + + + +
    Day Outlook Temperature Humidity Wind Ride
    1 Sunny Hot High Weak 0
    2 Sunny Hot High Strong 1
    3 Overcast Hot High Weak 1
    4 Rain Mild High Weak 1
    5 Rain Cool Normal Weak 1
    6 Rain Cool Normal Strong 0
    7 Overcast Cool Normal Strong 1
    8 Sunny Mild High Weak 0
    9 Sunny Cool Normal Weak 1
    10 Rain Mild Normal Weak 1
    11 Sunny Mild Normal Strong 1
    12 Overcast Mild High Strong 1
    13 Overcast Hot Normal Weak 1
    14 Rain Mild High Strong 0
    +
    + +
    +

    Simple Python Code to read in Data and perform Classification

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +infile = open(data_path("rideclass.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +ridedata = pd.read_csv(infile,names = ('Outlook','Temperature','Humidity','Wind','Ride'))
    +ridedata = pd.DataFrame(ridedata)
    +
    +# Features and targets
    +X = ridedata.loc[:, ridedata.columns != 'Ride'].values
    +y = ridedata.loc[:, ridedata.columns == 'Ride'].values
    +
    +# Create the encoder.
    +encoder = OneHotEncoder(handle_unknown="ignore")
    +# Assume for simplicity all features are categorical.
    +encoder.fit(X)    
    +# Apply the encoder.
    +X = encoder.transform(X)
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/ride.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Computing the Gini Factor

    + +

    The above functions (gini, entropy and misclassification error) are +important components of the so-called CART algorithm. We will discuss +this algorithm below after we have discussed the information gain +algorithm ID3. +

    + +

    In the example here we have converted all our attributes into numerical values \( 0,1,2 \) etc.

    + + + +
    +
    +
    +
    +
    +
    # Split a dataset based on an attribute and an attribute value
    +def test_split(index, value, dataset):
    +	left, right = list(), list()
    +	for row in dataset:
    +		if row[index] < value:
    +			left.append(row)
    +		else:
    +			right.append(row)
    +	return left, right
    + 
    +# Calculate the Gini index for a split dataset
    +def gini_index(groups, classes):
    +	# count all samples at split point
    +	n_instances = float(sum([len(group) for group in groups]))
    +	# sum weighted Gini index for each group
    +	gini = 0.0
    +	for group in groups:
    +		size = float(len(group))
    +		# avoid divide by zero
    +		if size == 0:
    +			continue
    +		score = 0.0
    +		# score the group based on the score for each class
    +		for class_val in classes:
    +			p = [row[-1] for row in group].count(class_val) / size
    +			score += p * p
    +		# weight the group score by its relative size
    +		gini += (1.0 - score) * (size / n_instances)
    +	return gini
    +
    +# Select the best split point for a dataset
    +def get_split(dataset):
    +	class_values = list(set(row[-1] for row in dataset))
    +	b_index, b_value, b_score, b_groups = 999, 999, 999, None
    +	for index in range(len(dataset[0])-1):
    +		for row in dataset:
    +			groups = test_split(index, row[index], dataset)
    +			gini = gini_index(groups, class_values)
    +			print('X%d < %.3f Gini=%.3f' % ((index+1), row[index], gini))
    +			if gini < b_score:
    +				b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    +	return {'index':b_index, 'value':b_value, 'groups':b_groups}
    + 
    +dataset = [[0,0,0,0,0],
    +            [0,0,0,1,1],
    +            [1,0,0,0,1],
    +            [2,1,0,0,1],
    +            [2,2,1,0,1],
    +            [2,2,1,1,0],
    +            [1,2,1,1,1],
    +            [0,1,0,0,0],
    +            [0,2,1,0,1],
    +            [2,1,1,0,1],
    +            [0,1,1,1,1],
    +            [1,1,0,1,1],
    +            [1,0,1,0,1],
    +            [2,1,0,1,0]]
    +
    +split = get_split(dataset)
    +print('Split: [X%d < %.3f]' % ((split['index']+1), split['value']))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Regression trees

    + + +
    +
    +
    +
    +
    +
    # Quadratic training set + noise
    +np.random.seed(42)
    +m = 200
    +X = np.random.rand(m, 1)
    +y = 4 * (X - 0.5) ** 2
    +y = y + np.random.randn(m, 1) / 10
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42)
    +tree_reg.fit(X, y)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Final regressor code

    + + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg1 = DecisionTreeRegressor(random_state=42, max_depth=2)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, max_depth=3)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +def plot_regression_predictions(tree_reg, X, y, axes=[0, 1, -0.2, 1], ylabel="$y$"):
    +    x1 = np.linspace(axes[0], axes[1], 500).reshape(-1, 1)
    +    y_pred = tree_reg.predict(x1)
    +    plt.axis(axes)
    +    plt.xlabel("$x_1$", fontsize=18)
    +    if ylabel:
    +        plt.ylabel(ylabel, fontsize=18, rotation=0)
    +    plt.plot(X, y, "b.")
    +    plt.plot(x1, y_pred, "r.-", linewidth=2, label=r"$\hat{y}$")
    +
    +plt.figure(figsize=(11, 4))
    +plt.subplot(121)
    +plot_regression_predictions(tree_reg1, X, y)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +plt.text(0.21, 0.65, "Depth=0", fontsize=15)
    +plt.text(0.01, 0.2, "Depth=1", fontsize=13)
    +plt.text(0.65, 0.8, "Depth=1", fontsize=13)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("max_depth=2", fontsize=14)
    +
    +plt.subplot(122)
    +plot_regression_predictions(tree_reg2, X, y, ylabel=None)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +for split in (0.0458, 0.1298, 0.2873, 0.9040):
    +    plt.plot([split, split], [-0.2, 1], "k:", linewidth=1)
    +plt.text(0.3, 0.5, "Depth=2", fontsize=13)
    +plt.title("max_depth=3", fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    tree_reg1 = DecisionTreeRegressor(random_state=42)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +x1 = np.linspace(0, 1, 500).reshape(-1, 1)
    +y_pred1 = tree_reg1.predict(x1)
    +y_pred2 = tree_reg2.predict(x1)
    +
    +plt.figure(figsize=(11, 4))
    +
    +plt.subplot(121)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred1, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.ylabel("$y$", fontsize=18, rotation=0)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("No restrictions", fontsize=14)
    +
    +plt.subplot(122)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred2, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.title("min_samples_leaf={}".format(tree_reg2.min_samples_leaf), fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Pros and cons of trees, pros

    + +
      +

    • 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)
    • +

    • Trees are very easy to explain to people. In fact, they are even easier to explain than linear regression!
    • +

    • No feature normalization needed
    • +

    • Tree models can handle both continuous and categorical data (Classification and Regression Trees)
    • +

    • Can model nonlinear relationships
    • +

    • Can model interactions between the different descriptive features
    • +

    • Trees can be displayed graphically, and are easily interpreted even by a non-expert (especially if they are small)
    • +
    +
    + +
    +

    Disadvantages

    + +
      +

    • Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches
    • +

    • If continuous features are used the tree may become quite large and hence less interpretable
    • +

    • 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
    • +

    • 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
    • +

    • 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.
    • +

    • If the number of features is relatively large (high dimensional) and the number of instances is relatively low, the tree might overfit the data
    • +

    • 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
    • +
    +

    +

    However, by aggregating many decision trees, using methods like +bagging, random forests, and boosting, the predictive performance of +trees can be substantially improved. +

    +
    + +
    +

    Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods

    + +

    As stated above and seen in many of the examples discussed here about +a single decision tree, we often end up overfitting our training +data. This normally means that we have a high variance. Can we reduce +the variance of a statistical learning method? +

    + +

    This leads us to a set of different methods that can combine different +machine learning algorithms or just use one of them to construct +forests and jungles of trees, homogeneous ones or heterogenous +ones. These methods are recognized by different names which we will +try to explain here. These are +

    + +
      +

    1. Voting classifiers
    2. +

    3. Bagging and Pasting
    4. +

    5. Random forests
    6. +

    7. Boosting methods, from adaptive to Extreme Gradient Boosting (XGBoost)
    8. +
    +

    +

    We discuss these methods here.

    +
    + +
    +

    An Overview of Ensemble Methods

    + +

    +
    +

    +
    +

    +
    + +
    +

    Why Voting?

    + +

    The idea behind boosting, and voting as well can be phrased as follows: +Can a group of people somehow arrive at highly +reasoned decisions, despite the weak judgement of the individual +members? +

    + +

    The aim is to create a good classifier by combining several weak classifiers. +A weak classifier is a classifier which is able to produce results that are only slightly better than guessing at random. +

    + +

    The basic approach is to apply repeatedly (in boosting this is done in an iterative way) a weak classifier to modifications of the data. +In voting we simply apply the law of large numbers while in boosting we give more weight to misclassified data in +each iteration. +

    + +

    Decision trees play an important role as our weak classifier. They serve as the basic method.

    +
    + +
    +

    Tossing coins

    + +

    The simplest case is a so-called voting ensemble. To illustrate this, +think of yourself tossing coins with a biased outcome of 51 per cent +for heads and 49% for tails. With only few tosses, +you may not clearly see this distribution for heads and tails. However, after some +thousands of tosses, there will be a clear majority of heads. With 2000 tosses +you should see approximately 1020 heads and 980 tails. +

    + +

    We can then state that the outcome is a clear majority of heads. If +you do this ten thousand times, it is easy to see that there is a 97% +likelihood of a majority of heads. +

    + +

    Another example would be to collect all polls before an +election. Different polls may show different likelihoods for a +candidate winning with say a majority of the popular vote. The majority vote +would then consist in many polls indicating that this candidate will +actually win. +

    + +

    The example here shows how we can implement the coin tossing case, +clealry demostrating that after some tosses we see the law of large +numbers kicking in. +

    +
    + +
    +

    Standard imports first

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Simple Voting Example, head or tail

    + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import matplotlib
    +import matplotlib.pyplot as plt
    +from matplotlib.colors import ListedColormap
    +plt.rcParams['axes.labelsize'] = 14
    +plt.rcParams['xtick.labelsize'] = 12
    +plt.rcParams['ytick.labelsize'] = 12
    +
    +heads_proba = 0.51
    +coin_tosses = (np.random.rand(10000, 10) < heads_proba).astype(np.int32)
    +cumulative_heads_ratio = np.cumsum(coin_tosses, axis=0) / np.arange(1, 10001).reshape(-1, 1)
    +plt.figure(figsize=(8,3.5))
    +plt.plot(cumulative_heads_ratio)
    +plt.plot([0, 10000], [0.51, 0.51], "k--", linewidth=2, label="51%")
    +plt.plot([0, 10000], [0.5, 0.5], "k-", label="50%")
    +plt.xlabel("Number of coin tosses")
    +plt.ylabel("Heads ratio")
    +plt.legend(loc="lower right")
    +plt.axis([0, 10000, 0.42, 0.58])
    +save_fig("votingsimple")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Using the Voting Classifier

    + +

    We can use the voting classifier on other data sets, here the exciting binary case of two distinct objects using the make moons functionality of Scikit-Learn.

    + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", probability=True, random_state=42)
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Voting and Bagging

    + + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(probability=True, random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +

    Bagging

    + +

    The plain decision trees suffer from high +variance. This means that if we split the training data into two parts +at random, and fit a decision tree to both halves, the results that we +get could be quite different. In contrast, a procedure with low +variance will yield similar results if applied repeatedly to distinct +data sets; linear regression tends to have low variance, if the ratio +of \( n \) to \( p \) is moderately large. +

    + +

    Bootstrap aggregation, or just bagging, is a +general-purpose procedure for reducing the variance of a statistical +learning method. +

    +
    + +
    +

    More bagging

    + +

    Bagging typically results in improved accuracy +over prediction using a single tree. Unfortunately, however, it can be +difficult to interpret the resulting model. Recall that one of the +advantages of decision trees is the attractive and easily interpreted +diagram that results. +

    + +

    However, when we bag a large number of trees, it is no longer +possible to represent the resulting statistical learning procedure +using a single tree, and it is no longer clear which variables are +most important to the procedure. Thus, bagging improves prediction +accuracy at the expense of interpretability. Although the collection +of bagged trees is much more difficult to interpret than a single +tree, one can obtain an overall summary of the importance of each +predictor using the MSE (for bagging regression trees) or the Gini +index (for bagging classification trees). In the case of bagging +regression trees, we can record the total amount that the MSE is +decreased due to splits over a given predictor, averaged over all \( B \) possible +trees. A large value indicates an important predictor. Similarly, in +the context of bagging classification trees, we can add up the total +amount that the Gini index is decreased by splits over a given +predictor, averaged over all \( B \) trees. +

    +
    + +
    +

    Making your own Bootstrap: Changing the Level of the Decision Tree

    + +

    Let us bring up our good old boostrap example from the linear regression lectures. We change the linerar regression algorithm with +a decision tree wth different depths and perform a bootstrap aggregate (in this case we perform as many bootstraps as data points \( n \)). +

    + + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import train_test_split
    +from sklearn.pipeline import make_pipeline
    +from sklearn.utils import resample
    +from sklearn.tree import DecisionTreeRegressor
    +
    +n = 100
    +n_boostraps = 100
    +maxdepth = 8
    +
    +# Make data set.
    +x = np.linspace(-3, 3, n).reshape(-1, 1)
    +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
    +error = np.zeros(maxdepth)
    +bias = np.zeros(maxdepth)
    +variance = np.zeros(maxdepth)
    +polydegree = np.zeros(maxdepth)
    +X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    +
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +# we produce a simple tree first as benchmark
    +simpletree = DecisionTreeRegressor(max_depth=3) 
    +simpletree.fit(X_train_scaled, y_train)
    +simpleprediction = simpletree.predict(X_test_scaled)
    +for degree in range(1,maxdepth):
    +    model = DecisionTreeRegressor(max_depth=degree) 
    +    y_pred = np.empty((y_test.shape[0], n_boostraps))
    +    for i in range(n_boostraps):
    +        x_, y_ = resample(X_train_scaled, y_train)
    +        model.fit(x_, y_)
    +        y_pred[:, i] = model.predict(X_test_scaled)#.ravel()
    +
    +    polydegree[degree] = degree
    +    error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
    +    bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
    +    variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
    +    print('Polynomial degree:', degree)
    +    print('Error:', error[degree])
    +    print('Bias^2:', bias[degree])
    +    print('Var:', variance[degree])
    +    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
    + 
    +mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**2))
    +print("Simple tree:",mse_simpletree)
    +plt.xlim(1,maxdepth)
    +plt.plot(polydegree, error, label='MSE')
    +plt.plot(polydegree, bias, label='bias')
    +plt.plot(polydegree, variance, label='Variance')
    +plt.legend()
    +save_fig("baggingboot")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    Random forests

    @@ -836,6 +2462,110 @@ plt.show()
    +
    +

    Making an ADAboost code yourself

    + + + +
    +
    +
    +
    +
    +
    import numpy as np
    +
    +class DecisionStump:
    +    def fit(self, X, y, weights):
    +        m, n = X.shape
    +        self.alpha = 0
    +        self.threshold = None
    +        self.polarity = 1
    +
    +        min_error = float('inf')
    +
    +        for feature in range(n):
    +            feature_values = np.unique(X[:, feature])
    +
    +            for threshold in feature_values:
    +                for polarity in [1, -1]:
    +                    predictions = np.ones(m)
    +                    predictions[X[:, feature] < threshold] = -1
    +                    predictions *= polarity
    +
    +                    error = sum(weights[predictions != y])
    +
    +                    if error < min_error:
    +                        min_error = error
    +                        self.alpha = 0.5 * np.log((1 - error) / (error + 1e-10))
    +                        self.threshold = threshold
    +                        self.feature_index = feature
    +                        self.polarity = polarity
    +
    +    def predict(self, X):
    +        m = X.shape[0]
    +        predictions = np.ones(m)
    +        if self.polarity == 1:
    +            predictions[X[:, self.feature_index] < self.threshold] = -1
    +        else:
    +            predictions[X[:, self.feature_index] >= self.threshold] = -1
    +        return predictions
    +
    +class AdaBoost:
    +    def fit(self, X, y, n_estimators):
    +        m = X.shape[0]
    +        self.alphas = []
    +        self.models = []
    +
    +        weights = np.ones(m) / m
    +
    +        for _ in range(n_estimators):
    +            stump = DecisionStump()
    +            stump.fit(X, y, weights)
    +            predictions = stump.predict(X)
    +
    +            error = sum(weights[predictions != y])
    +            if error == 0:
    +                break
    +
    +            self.models.append(stump)
    +            self.alphas.append(stump.alpha)
    +
    +            weights *= np.exp(-stump.alpha * y * predictions)
    +            weights /= np.sum(weights)
    +
    +    def predict(self, X):
    +        final_predictions = np.zeros(X.shape[0])
    +        for alpha, model in zip(self.alphas, self.models):
    +            final_predictions += alpha * model.predict(X)
    +        return np.sign(final_predictions)
    +
    +# Example dataset (X, y)
    +X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
    +y = np.array([-1, -1, -1, -1, 1, 1, 1, 1, 1, 1])  # Labels must be -1 or 1
    +
    +# Train AdaBoost
    +ada = AdaBoost()
    +ada.fit(X, y, n_estimators=10)
    +
    +# Predictions
    +predictions = ada.predict(X)
    +print("Predictions:", predictions)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent

    @@ -1233,6 +2963,108 @@ plt.show()
    +
    +

    Gradient boosting, making our own code for a regression case

    + + + +
    +
    +
    +
    +
    +
    import numpy as np
    +class DecisionTreeRegressor:
    +    def __init__(self, max_depth=3):
    +        self.max_depth = max_depth
    +        self.tree = None
    +    def fit(self, X, y):
    +        self.tree = self._grow_tree(X, y)
    +    def _grow_tree(self, X, y, depth=0):
    +        n_samples, n_features = X.shape
    +        if depth < self.max_depth:
    +            best_feature, best_threshold = self._best_split(X, y)
    +            if best_feature is not None:
    +                left_indices = X[:, best_feature] < best_threshold
    +                right_indices = X[:, best_feature] >= best_threshold
    +                left_child = self._grow_tree(X[left_indices], y[left_indices], depth + 1)
    +                right_child = self._grow_tree(X[right_indices], y[right_indices], depth + 1)
    +                return (best_feature, best_threshold, left_child, right_child)
    +        return np.mean(y)
    +    def _best_split(self, X, y):
    +        best_mse = float('inf')
    +        best_feature, best_threshold = None, None
    +        n_samples, n_features = X.shape
    +        
    +        for feature in range(n_features):
    +            thresholds = np.unique(X[:, feature])
    +            for threshold in thresholds:
    +                left_indices = X[:, feature] < threshold
    +                right_indices = X[:, feature] >= threshold
    +                if len(y[left_indices]) > 0 and len(y[right_indices]) > 0:
    +                    left_mse = np.mean((y[left_indices] - np.mean(y[left_indices])) ** 2)
    +                    right_mse = np.mean((y[right_indices] - np.mean(y[right_indices])) ** 2)
    +                    mse = (len(y[left_indices]) * left_mse + len(y[right_indices]) * right_mse) / n_samples
    +                    
    +                    if mse < best_mse:
    +                        best_mse = mse
    +                        best_feature = feature
    +                        best_threshold = threshold
    +        return best_feature, best_threshold
    +    def predict(self, X):
    +        return np.array([self._predict_sample(sample, self.tree) for sample in X])
    +    def _predict_sample(self, sample, node):
    +        if isinstance(node, tuple):
    +            feature, threshold, left_child, right_child = node
    +            if sample[feature] < threshold:
    +                return self._predict_sample(sample, left_child)
    +            else:
    +                return self._predict_sample(sample, right_child)
    +        return node
    +class GradientBoostingRegressor:
    +    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
    +        self.n_estimators = n_estimators
    +        self.learning_rate = learning_rate
    +        self.max_depth = max_depth
    +        self.models = []
    +    def fit(self, X, y):
    +        y_pred = np.zeros(y.shape)
    +        for _ in range(self.n_estimators):
    +            residuals = y - y_pred
    +            model = DecisionTreeRegressor(max_depth=self.max_depth)
    +            model.fit(X, residuals)
    +            y_pred += self.learning_rate * model.predict(X)
    +            self.models.append(model)
    +    def predict(self, X):
    +        y_pred = np.zeros(X.shape[0])
    +        for model in self.models:
    +            y_pred += self.learning_rate * model.predict(X)
    +        return y_pred
    +# Example usage
    +if __name__ == "__main__":
    +    # Sample data
    +    X = np.array([[1], [2], [3], [4], [5]])
    +    y = np.array([1.5, 1.7, 3.5, 3.7, 5.0])
    +    model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=2)
    +    model.fit(X, y)
    +    predictions = model.predict(X)
    +    print("Predictions:", predictions)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    diff --git a/doc/pub/week47/html/week47-solarized.html b/doc/pub/week47/html/week47-solarized.html index 15f03840e..c2ccd0705 100644 --- a/doc/pub/week47/html/week47-solarized.html +++ b/doc/pub/week47/html/week47-solarized.html @@ -64,6 +64,116 @@ div.toc p,a { @@ -191,22 +309,1591 @@ MathJax.Hub.Config({
    -Material for the lecture Monday 18 November +Plans for the lecture Monday 18 November, with video suggestions etc

    1. Basics of decision trees, classification and regression algorithms and ensemble models
    2. -
    3. Readings and Videos:
    4. - +
    5. Video on Decision trees https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn
    6. +
    7. Video on boosting methods https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai
    8. +
    9. Video on AdaBoost https://www.youtube.com/watch?v=LsK-xG1cLYA
    10. +
    11. Video on Gradient boost, part 1, parts 2-4 follow thereafter https://www.youtube.com/watch?v=3CC4N4z3GJc
    12. +
    13. Decision Trees: Rashcka et al chapter 3 pages 86-98, and chapter 7 on Ensemble methods, Voting and Bagging and Gradient Boosting. See also lecture from STK-IN4300, lecture 7 at https://www.uio.no/studier/emner/matnat/math/STK-IN4300/h20/slides/lecture_7.pdf.
    +
  • Decision Trees: Geron's chapter 6 covers decision trees while ensemble models, voting and bagging are discussed in chapter 7. See also lecture from STK-IN4300, lecture 7. Chapter 9.2 of Hastie et al contains also a good discussion.
  • + +
    + + +









    +

    Building a tree, regression

    + +

    There are mainly two steps

    +
      +
    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 \).
    2. +
    3. 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 \).
    4. +
    +

    How do we construct the regions \( R_1,\dots,R_J \)? In theory, the +regions could have any shape. However, we choose to divide the +predictor space into high-dimensional rectangles, or boxes, for +simplicity and for ease of interpretation of the resulting predictive +model. The goal is to find boxes \( R_1,\dots,R_J \) that minimize the +MSE, given by +

    + +$$ +\sum_{j=1}^J\sum_{i\in R_j}(y_i-\overline{y}_{R_j})^2, +$$ + +

    where \( \overline{y}_{R_j} \) is the mean response for the training observations +within box \( j \). +

    + +









    +

    A top-down approach, recursive binary splitting

    + +

    Unfortunately, it is computationally infeasible to consider every +possible partition of the feature space into \( J \) boxes. The common +strategy is to take a top-down approach +

    + +

    The approach is top-down because it begins at the top of the tree (all +observations belong to a single region) and then successively splits +the predictor space; each split is indicated via two new branches +further down on the tree. It is greedy because at each step of the +tree-building process, the best split is made at that particular step, +rather than looking ahead and picking a split that will lead to a +better tree in some future step. +

    + +









    +

    Making a tree

    + +

    In order to implement the recursive binary splitting we start by selecting +the predictor \( x_j \) and a cutpoint \( s \) that splits the predictor space into two regions \( R_1 \) and \( R_2 \) +

    +$$ +\left\{X\vert x_j < s\right\}, +$$ + +

    and

    +$$ +\left\{X\vert x_j \geq s\right\}, +$$ + +

    so that we obtain the lowest MSE, that is

    +$$ +\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, +$$ + +

    which we want to minimize by considering all predictors +\( x_1,x_2,\dots,x_p \). We consider also all possible values of \( s \) for +each predictor. These values could be determined by randomly assigned +numbers or by starting at the midpoint and then proceed till we find +an optimal value. +

    + +

    For any \( j \) and \( s \), we define the pair of half-planes where +\( \overline{y}_{R_1} \) is the mean response for the training +observations in \( R_1(j,s) \), and \( \overline{y}_{R_2} \) is the mean +response for the training observations in \( R_2(j,s) \). +

    + +

    Finding the values of \( j \) and \( s \) that minimize the above equation can be +done quite quickly, especially when the number of features \( p \) is not +too large. +

    + +

    Next, we repeat the process, looking +for the best predictor and best cutpoint in order to split the data +further so as to minimize the MSE within each of the resulting +regions. However, this time, instead of splitting the entire predictor +space, we split one of the two previously identified regions. We now +have three regions. Again, we look to split one of these three regions +further, so as to minimize the MSE. The process continues until a +stopping criterion is reached; for instance, we may continue until no +region contains more than five observations. +

    + + +

    Pruning the tree

    + +

    The above procedure is rather straightforward, but leads often to +overfitting and unnecessarily large and complicated trees. The basic +idea is to grow a large tree \( T_0 \) and then prune it back in order to +obtain a subtree. A smaller tree with fewer splits (fewer regions) can +lead to smaller variance and better interpretation at the cost of a +little more bias. +

    + +

    The so-called Cost complexity pruning algorithm gives us a +way to do just this. Rather than considering every possible subtree, +we consider a sequence of trees indexed by a nonnegative tuning +parameter \( \alpha \). +

    + +

    Read more at the following Scikit-Learn link on pruning.

    + +









    +

    Cost complexity pruning

    + +

    For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that

    +$$ +\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T}, +$$ + +

    is as small as possible. Here \( \overline{T} \) is +the number of terminal nodes of the tree \( T \) , \( R_m \) is the +rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node. +

    + +

    The tuning parameter \( \alpha \) controls a trade-off between the subtree’s +complexity and its fit to the training data. When \( \alpha = 0 \), then the +subtree \( T \) will simply equal \( T_0 \), +because then the above equation just measures the +training error. +However, as \( \alpha \) increases, there is a price to pay for +having a tree with many terminal nodes. The above equation will +tend to be minimized for a smaller subtree. +

    + +

    It turns out that as we increase \( \alpha \) from zero +branches get pruned from the tree in a nested and predictable fashion, +so obtaining the whole sequence of subtrees as a function of \( \alpha \) is +easy. We can select a value of \( \alpha \) using a validation set or using +cross-validation. We then return to the full data set and obtain the +subtree corresponding to \( \alpha \). +

    + +









    +

    Schematic Regression Procedure

    + +
    +Building a Regression Tree +

    + +

      +
    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.
    2. +
    3. Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \( \alpha \).
    4. +
    5. 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:
    6. +
        +
      • repeat steps 1 and 2 on all but the \( k \)-th fold of the training data.
      • +
      • Then we valuate the mean squared prediction error on the data in the left-out \( k \)-th fold, as a function of \( \alpha \).
      • +
      • Finally we average the results for each value of \( \alpha \), and pick \( \alpha \) to minimize the average error.
      • +
      +
    7. Return the subtree from Step 2 that corresponds to the chosen value of \( \alpha \).
    8. +
    +
    + + +









    +

    A Classification Tree

    + +

    A classification tree is very similar to a regression tree, except +that it is used to predict a qualitative response rather than a +quantitative one. Recall that for a regression tree, the predicted +response for an observation is given by the mean response of the +training observations that belong to the same terminal node. In +contrast, for a classification tree, we predict that each observation +belongs to the most commonly occurring class of training observations +in the region to which it belongs. In interpreting the results of a +classification tree, we are often interested not only in the class +prediction corresponding to a particular terminal node region, but +also in the class proportions among the training observations that +fall into that region. +

    + +









    +

    Growing a classification tree

    + +

    The task of growing a +classification tree is quite similar to the task of growing a +regression tree. Just as in the regression setting, we use recursive +binary splitting to grow a classification tree. However, in the +classification setting, the MSE cannot be used as a criterion for making +the binary splits. A natural alternative to MSE is the classification +error rate. Since we plan to assign an observation in a given region +to the most commonly occurring error rate class of training +observations in that region, the classification error rate is simply +the fraction of the training observations in that region that do not +belong to the most common class. +

    + +

    When building a classification tree, either the Gini index or the +entropy are typically used to evaluate the quality of a particular +split, since these two approaches are more sensitive to node purity +than is the classification error rate. +

    + +









    +

    Classification tree, how to split nodes

    + +

    If our targets are the outcome of a classification process that takes +for example \( k=1,2,\dots,K \) values, the only thing we need to think of +is to set up the splitting criteria for each node. +

    + +

    We define a PDF \( p_{mk} \) that represents the number of observations of +a class \( k \) in a region \( R_m \) with \( N_m \) observations. We represent +this likelihood function in terms of the proportion \( I(y_i=k) \) of +observations of this class in the region \( R_m \) as +

    + +$$ +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i=k). +$$ + +

    We let \( p_{mk} \) represent the majority class of observations in region +\( m \). The three most common ways of splitting a node are given by +

    + +
      +
    • Misclassification error
    • +
    +$$ +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i\ne k) = 1-p_{mk}. +$$ + +
      +
    • Gini index \( g \)
    • +
    +$$ +g = \sum_{k=1}^K p_{mk}(1-p_{mk}). +$$ + +
      +
    • Information entropy or just entropy \( s \)
    • +
    +$$ +s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}. +$$ + + +









    +

    Visualizing the Tree, Classification

    + + +
    +
    +
    +
    +
    +
    import os
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.metrics import confusion_matrix
    +from sklearn.tree import export_graphviz
    +
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import numpy as np
    +
    +
    +cancer = load_breast_cancer()
    +X = pd.DataFrame(cancer.data, columns=cancer.feature_names)
    +print(X)
    +y = pd.Categorical.from_codes(cancer.target, cancer.target_names)
    +y = pd.get_dummies(y)
    +print(y)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
    +tree_clf = DecisionTreeClassifier(max_depth=5)
    +tree_clf.fit(X_train, y_train)
    +
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/cancer.dot",
    +    feature_names=cancer.feature_names,
    +    class_names=cancer.target_names,
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Visualizing the Tree, The Moons

    + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.datasets import make_moons
    +from sklearn.tree import export_graphviz
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import os
    +
    +np.random.seed(42)
    +X, y = make_moons(n_samples=100, noise=0.25, random_state=53)
    +X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0)
    +tree_clf = DecisionTreeClassifier(max_depth=5)
    +tree_clf.fit(X_train, y_train)
    +
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/moons.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Other ways of visualizing the trees

    + +

    Scikit-Learn has also another way to visualize the trees which is very useful, here with the Iris data.

    + + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn import tree
    +X, y = load_iris(return_X_y=True)
    +tree_clf = tree.DecisionTreeClassifier()
    +tree_clf = tree_clf.fit(X, y)
    +# and then plot the tree
    +tree.plot_tree(tree_clf) 
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Printing out as text

    + +

    Alternatively, the tree can also be exported in textual format with the function exporttext. +This method doesn’t require the installation of external libraries and is more compact: +

    + + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.tree import export_text
    +iris = load_iris()
    +decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
    +decision_tree = decision_tree.fit(iris.data, iris.target)
    +r = export_text(decision_tree, feature_names=iris['feature_names'])
    +print(r)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Algorithms for Setting up Decision Trees

    + +

    Two algorithms stand out in the set up of decision trees:

    +
      +
    1. The CART (Classification And Regression Tree) algorithm for both classification and regression
    2. +
    3. The ID3 algorithm based on the computation of the information gain for classification
    4. +
    +

    We discuss both algorithms with applications here. The popular library +Scikit-Learn uses the CART algorithm. For classification problems +you can use either the gini index or the entropy to split a tree +in two branches. +

    + +









    +

    The CART algorithm for Classification

    + +

    For classification, the CART algorithm splits the data set in two subsets using a single feature \( k \) and a threshold \( t_k \). +This could be for example a threshold set by a number below a certain circumference of a malign tumor. +

    + +

    How do we find these two quantities? +We search for the pair \( (k,t_k) \) that produces the purest subset using for example the gini factor \( G \). +The cost function it tries to minimize is then +

    +$$ +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}G_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}G_{\mathrm{right}}, +$$ + +

    where \( G_{\mathrm{left/right}} \) measures the impurity of the left/right subset and \( m_{\mathrm{left/right}} \) + is the number of instances in the left/right subset +

    + +

    Once it has successfully split the training set in two, it splits the subsets using the same logic, then the subsubsets +and so on, recursively. It stops recursing once it reaches the maximum depth (defined by the +\( max\_depth \) hyperparameter), or if it cannot find a split that will reduce impurity. A few other +hyperparameters control additional stopping conditions such as the \( min\_samples\_split \), +\( min\_samples\_leaf \), \( min\_weight\_fraction\_leaf \), and \( max\_leaf\_nodes \). +

    + +









    +

    The CART algorithm for Regression

    + +

    The CART algorithm for regression works is similar to the one for classification except that instead of trying to split the +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 +

    +$$ +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}\mathrm{MSE}_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}\mathrm{MSE}_{\mathrm{right}}. +$$ + +

    Here the MSE for a specific node is defined as

    +$$ +\mathrm{MSE}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}(\overline{y}_{\mathrm{node}}-y_i)^2, +$$ + +

    with

    +$$ +\overline{y}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}y_i, +$$ + +

    the mean value of all observations in a specific node.

    + +

    Without any regularization, the regression task for decision trees, +just like for classification tasks, is prone to overfitting. +

    + +









    +

    Why binary splits?

    + +

    It is custom to split to a tree uising binary splits. The reason is +that multiway splits fragment the data too quickly, leaving +insufficient data at the next level down. Multiway splits can be +achieved by a series of binary split and this is normally preferred. +

    + +









    +

    Computing a Tree using the Gini Index

    + +

    Consider the following example with attributes/features and two +possible outcomes (classes) for each attribute. Assume we wish to find some +correlations between the average grade of a student as function of the +number of hours studied and hours slept. We want also to correlate the +grade in a given course with the general trend, whether the students +recently has gotten grades below average or above. +

    + +

    We have three features/attributes

    +
      +
    1. Trend of average grades before present course, classified as either below or above the average grade of the whole class
    2. +
    3. The number of hours studies, classified again as either higher (more than 3 hours per day) or lower . Here we have used a standard for one \( ECTS \) which is scaled to 25-30 hours of work for a semester which lasts 18 weeks, with 15 weeks of lectures and 3 weeks for exams, assuming a total of 30 ECTS per semester.
    4. +
    5. The number of hours slept as high for more than \( 8 \) hours and below for less than 8 hours of sleep, classified again as either high or low
    6. +
    7. The final grade whether it is above or below average
    8. +
    +









    +

    The Table

    + + + + + + + + + + + + + + + + + +
    Grade Trend Hours slept Hours Studied Grade
    Above Low High Above
    Below High Low Below
    Above Low High Above
    Above High High Above
    Below Low High Below
    Above Low Low Below
    Below High High Below
    Below Low High Below
    Above Low Low Below
    Above High High Above
    + +









    +

    Computing the various Gini Indices

    + +

    In computations we will translate all classes into numbers. Being +these binary classes, they can easily be split into ones and zeros. +

    + +
    +Gini index for Average trend +

    +

    See whiteboard notes from lecture November 11 at https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf

    +
    + + +









    +

    A possible code using Scikit-Learn

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +infile = open(data_path("grades.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +grades = pd.read_csv(infile)
    +grades = pd.DataFrame(grades)
    +display(grades)
    +# Features and targets
    +X = grades.loc[:, grades.columns != 'Grade'].values
    +y = grades.loc[:, grades.columns == 'Grade'].values
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/grade.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Further example: Computing the Gini index

    + +

    The next example we will look at is a classical one in many Machine +Learning applications. Based on various meteorological features, we +have several so-called attributes which decide whether we at the end +will do some outdoor activity like skiing, going for a bike ride etc +etc. The table here contains the feautures outlook, temperature, +humidity and wind. The target or output is whether we ride +(True=1) or whether we do something else that day (False=0). The +attributes for each feature are then sunny, overcast and rain for the +outlook, hot, cold and mild for temperature, high and normal for +humidity and weak and strong for wind. +

    + +

    The table here summarizes the various attributes and

    + + + + + + + + + + + + + + + + + + + + +
    Day Outlook Temperature Humidity Wind Ride
    1 Sunny Hot High Weak 0
    2 Sunny Hot High Strong 1
    3 Overcast Hot High Weak 1
    4 Rain Mild High Weak 1
    5 Rain Cool Normal Weak 1
    6 Rain Cool Normal Strong 0
    7 Overcast Cool Normal Strong 1
    8 Sunny Mild High Weak 0
    9 Sunny Cool Normal Weak 1
    10 Rain Mild Normal Weak 1
    11 Sunny Mild Normal Strong 1
    12 Overcast Mild High Strong 1
    13 Overcast Hot Normal Weak 1
    14 Rain Mild High Strong 0
    + +









    +

    Simple Python Code to read in Data and perform Classification

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +infile = open(data_path("rideclass.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +ridedata = pd.read_csv(infile,names = ('Outlook','Temperature','Humidity','Wind','Ride'))
    +ridedata = pd.DataFrame(ridedata)
    +
    +# Features and targets
    +X = ridedata.loc[:, ridedata.columns != 'Ride'].values
    +y = ridedata.loc[:, ridedata.columns == 'Ride'].values
    +
    +# Create the encoder.
    +encoder = OneHotEncoder(handle_unknown="ignore")
    +# Assume for simplicity all features are categorical.
    +encoder.fit(X)    
    +# Apply the encoder.
    +X = encoder.transform(X)
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/ride.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Computing the Gini Factor

    + +

    The above functions (gini, entropy and misclassification error) are +important components of the so-called CART algorithm. We will discuss +this algorithm below after we have discussed the information gain +algorithm ID3. +

    + +

    In the example here we have converted all our attributes into numerical values \( 0,1,2 \) etc.

    + + + +
    +
    +
    +
    +
    +
    # Split a dataset based on an attribute and an attribute value
    +def test_split(index, value, dataset):
    +	left, right = list(), list()
    +	for row in dataset:
    +		if row[index] < value:
    +			left.append(row)
    +		else:
    +			right.append(row)
    +	return left, right
    + 
    +# Calculate the Gini index for a split dataset
    +def gini_index(groups, classes):
    +	# count all samples at split point
    +	n_instances = float(sum([len(group) for group in groups]))
    +	# sum weighted Gini index for each group
    +	gini = 0.0
    +	for group in groups:
    +		size = float(len(group))
    +		# avoid divide by zero
    +		if size == 0:
    +			continue
    +		score = 0.0
    +		# score the group based on the score for each class
    +		for class_val in classes:
    +			p = [row[-1] for row in group].count(class_val) / size
    +			score += p * p
    +		# weight the group score by its relative size
    +		gini += (1.0 - score) * (size / n_instances)
    +	return gini
    +
    +# Select the best split point for a dataset
    +def get_split(dataset):
    +	class_values = list(set(row[-1] for row in dataset))
    +	b_index, b_value, b_score, b_groups = 999, 999, 999, None
    +	for index in range(len(dataset[0])-1):
    +		for row in dataset:
    +			groups = test_split(index, row[index], dataset)
    +			gini = gini_index(groups, class_values)
    +			print('X%d < %.3f Gini=%.3f' % ((index+1), row[index], gini))
    +			if gini < b_score:
    +				b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    +	return {'index':b_index, 'value':b_value, 'groups':b_groups}
    + 
    +dataset = [[0,0,0,0,0],
    +            [0,0,0,1,1],
    +            [1,0,0,0,1],
    +            [2,1,0,0,1],
    +            [2,2,1,0,1],
    +            [2,2,1,1,0],
    +            [1,2,1,1,1],
    +            [0,1,0,0,0],
    +            [0,2,1,0,1],
    +            [2,1,1,0,1],
    +            [0,1,1,1,1],
    +            [1,1,0,1,1],
    +            [1,0,1,0,1],
    +            [2,1,0,1,0]]
    +
    +split = get_split(dataset)
    +print('Split: [X%d < %.3f]' % ((split['index']+1), split['value']))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Regression trees

    + + +
    +
    +
    +
    +
    +
    # Quadratic training set + noise
    +np.random.seed(42)
    +m = 200
    +X = np.random.rand(m, 1)
    +y = 4 * (X - 0.5) ** 2
    +y = y + np.random.randn(m, 1) / 10
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42)
    +tree_reg.fit(X, y)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Final regressor code

    + + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg1 = DecisionTreeRegressor(random_state=42, max_depth=2)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, max_depth=3)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +def plot_regression_predictions(tree_reg, X, y, axes=[0, 1, -0.2, 1], ylabel="$y$"):
    +    x1 = np.linspace(axes[0], axes[1], 500).reshape(-1, 1)
    +    y_pred = tree_reg.predict(x1)
    +    plt.axis(axes)
    +    plt.xlabel("$x_1$", fontsize=18)
    +    if ylabel:
    +        plt.ylabel(ylabel, fontsize=18, rotation=0)
    +    plt.plot(X, y, "b.")
    +    plt.plot(x1, y_pred, "r.-", linewidth=2, label=r"$\hat{y}$")
    +
    +plt.figure(figsize=(11, 4))
    +plt.subplot(121)
    +plot_regression_predictions(tree_reg1, X, y)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +plt.text(0.21, 0.65, "Depth=0", fontsize=15)
    +plt.text(0.01, 0.2, "Depth=1", fontsize=13)
    +plt.text(0.65, 0.8, "Depth=1", fontsize=13)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("max_depth=2", fontsize=14)
    +
    +plt.subplot(122)
    +plot_regression_predictions(tree_reg2, X, y, ylabel=None)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +for split in (0.0458, 0.1298, 0.2873, 0.9040):
    +    plt.plot([split, split], [-0.2, 1], "k:", linewidth=1)
    +plt.text(0.3, 0.5, "Depth=2", fontsize=13)
    +plt.title("max_depth=3", fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    tree_reg1 = DecisionTreeRegressor(random_state=42)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +x1 = np.linspace(0, 1, 500).reshape(-1, 1)
    +y_pred1 = tree_reg1.predict(x1)
    +y_pred2 = tree_reg2.predict(x1)
    +
    +plt.figure(figsize=(11, 4))
    +
    +plt.subplot(121)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred1, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.ylabel("$y$", fontsize=18, rotation=0)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("No restrictions", fontsize=14)
    +
    +plt.subplot(122)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred2, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.title("min_samples_leaf={}".format(tree_reg2.min_samples_leaf), fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Pros and cons of trees, pros

    + +
      +
    • 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)
    • +
    • Trees are very easy to explain to people. In fact, they are even easier to explain than linear regression!
    • +
    • No feature normalization needed
    • +
    • Tree models can handle both continuous and categorical data (Classification and Regression Trees)
    • +
    • Can model nonlinear relationships
    • +
    • Can model interactions between the different descriptive features
    • +
    • Trees can be displayed graphically, and are easily interpreted even by a non-expert (especially if they are small)
    • +
    +









    +

    Disadvantages

    + +
      +
    • Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches
    • +
    • If continuous features are used the tree may become quite large and hence less interpretable
    • +
    • 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
    • +
    • 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
    • +
    • 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.
    • +
    • If the number of features is relatively large (high dimensional) and the number of instances is relatively low, the tree might overfit the data
    • +
    • 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
    • +
    +

    However, by aggregating many decision trees, using methods like +bagging, random forests, and boosting, the predictive performance of +trees can be substantially improved. +

    + +









    +

    Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods

    + +

    As stated above and seen in many of the examples discussed here about +a single decision tree, we often end up overfitting our training +data. This normally means that we have a high variance. Can we reduce +the variance of a statistical learning method? +

    + +

    This leads us to a set of different methods that can combine different +machine learning algorithms or just use one of them to construct +forests and jungles of trees, homogeneous ones or heterogenous +ones. These methods are recognized by different names which we will +try to explain here. These are +

    + +
      +
    1. Voting classifiers
    2. +
    3. Bagging and Pasting
    4. +
    5. Random forests
    6. +
    7. Boosting methods, from adaptive to Extreme Gradient Boosting (XGBoost)
    8. +
    +

    We discuss these methods here.

    + +









    +

    An Overview of Ensemble Methods

    + +

    +
    +

    +
    +

    + +









    +

    Why Voting?

    + +

    The idea behind boosting, and voting as well can be phrased as follows: +Can a group of people somehow arrive at highly +reasoned decisions, despite the weak judgement of the individual +members? +

    + +

    The aim is to create a good classifier by combining several weak classifiers. +A weak classifier is a classifier which is able to produce results that are only slightly better than guessing at random. +

    + +

    The basic approach is to apply repeatedly (in boosting this is done in an iterative way) a weak classifier to modifications of the data. +In voting we simply apply the law of large numbers while in boosting we give more weight to misclassified data in +each iteration. +

    + +

    Decision trees play an important role as our weak classifier. They serve as the basic method.

    + +









    +

    Tossing coins

    + +

    The simplest case is a so-called voting ensemble. To illustrate this, +think of yourself tossing coins with a biased outcome of 51 per cent +for heads and 49% for tails. With only few tosses, +you may not clearly see this distribution for heads and tails. However, after some +thousands of tosses, there will be a clear majority of heads. With 2000 tosses +you should see approximately 1020 heads and 980 tails. +

    + +

    We can then state that the outcome is a clear majority of heads. If +you do this ten thousand times, it is easy to see that there is a 97% +likelihood of a majority of heads. +

    + +

    Another example would be to collect all polls before an +election. Different polls may show different likelihoods for a +candidate winning with say a majority of the popular vote. The majority vote +would then consist in many polls indicating that this candidate will +actually win. +

    + +

    The example here shows how we can implement the coin tossing case, +clealry demostrating that after some tosses we see the law of large +numbers kicking in. +

    + +









    +

    Standard imports first

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Simple Voting Example, head or tail

    + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import matplotlib
    +import matplotlib.pyplot as plt
    +from matplotlib.colors import ListedColormap
    +plt.rcParams['axes.labelsize'] = 14
    +plt.rcParams['xtick.labelsize'] = 12
    +plt.rcParams['ytick.labelsize'] = 12
    +
    +heads_proba = 0.51
    +coin_tosses = (np.random.rand(10000, 10) < heads_proba).astype(np.int32)
    +cumulative_heads_ratio = np.cumsum(coin_tosses, axis=0) / np.arange(1, 10001).reshape(-1, 1)
    +plt.figure(figsize=(8,3.5))
    +plt.plot(cumulative_heads_ratio)
    +plt.plot([0, 10000], [0.51, 0.51], "k--", linewidth=2, label="51%")
    +plt.plot([0, 10000], [0.5, 0.5], "k-", label="50%")
    +plt.xlabel("Number of coin tosses")
    +plt.ylabel("Heads ratio")
    +plt.legend(loc="lower right")
    +plt.axis([0, 10000, 0.42, 0.58])
    +save_fig("votingsimple")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Using the Voting Classifier

    + +

    We can use the voting classifier on other data sets, here the exciting binary case of two distinct objects using the make moons functionality of Scikit-Learn.

    + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", probability=True, random_state=42)
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Voting and Bagging

    + + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(probability=True, random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Bagging

    + +

    The plain decision trees suffer from high +variance. This means that if we split the training data into two parts +at random, and fit a decision tree to both halves, the results that we +get could be quite different. In contrast, a procedure with low +variance will yield similar results if applied repeatedly to distinct +data sets; linear regression tends to have low variance, if the ratio +of \( n \) to \( p \) is moderately large. +

    + +

    Bootstrap aggregation, or just bagging, is a +general-purpose procedure for reducing the variance of a statistical +learning method. +

    + +









    +

    More bagging

    + +

    Bagging typically results in improved accuracy +over prediction using a single tree. Unfortunately, however, it can be +difficult to interpret the resulting model. Recall that one of the +advantages of decision trees is the attractive and easily interpreted +diagram that results. +

    + +

    However, when we bag a large number of trees, it is no longer +possible to represent the resulting statistical learning procedure +using a single tree, and it is no longer clear which variables are +most important to the procedure. Thus, bagging improves prediction +accuracy at the expense of interpretability. Although the collection +of bagged trees is much more difficult to interpret than a single +tree, one can obtain an overall summary of the importance of each +predictor using the MSE (for bagging regression trees) or the Gini +index (for bagging classification trees). In the case of bagging +regression trees, we can record the total amount that the MSE is +decreased due to splits over a given predictor, averaged over all \( B \) possible +trees. A large value indicates an important predictor. Similarly, in +the context of bagging classification trees, we can add up the total +amount that the Gini index is decreased by splits over a given +predictor, averaged over all \( B \) trees. +

    + +









    +

    Making your own Bootstrap: Changing the Level of the Decision Tree

    + +

    Let us bring up our good old boostrap example from the linear regression lectures. We change the linerar regression algorithm with +a decision tree wth different depths and perform a bootstrap aggregate (in this case we perform as many bootstraps as data points \( n \)). +

    + + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import train_test_split
    +from sklearn.pipeline import make_pipeline
    +from sklearn.utils import resample
    +from sklearn.tree import DecisionTreeRegressor
    +
    +n = 100
    +n_boostraps = 100
    +maxdepth = 8
    +
    +# Make data set.
    +x = np.linspace(-3, 3, n).reshape(-1, 1)
    +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
    +error = np.zeros(maxdepth)
    +bias = np.zeros(maxdepth)
    +variance = np.zeros(maxdepth)
    +polydegree = np.zeros(maxdepth)
    +X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    +
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +# we produce a simple tree first as benchmark
    +simpletree = DecisionTreeRegressor(max_depth=3) 
    +simpletree.fit(X_train_scaled, y_train)
    +simpleprediction = simpletree.predict(X_test_scaled)
    +for degree in range(1,maxdepth):
    +    model = DecisionTreeRegressor(max_depth=degree) 
    +    y_pred = np.empty((y_test.shape[0], n_boostraps))
    +    for i in range(n_boostraps):
    +        x_, y_ = resample(X_train_scaled, y_train)
    +        model.fit(x_, y_)
    +        y_pred[:, i] = model.predict(X_test_scaled)#.ravel()
    +
    +    polydegree[degree] = degree
    +    error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
    +    bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
    +    variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
    +    print('Polynomial degree:', degree)
    +    print('Error:', error[degree])
    +    print('Bias^2:', bias[degree])
    +    print('Var:', variance[degree])
    +    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
    + 
    +mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**2))
    +print("Simple tree:",mse_simpletree)
    +plt.xlim(1,maxdepth)
    +plt.plot(polydegree, error, label='MSE')
    +plt.plot(polydegree, bias, label='bias')
    +plt.plot(polydegree, variance, label='Variance')
    +plt.legend()
    +save_fig("baggingboot")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    @@ -737,6 +2424,110 @@ plt.show()
    +









    +

    Making an ADAboost code yourself

    + + + +
    +
    +
    +
    +
    +
    import numpy as np
    +
    +class DecisionStump:
    +    def fit(self, X, y, weights):
    +        m, n = X.shape
    +        self.alpha = 0
    +        self.threshold = None
    +        self.polarity = 1
    +
    +        min_error = float('inf')
    +
    +        for feature in range(n):
    +            feature_values = np.unique(X[:, feature])
    +
    +            for threshold in feature_values:
    +                for polarity in [1, -1]:
    +                    predictions = np.ones(m)
    +                    predictions[X[:, feature] < threshold] = -1
    +                    predictions *= polarity
    +
    +                    error = sum(weights[predictions != y])
    +
    +                    if error < min_error:
    +                        min_error = error
    +                        self.alpha = 0.5 * np.log((1 - error) / (error + 1e-10))
    +                        self.threshold = threshold
    +                        self.feature_index = feature
    +                        self.polarity = polarity
    +
    +    def predict(self, X):
    +        m = X.shape[0]
    +        predictions = np.ones(m)
    +        if self.polarity == 1:
    +            predictions[X[:, self.feature_index] < self.threshold] = -1
    +        else:
    +            predictions[X[:, self.feature_index] >= self.threshold] = -1
    +        return predictions
    +
    +class AdaBoost:
    +    def fit(self, X, y, n_estimators):
    +        m = X.shape[0]
    +        self.alphas = []
    +        self.models = []
    +
    +        weights = np.ones(m) / m
    +
    +        for _ in range(n_estimators):
    +            stump = DecisionStump()
    +            stump.fit(X, y, weights)
    +            predictions = stump.predict(X)
    +
    +            error = sum(weights[predictions != y])
    +            if error == 0:
    +                break
    +
    +            self.models.append(stump)
    +            self.alphas.append(stump.alpha)
    +
    +            weights *= np.exp(-stump.alpha * y * predictions)
    +            weights /= np.sum(weights)
    +
    +    def predict(self, X):
    +        final_predictions = np.zeros(X.shape[0])
    +        for alpha, model in zip(self.alphas, self.models):
    +            final_predictions += alpha * model.predict(X)
    +        return np.sign(final_predictions)
    +
    +# Example dataset (X, y)
    +X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
    +y = np.array([-1, -1, -1, -1, 1, 1, 1, 1, 1, 1])  # Labels must be -1 or 1
    +
    +# Train AdaBoost
    +ada = AdaBoost()
    +ada.fit(X, y, n_estimators=10)
    +
    +# Predictions
    +predictions = ada.predict(X)
    +print("Predictions:", predictions)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +









    Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent

    @@ -1114,6 +2905,107 @@ plt.show()
    +









    +

    Gradient boosting, making our own code for a regression case

    + + + +
    +
    +
    +
    +
    +
    import numpy as np
    +class DecisionTreeRegressor:
    +    def __init__(self, max_depth=3):
    +        self.max_depth = max_depth
    +        self.tree = None
    +    def fit(self, X, y):
    +        self.tree = self._grow_tree(X, y)
    +    def _grow_tree(self, X, y, depth=0):
    +        n_samples, n_features = X.shape
    +        if depth < self.max_depth:
    +            best_feature, best_threshold = self._best_split(X, y)
    +            if best_feature is not None:
    +                left_indices = X[:, best_feature] < best_threshold
    +                right_indices = X[:, best_feature] >= best_threshold
    +                left_child = self._grow_tree(X[left_indices], y[left_indices], depth + 1)
    +                right_child = self._grow_tree(X[right_indices], y[right_indices], depth + 1)
    +                return (best_feature, best_threshold, left_child, right_child)
    +        return np.mean(y)
    +    def _best_split(self, X, y):
    +        best_mse = float('inf')
    +        best_feature, best_threshold = None, None
    +        n_samples, n_features = X.shape
    +        
    +        for feature in range(n_features):
    +            thresholds = np.unique(X[:, feature])
    +            for threshold in thresholds:
    +                left_indices = X[:, feature] < threshold
    +                right_indices = X[:, feature] >= threshold
    +                if len(y[left_indices]) > 0 and len(y[right_indices]) > 0:
    +                    left_mse = np.mean((y[left_indices] - np.mean(y[left_indices])) ** 2)
    +                    right_mse = np.mean((y[right_indices] - np.mean(y[right_indices])) ** 2)
    +                    mse = (len(y[left_indices]) * left_mse + len(y[right_indices]) * right_mse) / n_samples
    +                    
    +                    if mse < best_mse:
    +                        best_mse = mse
    +                        best_feature = feature
    +                        best_threshold = threshold
    +        return best_feature, best_threshold
    +    def predict(self, X):
    +        return np.array([self._predict_sample(sample, self.tree) for sample in X])
    +    def _predict_sample(self, sample, node):
    +        if isinstance(node, tuple):
    +            feature, threshold, left_child, right_child = node
    +            if sample[feature] < threshold:
    +                return self._predict_sample(sample, left_child)
    +            else:
    +                return self._predict_sample(sample, right_child)
    +        return node
    +class GradientBoostingRegressor:
    +    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
    +        self.n_estimators = n_estimators
    +        self.learning_rate = learning_rate
    +        self.max_depth = max_depth
    +        self.models = []
    +    def fit(self, X, y):
    +        y_pred = np.zeros(y.shape)
    +        for _ in range(self.n_estimators):
    +            residuals = y - y_pred
    +            model = DecisionTreeRegressor(max_depth=self.max_depth)
    +            model.fit(X, residuals)
    +            y_pred += self.learning_rate * model.predict(X)
    +            self.models.append(model)
    +    def predict(self, X):
    +        y_pred = np.zeros(X.shape[0])
    +        for model in self.models:
    +            y_pred += self.learning_rate * model.predict(X)
    +        return y_pred
    +# Example usage
    +if __name__ == "__main__":
    +    # Sample data
    +    X = np.array([[1], [2], [3], [4], [5]])
    +    y = np.array([1.5, 1.7, 3.5, 3.7, 5.0])
    +    model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=2)
    +    model.fit(X, y)
    +    predictions = model.predict(X)
    +    print("Predictions:", predictions)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    © 1999-2024, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license diff --git a/doc/pub/week47/html/week47.html b/doc/pub/week47/html/week47.html index 4c7da69dc..b67b71dd7 100644 --- a/doc/pub/week47/html/week47.html +++ b/doc/pub/week47/html/week47.html @@ -141,6 +141,116 @@ div.toc p,a { @@ -268,22 +386,1591 @@ MathJax.Hub.Config({
    -Material for the lecture Monday 18 November +Plans for the lecture Monday 18 November, with video suggestions etc

    1. Basics of decision trees, classification and regression algorithms and ensemble models
    2. -
    3. Readings and Videos:
    4. - +
    5. Video on Decision trees https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn
    6. +
    7. Video on boosting methods https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai
    8. +
    9. Video on AdaBoost https://www.youtube.com/watch?v=LsK-xG1cLYA
    10. +
    11. Video on Gradient boost, part 1, parts 2-4 follow thereafter https://www.youtube.com/watch?v=3CC4N4z3GJc
    12. +
    13. Decision Trees: Rashcka et al chapter 3 pages 86-98, and chapter 7 on Ensemble methods, Voting and Bagging and Gradient Boosting. See also lecture from STK-IN4300, lecture 7 at https://www.uio.no/studier/emner/matnat/math/STK-IN4300/h20/slides/lecture_7.pdf.
    +
  • Decision Trees: Geron's chapter 6 covers decision trees while ensemble models, voting and bagging are discussed in chapter 7. See also lecture from STK-IN4300, lecture 7. Chapter 9.2 of Hastie et al contains also a good discussion.
  • + +
    + + +









    +

    Building a tree, regression

    + +

    There are mainly two steps

    +
      +
    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 \).
    2. +
    3. 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 \).
    4. +
    +

    How do we construct the regions \( R_1,\dots,R_J \)? In theory, the +regions could have any shape. However, we choose to divide the +predictor space into high-dimensional rectangles, or boxes, for +simplicity and for ease of interpretation of the resulting predictive +model. The goal is to find boxes \( R_1,\dots,R_J \) that minimize the +MSE, given by +

    + +$$ +\sum_{j=1}^J\sum_{i\in R_j}(y_i-\overline{y}_{R_j})^2, +$$ + +

    where \( \overline{y}_{R_j} \) is the mean response for the training observations +within box \( j \). +

    + +









    +

    A top-down approach, recursive binary splitting

    + +

    Unfortunately, it is computationally infeasible to consider every +possible partition of the feature space into \( J \) boxes. The common +strategy is to take a top-down approach +

    + +

    The approach is top-down because it begins at the top of the tree (all +observations belong to a single region) and then successively splits +the predictor space; each split is indicated via two new branches +further down on the tree. It is greedy because at each step of the +tree-building process, the best split is made at that particular step, +rather than looking ahead and picking a split that will lead to a +better tree in some future step. +

    + +









    +

    Making a tree

    + +

    In order to implement the recursive binary splitting we start by selecting +the predictor \( x_j \) and a cutpoint \( s \) that splits the predictor space into two regions \( R_1 \) and \( R_2 \) +

    +$$ +\left\{X\vert x_j < s\right\}, +$$ + +

    and

    +$$ +\left\{X\vert x_j \geq s\right\}, +$$ + +

    so that we obtain the lowest MSE, that is

    +$$ +\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, +$$ + +

    which we want to minimize by considering all predictors +\( x_1,x_2,\dots,x_p \). We consider also all possible values of \( s \) for +each predictor. These values could be determined by randomly assigned +numbers or by starting at the midpoint and then proceed till we find +an optimal value. +

    + +

    For any \( j \) and \( s \), we define the pair of half-planes where +\( \overline{y}_{R_1} \) is the mean response for the training +observations in \( R_1(j,s) \), and \( \overline{y}_{R_2} \) is the mean +response for the training observations in \( R_2(j,s) \). +

    + +

    Finding the values of \( j \) and \( s \) that minimize the above equation can be +done quite quickly, especially when the number of features \( p \) is not +too large. +

    + +

    Next, we repeat the process, looking +for the best predictor and best cutpoint in order to split the data +further so as to minimize the MSE within each of the resulting +regions. However, this time, instead of splitting the entire predictor +space, we split one of the two previously identified regions. We now +have three regions. Again, we look to split one of these three regions +further, so as to minimize the MSE. The process continues until a +stopping criterion is reached; for instance, we may continue until no +region contains more than five observations. +

    + + +

    Pruning the tree

    + +

    The above procedure is rather straightforward, but leads often to +overfitting and unnecessarily large and complicated trees. The basic +idea is to grow a large tree \( T_0 \) and then prune it back in order to +obtain a subtree. A smaller tree with fewer splits (fewer regions) can +lead to smaller variance and better interpretation at the cost of a +little more bias. +

    + +

    The so-called Cost complexity pruning algorithm gives us a +way to do just this. Rather than considering every possible subtree, +we consider a sequence of trees indexed by a nonnegative tuning +parameter \( \alpha \). +

    + +

    Read more at the following Scikit-Learn link on pruning.

    + +









    +

    Cost complexity pruning

    + +

    For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that

    +$$ +\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T}, +$$ + +

    is as small as possible. Here \( \overline{T} \) is +the number of terminal nodes of the tree \( T \) , \( R_m \) is the +rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node. +

    + +

    The tuning parameter \( \alpha \) controls a trade-off between the subtree’s +complexity and its fit to the training data. When \( \alpha = 0 \), then the +subtree \( T \) will simply equal \( T_0 \), +because then the above equation just measures the +training error. +However, as \( \alpha \) increases, there is a price to pay for +having a tree with many terminal nodes. The above equation will +tend to be minimized for a smaller subtree. +

    + +

    It turns out that as we increase \( \alpha \) from zero +branches get pruned from the tree in a nested and predictable fashion, +so obtaining the whole sequence of subtrees as a function of \( \alpha \) is +easy. We can select a value of \( \alpha \) using a validation set or using +cross-validation. We then return to the full data set and obtain the +subtree corresponding to \( \alpha \). +

    + +









    +

    Schematic Regression Procedure

    + +
    +Building a Regression Tree +

    + +

      +
    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.
    2. +
    3. Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \( \alpha \).
    4. +
    5. 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:
    6. +
        +
      • repeat steps 1 and 2 on all but the \( k \)-th fold of the training data.
      • +
      • Then we valuate the mean squared prediction error on the data in the left-out \( k \)-th fold, as a function of \( \alpha \).
      • +
      • Finally we average the results for each value of \( \alpha \), and pick \( \alpha \) to minimize the average error.
      • +
      +
    7. Return the subtree from Step 2 that corresponds to the chosen value of \( \alpha \).
    8. +
    +
    + + +









    +

    A Classification Tree

    + +

    A classification tree is very similar to a regression tree, except +that it is used to predict a qualitative response rather than a +quantitative one. Recall that for a regression tree, the predicted +response for an observation is given by the mean response of the +training observations that belong to the same terminal node. In +contrast, for a classification tree, we predict that each observation +belongs to the most commonly occurring class of training observations +in the region to which it belongs. In interpreting the results of a +classification tree, we are often interested not only in the class +prediction corresponding to a particular terminal node region, but +also in the class proportions among the training observations that +fall into that region. +

    + +









    +

    Growing a classification tree

    + +

    The task of growing a +classification tree is quite similar to the task of growing a +regression tree. Just as in the regression setting, we use recursive +binary splitting to grow a classification tree. However, in the +classification setting, the MSE cannot be used as a criterion for making +the binary splits. A natural alternative to MSE is the classification +error rate. Since we plan to assign an observation in a given region +to the most commonly occurring error rate class of training +observations in that region, the classification error rate is simply +the fraction of the training observations in that region that do not +belong to the most common class. +

    + +

    When building a classification tree, either the Gini index or the +entropy are typically used to evaluate the quality of a particular +split, since these two approaches are more sensitive to node purity +than is the classification error rate. +

    + +









    +

    Classification tree, how to split nodes

    + +

    If our targets are the outcome of a classification process that takes +for example \( k=1,2,\dots,K \) values, the only thing we need to think of +is to set up the splitting criteria for each node. +

    + +

    We define a PDF \( p_{mk} \) that represents the number of observations of +a class \( k \) in a region \( R_m \) with \( N_m \) observations. We represent +this likelihood function in terms of the proportion \( I(y_i=k) \) of +observations of this class in the region \( R_m \) as +

    + +$$ +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i=k). +$$ + +

    We let \( p_{mk} \) represent the majority class of observations in region +\( m \). The three most common ways of splitting a node are given by +

    + +
      +
    • Misclassification error
    • +
    +$$ +p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i\ne k) = 1-p_{mk}. +$$ + +
      +
    • Gini index \( g \)
    • +
    +$$ +g = \sum_{k=1}^K p_{mk}(1-p_{mk}). +$$ + +
      +
    • Information entropy or just entropy \( s \)
    • +
    +$$ +s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}. +$$ + + +









    +

    Visualizing the Tree, Classification

    + + +
    +
    +
    +
    +
    +
    import os
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.metrics import confusion_matrix
    +from sklearn.tree import export_graphviz
    +
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import numpy as np
    +
    +
    +cancer = load_breast_cancer()
    +X = pd.DataFrame(cancer.data, columns=cancer.feature_names)
    +print(X)
    +y = pd.Categorical.from_codes(cancer.target, cancer.target_names)
    +y = pd.get_dummies(y)
    +print(y)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
    +tree_clf = DecisionTreeClassifier(max_depth=5)
    +tree_clf.fit(X_train, y_train)
    +
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/cancer.dot",
    +    feature_names=cancer.feature_names,
    +    class_names=cancer.target_names,
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Visualizing the Tree, The Moons

    + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.datasets import make_moons
    +from sklearn.tree import export_graphviz
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import os
    +
    +np.random.seed(42)
    +X, y = make_moons(n_samples=100, noise=0.25, random_state=53)
    +X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0)
    +tree_clf = DecisionTreeClassifier(max_depth=5)
    +tree_clf.fit(X_train, y_train)
    +
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/moons.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Other ways of visualizing the trees

    + +

    Scikit-Learn has also another way to visualize the trees which is very useful, here with the Iris data.

    + + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn import tree
    +X, y = load_iris(return_X_y=True)
    +tree_clf = tree.DecisionTreeClassifier()
    +tree_clf = tree_clf.fit(X, y)
    +# and then plot the tree
    +tree.plot_tree(tree_clf) 
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Printing out as text

    + +

    Alternatively, the tree can also be exported in textual format with the function exporttext. +This method doesn’t require the installation of external libraries and is more compact: +

    + + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_iris
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.tree import export_text
    +iris = load_iris()
    +decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
    +decision_tree = decision_tree.fit(iris.data, iris.target)
    +r = export_text(decision_tree, feature_names=iris['feature_names'])
    +print(r)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Algorithms for Setting up Decision Trees

    + +

    Two algorithms stand out in the set up of decision trees:

    +
      +
    1. The CART (Classification And Regression Tree) algorithm for both classification and regression
    2. +
    3. The ID3 algorithm based on the computation of the information gain for classification
    4. +
    +

    We discuss both algorithms with applications here. The popular library +Scikit-Learn uses the CART algorithm. For classification problems +you can use either the gini index or the entropy to split a tree +in two branches. +

    + +









    +

    The CART algorithm for Classification

    + +

    For classification, the CART algorithm splits the data set in two subsets using a single feature \( k \) and a threshold \( t_k \). +This could be for example a threshold set by a number below a certain circumference of a malign tumor. +

    + +

    How do we find these two quantities? +We search for the pair \( (k,t_k) \) that produces the purest subset using for example the gini factor \( G \). +The cost function it tries to minimize is then +

    +$$ +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}G_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}G_{\mathrm{right}}, +$$ + +

    where \( G_{\mathrm{left/right}} \) measures the impurity of the left/right subset and \( m_{\mathrm{left/right}} \) + is the number of instances in the left/right subset +

    + +

    Once it has successfully split the training set in two, it splits the subsets using the same logic, then the subsubsets +and so on, recursively. It stops recursing once it reaches the maximum depth (defined by the +\( max\_depth \) hyperparameter), or if it cannot find a split that will reduce impurity. A few other +hyperparameters control additional stopping conditions such as the \( min\_samples\_split \), +\( min\_samples\_leaf \), \( min\_weight\_fraction\_leaf \), and \( max\_leaf\_nodes \). +

    + +









    +

    The CART algorithm for Regression

    + +

    The CART algorithm for regression works is similar to the one for classification except that instead of trying to split the +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 +

    +$$ +C(k,t_k) = \frac{m_{\mathrm{left}}}{m}\mathrm{MSE}_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}\mathrm{MSE}_{\mathrm{right}}. +$$ + +

    Here the MSE for a specific node is defined as

    +$$ +\mathrm{MSE}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}(\overline{y}_{\mathrm{node}}-y_i)^2, +$$ + +

    with

    +$$ +\overline{y}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}y_i, +$$ + +

    the mean value of all observations in a specific node.

    + +

    Without any regularization, the regression task for decision trees, +just like for classification tasks, is prone to overfitting. +

    + +









    +

    Why binary splits?

    + +

    It is custom to split to a tree uising binary splits. The reason is +that multiway splits fragment the data too quickly, leaving +insufficient data at the next level down. Multiway splits can be +achieved by a series of binary split and this is normally preferred. +

    + +









    +

    Computing a Tree using the Gini Index

    + +

    Consider the following example with attributes/features and two +possible outcomes (classes) for each attribute. Assume we wish to find some +correlations between the average grade of a student as function of the +number of hours studied and hours slept. We want also to correlate the +grade in a given course with the general trend, whether the students +recently has gotten grades below average or above. +

    + +

    We have three features/attributes

    +
      +
    1. Trend of average grades before present course, classified as either below or above the average grade of the whole class
    2. +
    3. The number of hours studies, classified again as either higher (more than 3 hours per day) or lower . Here we have used a standard for one \( ECTS \) which is scaled to 25-30 hours of work for a semester which lasts 18 weeks, with 15 weeks of lectures and 3 weeks for exams, assuming a total of 30 ECTS per semester.
    4. +
    5. The number of hours slept as high for more than \( 8 \) hours and below for less than 8 hours of sleep, classified again as either high or low
    6. +
    7. The final grade whether it is above or below average
    8. +
    +









    +

    The Table

    + + + + + + + + + + + + + + + + + +
    Grade Trend Hours slept Hours Studied Grade
    Above Low High Above
    Below High Low Below
    Above Low High Above
    Above High High Above
    Below Low High Below
    Above Low Low Below
    Below High High Below
    Below Low High Below
    Above Low Low Below
    Above High High Above
    + +









    +

    Computing the various Gini Indices

    + +

    In computations we will translate all classes into numbers. Being +these binary classes, they can easily be split into ones and zeros. +

    + +
    +Gini index for Average trend +

    +

    See whiteboard notes from lecture November 11 at https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf

    +
    + + +









    +

    A possible code using Scikit-Learn

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +infile = open(data_path("grades.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +grades = pd.read_csv(infile)
    +grades = pd.DataFrame(grades)
    +display(grades)
    +# Features and targets
    +X = grades.loc[:, grades.columns != 'Grade'].values
    +y = grades.loc[:, grades.columns == 'Grade'].values
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/grade.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Further example: Computing the Gini index

    + +

    The next example we will look at is a classical one in many Machine +Learning applications. Based on various meteorological features, we +have several so-called attributes which decide whether we at the end +will do some outdoor activity like skiing, going for a bike ride etc +etc. The table here contains the feautures outlook, temperature, +humidity and wind. The target or output is whether we ride +(True=1) or whether we do something else that day (False=0). The +attributes for each feature are then sunny, overcast and rain for the +outlook, hot, cold and mild for temperature, high and normal for +humidity and weak and strong for wind. +

    + +

    The table here summarizes the various attributes and

    + + + + + + + + + + + + + + + + + + + + +
    Day Outlook Temperature Humidity Wind Ride
    1 Sunny Hot High Weak 0
    2 Sunny Hot High Strong 1
    3 Overcast Hot High Weak 1
    4 Rain Mild High Weak 1
    5 Rain Cool Normal Weak 1
    6 Rain Cool Normal Strong 0
    7 Overcast Cool Normal Strong 1
    8 Sunny Mild High Weak 0
    9 Sunny Cool Normal Weak 1
    10 Rain Mild Normal Weak 1
    11 Sunny Mild Normal Strong 1
    12 Overcast Mild High Strong 1
    13 Overcast Hot Normal Weak 1
    14 Rain Mild High Strong 0
    + +









    +

    Simple Python Code to read in Data and perform Classification

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +infile = open(data_path("rideclass.csv"),'r')
    +
    +# Read the experimental data with Pandas
    +from IPython.display import display
    +ridedata = pd.read_csv(infile,names = ('Outlook','Temperature','Humidity','Wind','Ride'))
    +ridedata = pd.DataFrame(ridedata)
    +
    +# Features and targets
    +X = ridedata.loc[:, ridedata.columns != 'Ride'].values
    +y = ridedata.loc[:, ridedata.columns == 'Ride'].values
    +
    +# Create the encoder.
    +encoder = OneHotEncoder(handle_unknown="ignore")
    +# Assume for simplicity all features are categorical.
    +encoder.fit(X)    
    +# Apply the encoder.
    +X = encoder.transform(X)
    +print(X)
    +# Then do a Classification tree
    +tree_clf = DecisionTreeClassifier(max_depth=2)
    +tree_clf.fit(X, y)
    +print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
    +#transfer to a decision tree graph
    +export_graphviz(
    +    tree_clf,
    +    out_file="DataFiles/ride.dot",
    +    rounded=True,
    +    filled=True
    +)
    +cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
    +os.system(cmd)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Computing the Gini Factor

    + +

    The above functions (gini, entropy and misclassification error) are +important components of the so-called CART algorithm. We will discuss +this algorithm below after we have discussed the information gain +algorithm ID3. +

    + +

    In the example here we have converted all our attributes into numerical values \( 0,1,2 \) etc.

    + + + +
    +
    +
    +
    +
    +
    # Split a dataset based on an attribute and an attribute value
    +def test_split(index, value, dataset):
    +	left, right = list(), list()
    +	for row in dataset:
    +		if row[index] < value:
    +			left.append(row)
    +		else:
    +			right.append(row)
    +	return left, right
    + 
    +# Calculate the Gini index for a split dataset
    +def gini_index(groups, classes):
    +	# count all samples at split point
    +	n_instances = float(sum([len(group) for group in groups]))
    +	# sum weighted Gini index for each group
    +	gini = 0.0
    +	for group in groups:
    +		size = float(len(group))
    +		# avoid divide by zero
    +		if size == 0:
    +			continue
    +		score = 0.0
    +		# score the group based on the score for each class
    +		for class_val in classes:
    +			p = [row[-1] for row in group].count(class_val) / size
    +			score += p * p
    +		# weight the group score by its relative size
    +		gini += (1.0 - score) * (size / n_instances)
    +	return gini
    +
    +# Select the best split point for a dataset
    +def get_split(dataset):
    +	class_values = list(set(row[-1] for row in dataset))
    +	b_index, b_value, b_score, b_groups = 999, 999, 999, None
    +	for index in range(len(dataset[0])-1):
    +		for row in dataset:
    +			groups = test_split(index, row[index], dataset)
    +			gini = gini_index(groups, class_values)
    +			print('X%d < %.3f Gini=%.3f' % ((index+1), row[index], gini))
    +			if gini < b_score:
    +				b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    +	return {'index':b_index, 'value':b_value, 'groups':b_groups}
    + 
    +dataset = [[0,0,0,0,0],
    +            [0,0,0,1,1],
    +            [1,0,0,0,1],
    +            [2,1,0,0,1],
    +            [2,2,1,0,1],
    +            [2,2,1,1,0],
    +            [1,2,1,1,1],
    +            [0,1,0,0,0],
    +            [0,2,1,0,1],
    +            [2,1,1,0,1],
    +            [0,1,1,1,1],
    +            [1,1,0,1,1],
    +            [1,0,1,0,1],
    +            [2,1,0,1,0]]
    +
    +split = get_split(dataset)
    +print('Split: [X%d < %.3f]' % ((split['index']+1), split['value']))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Regression trees

    + + +
    +
    +
    +
    +
    +
    # Quadratic training set + noise
    +np.random.seed(42)
    +m = 200
    +X = np.random.rand(m, 1)
    +y = 4 * (X - 0.5) ** 2
    +y = y + np.random.randn(m, 1) / 10
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42)
    +tree_reg.fit(X, y)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Final regressor code

    + + +
    +
    +
    +
    +
    +
    from sklearn.tree import DecisionTreeRegressor
    +
    +tree_reg1 = DecisionTreeRegressor(random_state=42, max_depth=2)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, max_depth=3)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +def plot_regression_predictions(tree_reg, X, y, axes=[0, 1, -0.2, 1], ylabel="$y$"):
    +    x1 = np.linspace(axes[0], axes[1], 500).reshape(-1, 1)
    +    y_pred = tree_reg.predict(x1)
    +    plt.axis(axes)
    +    plt.xlabel("$x_1$", fontsize=18)
    +    if ylabel:
    +        plt.ylabel(ylabel, fontsize=18, rotation=0)
    +    plt.plot(X, y, "b.")
    +    plt.plot(x1, y_pred, "r.-", linewidth=2, label=r"$\hat{y}$")
    +
    +plt.figure(figsize=(11, 4))
    +plt.subplot(121)
    +plot_regression_predictions(tree_reg1, X, y)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +plt.text(0.21, 0.65, "Depth=0", fontsize=15)
    +plt.text(0.01, 0.2, "Depth=1", fontsize=13)
    +plt.text(0.65, 0.8, "Depth=1", fontsize=13)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("max_depth=2", fontsize=14)
    +
    +plt.subplot(122)
    +plot_regression_predictions(tree_reg2, X, y, ylabel=None)
    +for split, style in ((0.1973, "k-"), (0.0917, "k--"), (0.7718, "k--")):
    +    plt.plot([split, split], [-0.2, 1], style, linewidth=2)
    +for split in (0.0458, 0.1298, 0.2873, 0.9040):
    +    plt.plot([split, split], [-0.2, 1], "k:", linewidth=1)
    +plt.text(0.3, 0.5, "Depth=2", fontsize=13)
    +plt.title("max_depth=3", fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    tree_reg1 = DecisionTreeRegressor(random_state=42)
    +tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)
    +tree_reg1.fit(X, y)
    +tree_reg2.fit(X, y)
    +
    +x1 = np.linspace(0, 1, 500).reshape(-1, 1)
    +y_pred1 = tree_reg1.predict(x1)
    +y_pred2 = tree_reg2.predict(x1)
    +
    +plt.figure(figsize=(11, 4))
    +
    +plt.subplot(121)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred1, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.ylabel("$y$", fontsize=18, rotation=0)
    +plt.legend(loc="upper center", fontsize=18)
    +plt.title("No restrictions", fontsize=14)
    +
    +plt.subplot(122)
    +plt.plot(X, y, "b.")
    +plt.plot(x1, y_pred2, "r.-", linewidth=2, label=r"$\hat{y}$")
    +plt.axis([0, 1, -0.2, 1.1])
    +plt.xlabel("$x_1$", fontsize=18)
    +plt.title("min_samples_leaf={}".format(tree_reg2.min_samples_leaf), fontsize=14)
    +
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Pros and cons of trees, pros

    + +
      +
    • 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)
    • +
    • Trees are very easy to explain to people. In fact, they are even easier to explain than linear regression!
    • +
    • No feature normalization needed
    • +
    • Tree models can handle both continuous and categorical data (Classification and Regression Trees)
    • +
    • Can model nonlinear relationships
    • +
    • Can model interactions between the different descriptive features
    • +
    • Trees can be displayed graphically, and are easily interpreted even by a non-expert (especially if they are small)
    • +
    +









    +

    Disadvantages

    + +
      +
    • Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches
    • +
    • If continuous features are used the tree may become quite large and hence less interpretable
    • +
    • 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
    • +
    • 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
    • +
    • 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.
    • +
    • If the number of features is relatively large (high dimensional) and the number of instances is relatively low, the tree might overfit the data
    • +
    • 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
    • +
    +

    However, by aggregating many decision trees, using methods like +bagging, random forests, and boosting, the predictive performance of +trees can be substantially improved. +

    + +









    +

    Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods

    + +

    As stated above and seen in many of the examples discussed here about +a single decision tree, we often end up overfitting our training +data. This normally means that we have a high variance. Can we reduce +the variance of a statistical learning method? +

    + +

    This leads us to a set of different methods that can combine different +machine learning algorithms or just use one of them to construct +forests and jungles of trees, homogeneous ones or heterogenous +ones. These methods are recognized by different names which we will +try to explain here. These are +

    + +
      +
    1. Voting classifiers
    2. +
    3. Bagging and Pasting
    4. +
    5. Random forests
    6. +
    7. Boosting methods, from adaptive to Extreme Gradient Boosting (XGBoost)
    8. +
    +

    We discuss these methods here.

    + +









    +

    An Overview of Ensemble Methods

    + +

    +
    +

    +
    +

    + +









    +

    Why Voting?

    + +

    The idea behind boosting, and voting as well can be phrased as follows: +Can a group of people somehow arrive at highly +reasoned decisions, despite the weak judgement of the individual +members? +

    + +

    The aim is to create a good classifier by combining several weak classifiers. +A weak classifier is a classifier which is able to produce results that are only slightly better than guessing at random. +

    + +

    The basic approach is to apply repeatedly (in boosting this is done in an iterative way) a weak classifier to modifications of the data. +In voting we simply apply the law of large numbers while in boosting we give more weight to misclassified data in +each iteration. +

    + +

    Decision trees play an important role as our weak classifier. They serve as the basic method.

    + +









    +

    Tossing coins

    + +

    The simplest case is a so-called voting ensemble. To illustrate this, +think of yourself tossing coins with a biased outcome of 51 per cent +for heads and 49% for tails. With only few tosses, +you may not clearly see this distribution for heads and tails. However, after some +thousands of tosses, there will be a clear majority of heads. With 2000 tosses +you should see approximately 1020 heads and 980 tails. +

    + +

    We can then state that the outcome is a clear majority of heads. If +you do this ten thousand times, it is easy to see that there is a 97% +likelihood of a majority of heads. +

    + +

    Another example would be to collect all polls before an +election. Different polls may show different likelihoods for a +candidate winning with say a majority of the popular vote. The majority vote +would then consist in many polls indicating that this candidate will +actually win. +

    + +

    The example here shows how we can implement the coin tossing case, +clealry demostrating that after some tosses we see the law of large +numbers kicking in. +

    + +









    +

    Standard imports first

    + + + +
    +
    +
    +
    +
    +
    # Common imports
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import pandas as pd
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.tree import DecisionTreeClassifier
    +from sklearn.model_selection import train_test_split
    +from sklearn.tree import export_graphviz
    +from sklearn.preprocessing import StandardScaler, OneHotEncoder
    +from sklearn.compose import ColumnTransformer
    +from IPython.display import Image 
    +from pydot import graph_from_dot_data
    +import os
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Simple Voting Example, head or tail

    + + +
    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import matplotlib
    +import matplotlib.pyplot as plt
    +from matplotlib.colors import ListedColormap
    +plt.rcParams['axes.labelsize'] = 14
    +plt.rcParams['xtick.labelsize'] = 12
    +plt.rcParams['ytick.labelsize'] = 12
    +
    +heads_proba = 0.51
    +coin_tosses = (np.random.rand(10000, 10) < heads_proba).astype(np.int32)
    +cumulative_heads_ratio = np.cumsum(coin_tosses, axis=0) / np.arange(1, 10001).reshape(-1, 1)
    +plt.figure(figsize=(8,3.5))
    +plt.plot(cumulative_heads_ratio)
    +plt.plot([0, 10000], [0.51, 0.51], "k--", linewidth=2, label="51%")
    +plt.plot([0, 10000], [0.5, 0.5], "k-", label="50%")
    +plt.xlabel("Number of coin tosses")
    +plt.ylabel("Heads ratio")
    +plt.legend(loc="lower right")
    +plt.axis([0, 10000, 0.42, 0.58])
    +save_fig("votingsimple")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Using the Voting Classifier

    + +

    We can use the voting classifier on other data sets, here the exciting binary case of two distinct objects using the make moons functionality of Scikit-Learn.

    + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +log_clf = LogisticRegression(solver="liblinear", random_state=42)
    +rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
    +svm_clf = SVC(gamma="auto", probability=True, random_state=42)
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Voting and Bagging

    + + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +from sklearn.datasets import make_moons
    +
    +X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    +from sklearn.ensemble import RandomForestClassifier
    +from sklearn.ensemble import VotingClassifier
    +from sklearn.linear_model import LogisticRegression
    +from sklearn.svm import SVC
    +
    +log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='hard')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    log_clf = LogisticRegression(random_state=42)
    +rnd_clf = RandomForestClassifier(random_state=42)
    +svm_clf = SVC(probability=True, random_state=42)
    +
    +voting_clf = VotingClassifier(
    +    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    +    voting='soft')
    +voting_clf.fit(X_train, y_train)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    from sklearn.metrics import accuracy_score
    +
    +for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    +    clf.fit(X_train, y_train)
    +    y_pred = clf.predict(X_test)
    +    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +









    +

    Bagging

    + +

    The plain decision trees suffer from high +variance. This means that if we split the training data into two parts +at random, and fit a decision tree to both halves, the results that we +get could be quite different. In contrast, a procedure with low +variance will yield similar results if applied repeatedly to distinct +data sets; linear regression tends to have low variance, if the ratio +of \( n \) to \( p \) is moderately large. +

    + +

    Bootstrap aggregation, or just bagging, is a +general-purpose procedure for reducing the variance of a statistical +learning method. +

    + +









    +

    More bagging

    + +

    Bagging typically results in improved accuracy +over prediction using a single tree. Unfortunately, however, it can be +difficult to interpret the resulting model. Recall that one of the +advantages of decision trees is the attractive and easily interpreted +diagram that results. +

    + +

    However, when we bag a large number of trees, it is no longer +possible to represent the resulting statistical learning procedure +using a single tree, and it is no longer clear which variables are +most important to the procedure. Thus, bagging improves prediction +accuracy at the expense of interpretability. Although the collection +of bagged trees is much more difficult to interpret than a single +tree, one can obtain an overall summary of the importance of each +predictor using the MSE (for bagging regression trees) or the Gini +index (for bagging classification trees). In the case of bagging +regression trees, we can record the total amount that the MSE is +decreased due to splits over a given predictor, averaged over all \( B \) possible +trees. A large value indicates an important predictor. Similarly, in +the context of bagging classification trees, we can add up the total +amount that the Gini index is decreased by splits over a given +predictor, averaged over all \( B \) trees. +

    + +









    +

    Making your own Bootstrap: Changing the Level of the Decision Tree

    + +

    Let us bring up our good old boostrap example from the linear regression lectures. We change the linerar regression algorithm with +a decision tree wth different depths and perform a bootstrap aggregate (in this case we perform as many bootstraps as data points \( n \)). +

    + + +
    +
    +
    +
    +
    +
    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import train_test_split
    +from sklearn.pipeline import make_pipeline
    +from sklearn.utils import resample
    +from sklearn.tree import DecisionTreeRegressor
    +
    +n = 100
    +n_boostraps = 100
    +maxdepth = 8
    +
    +# Make data set.
    +x = np.linspace(-3, 3, n).reshape(-1, 1)
    +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
    +error = np.zeros(maxdepth)
    +bias = np.zeros(maxdepth)
    +variance = np.zeros(maxdepth)
    +polydegree = np.zeros(maxdepth)
    +X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    +
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +# we produce a simple tree first as benchmark
    +simpletree = DecisionTreeRegressor(max_depth=3) 
    +simpletree.fit(X_train_scaled, y_train)
    +simpleprediction = simpletree.predict(X_test_scaled)
    +for degree in range(1,maxdepth):
    +    model = DecisionTreeRegressor(max_depth=degree) 
    +    y_pred = np.empty((y_test.shape[0], n_boostraps))
    +    for i in range(n_boostraps):
    +        x_, y_ = resample(X_train_scaled, y_train)
    +        model.fit(x_, y_)
    +        y_pred[:, i] = model.predict(X_test_scaled)#.ravel()
    +
    +    polydegree[degree] = degree
    +    error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
    +    bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
    +    variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
    +    print('Polynomial degree:', degree)
    +    print('Error:', error[degree])
    +    print('Bias^2:', bias[degree])
    +    print('Var:', variance[degree])
    +    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
    + 
    +mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**2))
    +print("Simple tree:",mse_simpletree)
    +plt.xlim(1,maxdepth)
    +plt.plot(polydegree, error, label='MSE')
    +plt.plot(polydegree, bias, label='bias')
    +plt.plot(polydegree, variance, label='Variance')
    +plt.legend()
    +save_fig("baggingboot")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    @@ -814,6 +2501,110 @@ plt.show()
    +









    +

    Making an ADAboost code yourself

    + + + +
    +
    +
    +
    +
    +
    import numpy as np
    +
    +class DecisionStump:
    +    def fit(self, X, y, weights):
    +        m, n = X.shape
    +        self.alpha = 0
    +        self.threshold = None
    +        self.polarity = 1
    +
    +        min_error = float('inf')
    +
    +        for feature in range(n):
    +            feature_values = np.unique(X[:, feature])
    +
    +            for threshold in feature_values:
    +                for polarity in [1, -1]:
    +                    predictions = np.ones(m)
    +                    predictions[X[:, feature] < threshold] = -1
    +                    predictions *= polarity
    +
    +                    error = sum(weights[predictions != y])
    +
    +                    if error < min_error:
    +                        min_error = error
    +                        self.alpha = 0.5 * np.log((1 - error) / (error + 1e-10))
    +                        self.threshold = threshold
    +                        self.feature_index = feature
    +                        self.polarity = polarity
    +
    +    def predict(self, X):
    +        m = X.shape[0]
    +        predictions = np.ones(m)
    +        if self.polarity == 1:
    +            predictions[X[:, self.feature_index] < self.threshold] = -1
    +        else:
    +            predictions[X[:, self.feature_index] >= self.threshold] = -1
    +        return predictions
    +
    +class AdaBoost:
    +    def fit(self, X, y, n_estimators):
    +        m = X.shape[0]
    +        self.alphas = []
    +        self.models = []
    +
    +        weights = np.ones(m) / m
    +
    +        for _ in range(n_estimators):
    +            stump = DecisionStump()
    +            stump.fit(X, y, weights)
    +            predictions = stump.predict(X)
    +
    +            error = sum(weights[predictions != y])
    +            if error == 0:
    +                break
    +
    +            self.models.append(stump)
    +            self.alphas.append(stump.alpha)
    +
    +            weights *= np.exp(-stump.alpha * y * predictions)
    +            weights /= np.sum(weights)
    +
    +    def predict(self, X):
    +        final_predictions = np.zeros(X.shape[0])
    +        for alpha, model in zip(self.alphas, self.models):
    +            final_predictions += alpha * model.predict(X)
    +        return np.sign(final_predictions)
    +
    +# Example dataset (X, y)
    +X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
    +y = np.array([-1, -1, -1, -1, 1, 1, 1, 1, 1, 1])  # Labels must be -1 or 1
    +
    +# Train AdaBoost
    +ada = AdaBoost()
    +ada.fit(X, y, n_estimators=10)
    +
    +# Predictions
    +predictions = ada.predict(X)
    +print("Predictions:", predictions)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +









    Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent

    @@ -1191,6 +2982,107 @@ plt.show()
    +









    +

    Gradient boosting, making our own code for a regression case

    + + + +
    +
    +
    +
    +
    +
    import numpy as np
    +class DecisionTreeRegressor:
    +    def __init__(self, max_depth=3):
    +        self.max_depth = max_depth
    +        self.tree = None
    +    def fit(self, X, y):
    +        self.tree = self._grow_tree(X, y)
    +    def _grow_tree(self, X, y, depth=0):
    +        n_samples, n_features = X.shape
    +        if depth < self.max_depth:
    +            best_feature, best_threshold = self._best_split(X, y)
    +            if best_feature is not None:
    +                left_indices = X[:, best_feature] < best_threshold
    +                right_indices = X[:, best_feature] >= best_threshold
    +                left_child = self._grow_tree(X[left_indices], y[left_indices], depth + 1)
    +                right_child = self._grow_tree(X[right_indices], y[right_indices], depth + 1)
    +                return (best_feature, best_threshold, left_child, right_child)
    +        return np.mean(y)
    +    def _best_split(self, X, y):
    +        best_mse = float('inf')
    +        best_feature, best_threshold = None, None
    +        n_samples, n_features = X.shape
    +        
    +        for feature in range(n_features):
    +            thresholds = np.unique(X[:, feature])
    +            for threshold in thresholds:
    +                left_indices = X[:, feature] < threshold
    +                right_indices = X[:, feature] >= threshold
    +                if len(y[left_indices]) > 0 and len(y[right_indices]) > 0:
    +                    left_mse = np.mean((y[left_indices] - np.mean(y[left_indices])) ** 2)
    +                    right_mse = np.mean((y[right_indices] - np.mean(y[right_indices])) ** 2)
    +                    mse = (len(y[left_indices]) * left_mse + len(y[right_indices]) * right_mse) / n_samples
    +                    
    +                    if mse < best_mse:
    +                        best_mse = mse
    +                        best_feature = feature
    +                        best_threshold = threshold
    +        return best_feature, best_threshold
    +    def predict(self, X):
    +        return np.array([self._predict_sample(sample, self.tree) for sample in X])
    +    def _predict_sample(self, sample, node):
    +        if isinstance(node, tuple):
    +            feature, threshold, left_child, right_child = node
    +            if sample[feature] < threshold:
    +                return self._predict_sample(sample, left_child)
    +            else:
    +                return self._predict_sample(sample, right_child)
    +        return node
    +class GradientBoostingRegressor:
    +    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
    +        self.n_estimators = n_estimators
    +        self.learning_rate = learning_rate
    +        self.max_depth = max_depth
    +        self.models = []
    +    def fit(self, X, y):
    +        y_pred = np.zeros(y.shape)
    +        for _ in range(self.n_estimators):
    +            residuals = y - y_pred
    +            model = DecisionTreeRegressor(max_depth=self.max_depth)
    +            model.fit(X, residuals)
    +            y_pred += self.learning_rate * model.predict(X)
    +            self.models.append(model)
    +    def predict(self, X):
    +        y_pred = np.zeros(X.shape[0])
    +        for model in self.models:
    +            y_pred += self.learning_rate * model.predict(X)
    +        return y_pred
    +# Example usage
    +if __name__ == "__main__":
    +    # Sample data
    +    X = np.array([[1], [2], [3], [4], [5]])
    +    y = np.array([1.5, 1.7, 3.5, 3.7, 5.0])
    +    model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=2)
    +    model.fit(X, y)
    +    predictions = model.predict(X)
    +    print("Predictions:", predictions)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    © 1999-2024, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license diff --git a/doc/pub/week47/ipynb/ipynb-week47-src.tar.gz b/doc/pub/week47/ipynb/ipynb-week47-src.tar.gz index 1dc8c69d3..78b3c2864 100644 Binary files a/doc/pub/week47/ipynb/ipynb-week47-src.tar.gz and b/doc/pub/week47/ipynb/ipynb-week47-src.tar.gz differ diff --git a/doc/pub/week47/ipynb/week47.ipynb b/doc/pub/week47/ipynb/week47.ipynb index 4acbaac61..0c1345e91 100644 --- a/doc/pub/week47/ipynb/week47.ipynb +++ b/doc/pub/week47/ipynb/week47.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "4d8931aa", + "id": "4d3fe757", "metadata": { "editable": true }, @@ -14,7 +14,7 @@ }, { "cell_type": "markdown", - "id": "b88b18f7", + "id": "7f85b422", "metadata": { "editable": true }, @@ -27,7 +27,7 @@ }, { "cell_type": "markdown", - "id": "7acd7259", + "id": "fa24eb3c", "metadata": { "editable": true }, @@ -42,30 +42,1861 @@ "\n", " \n", "\n", - "**Material for the lecture Monday 18 November.**\n", + "**Plans for the lecture Monday 18 November, with video suggestions etc.**\n", "\n", "1. Basics of decision trees, classification and regression algorithms and ensemble models \n", "\n", "2. Readings and Videos:\n", "\n", - " * These lecture notes\n", + "a. These lecture notes at \n", + "\n", + "b. See also lecture notes from week 46 at . The lecture on Monday starts with a repetition on how to make a decision tree.\n", "\n", "\n", "\n", - " * Video on Decision trees \n", + "c. Video on Decision trees \n", "\n", - " * Video on boosting methods \n", + "d. Video on boosting methods \n", "\n", - " * Video on AdaBoost \n", + "e. Video on AdaBoost \n", "\n", - " * Video on Gradient boost, part 1, parts 2-4 follow thereafter \n", + "f. Video on Gradient boost, part 1, parts 2-4 follow thereafter \n", "\n", - " * Decision Trees: Geron's chapter 6 covers decision trees while ensemble models, voting and bagging are discussed in chapter 7. See also lecture from [STK-IN4300, lecture 7](https://www.uio.no/studier/emner/matnat/math/STK-IN4300/h20/slides/lecture_7.pdf). Chapter 9.2 of Hastie et al contains also a good discussion." + "g. Decision Trees: Rashcka et al chapter 3 pages 86-98, and chapter 7 on Ensemble methods, Voting and Bagging and Gradient Boosting. See also lecture from STK-IN4300, lecture 7 at . \n", + "\n", + "* Decision Trees: Geron's chapter 6 covers decision trees while ensemble models, voting and bagging are discussed in chapter 7. See also lecture from [STK-IN4300, lecture 7](https://www.uio.no/studier/emner/matnat/math/STK-IN4300/h20/slides/lecture_7.pdf). Chapter 9.2 of Hastie et al contains also a good discussion." ] }, { "cell_type": "markdown", - "id": "32b919e8", + "id": "d784b210", + "metadata": { + "editable": true + }, + "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", + "id": "8db0495c", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\sum_{j=1}^J\\sum_{i\\in R_j}(y_i-\\overline{y}_{R_j})^2,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "d2bee1b4", + "metadata": { + "editable": true + }, + "source": [ + "where $\\overline{y}_{R_j}$ is the mean response for the training observations \n", + "within box $j$." + ] + }, + { + "cell_type": "markdown", + "id": "0811715d", + "metadata": { + "editable": true + }, + "source": [ + "## 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." + ] + }, + { + "cell_type": "markdown", + "id": "e230740b", + "metadata": { + "editable": true + }, + "source": [ + "## 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", + "id": "6fb81b46", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\left\\{X\\vert x_j < s\\right\\},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "5a6c7eee", + "metadata": { + "editable": true + }, + "source": [ + "and" + ] + }, + { + "cell_type": "markdown", + "id": "472962b3", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\left\\{X\\vert x_j \\geq s\\right\\},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "5569b95d", + "metadata": { + "editable": true + }, + "source": [ + "so that we obtain the lowest MSE, that is" + ] + }, + { + "cell_type": "markdown", + "id": "603c97da", + "metadata": { + "editable": true + }, + "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", + "id": "a27085de", + "metadata": { + "editable": true + }, + "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." + ] + }, + { + "cell_type": "markdown", + "id": "6edce7f4", + "metadata": { + "editable": true + }, + "source": [ + "## 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", + "Read more at the following [Scikit-Learn link on pruning](https://scikit-learn.org/stable/auto_examples/tree/plot_cost_complexity_pruning.html#sphx-glr-auto-examples-tree-plot-cost-complexity-pruning-py)." + ] + }, + { + "cell_type": "markdown", + "id": "8ef9b6b3", + "metadata": { + "editable": true + }, + "source": [ + "## Cost complexity pruning\n", + "\n", + "For each value of $\\alpha$ there corresponds a subtree $T \\in T_0$ such that" + ] + }, + { + "cell_type": "markdown", + "id": "fff0f5a7", + "metadata": { + "editable": true + }, + "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", + "id": "f57b665d", + "metadata": { + "editable": true + }, + "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", + "complexity 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", + "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$." + ] + }, + { + "cell_type": "markdown", + "id": "0b74057b", + "metadata": { + "editable": true + }, + "source": [ + "## 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", + "4. Return the subtree from Step 2 that corresponds to the chosen value of $\\alpha$." + ] + }, + { + "cell_type": "markdown", + "id": "4610be03", + "metadata": { + "editable": true + }, + "source": [ + "## 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." + ] + }, + { + "cell_type": "markdown", + "id": "32f831ba", + "metadata": { + "editable": true + }, + "source": [ + "## 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." + ] + }, + { + "cell_type": "markdown", + "id": "34d5f655", + "metadata": { + "editable": true + }, + "source": [ + "## 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", + "id": "a3de75e0", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "p_{mk} = \\frac{1}{N_m}\\sum_{x_i\\in R_m}I(y_i=k).\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "aa43ee50", + "metadata": { + "editable": true + }, + "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", + "id": "e1222e38", + "metadata": { + "editable": true + }, + "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", + "id": "1395dc91", + "metadata": { + "editable": true + }, + "source": [ + "* Gini index $g$" + ] + }, + { + "cell_type": "markdown", + "id": "993d40a8", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "g = \\sum_{k=1}^K p_{mk}(1-p_{mk}).\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "6bd76b59", + "metadata": { + "editable": true + }, + "source": [ + "* Information entropy or just entropy $s$" + ] + }, + { + "cell_type": "markdown", + "id": "e9461b02", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "s = -\\sum_{k=1}^K p_{mk}\\log{p_{mk}}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "ba2f5149", + "metadata": { + "editable": true + }, + "source": [ + "## Visualizing the Tree, Classification" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "94ba287b", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "id": "3344c069", + "metadata": { + "editable": true + }, + "source": [ + "## Visualizing the Tree, The Moons" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a00d93af", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "id": "3d3a793c", + "metadata": { + "editable": true + }, + "source": [ + "## Other ways of visualizing the trees\n", + "\n", + "**Scikit-Learn** has also another way to visualize the trees which is very useful, here with the Iris data." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9221c3e4", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], + "source": [ + "from sklearn.datasets import load_iris\n", + "from sklearn import tree\n", + "X, y = load_iris(return_X_y=True)\n", + "tree_clf = tree.DecisionTreeClassifier()\n", + "tree_clf = tree_clf.fit(X, y)\n", + "# and then plot the tree\n", + "tree.plot_tree(tree_clf)" + ] + }, + { + "cell_type": "markdown", + "id": "884f2e72", + "metadata": { + "editable": true + }, + "source": [ + "## Printing out as text\n", + "\n", + "Alternatively, the tree can also be exported in textual format with the function exporttext.\n", + "This method doesn’t require the installation of external libraries and is more compact:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "e25589c9", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], + "source": [ + "from sklearn.datasets import load_iris\n", + "from sklearn.tree import DecisionTreeClassifier\n", + "from sklearn.tree import export_text\n", + "iris = load_iris()\n", + "decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)\n", + "decision_tree = decision_tree.fit(iris.data, iris.target)\n", + "r = export_text(decision_tree, feature_names=iris['feature_names'])\n", + "print(r)" + ] + }, + { + "cell_type": "markdown", + "id": "941f9cfb", + "metadata": { + "editable": true + }, + "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." + ] + }, + { + "cell_type": "markdown", + "id": "91e465af", + "metadata": { + "editable": true + }, + "source": [ + "## 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", + "id": "5856c050", + "metadata": { + "editable": true + }, + "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", + "id": "6c6ff8fc", + "metadata": { + "editable": true + }, + "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$." + ] + }, + { + "cell_type": "markdown", + "id": "9275e55a", + "metadata": { + "editable": true + }, + "source": [ + "## 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", + "id": "80931cfb", + "metadata": { + "editable": true + }, + "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", + "id": "c42166f3", + "metadata": { + "editable": true + }, + "source": [ + "Here the MSE for a specific node is defined as" + ] + }, + { + "cell_type": "markdown", + "id": "b1d57a3a", + "metadata": { + "editable": true + }, + "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", + "id": "583fb1e3", + "metadata": { + "editable": true + }, + "source": [ + "with" + ] + }, + { + "cell_type": "markdown", + "id": "96924faf", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\overline{y}_{\\mathrm{node}}=\\frac{1}{m_\\mathrm{node}}\\sum_{i\\in \\mathrm{node}}y_i,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "fa77d3a5", + "metadata": { + "editable": true + }, + "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." + ] + }, + { + "cell_type": "markdown", + "id": "ab9cb860", + "metadata": { + "editable": true + }, + "source": [ + "## Why binary splits?\n", + "\n", + "It is custom to split to a tree uising binary splits. The reason is\n", + "that multiway splits fragment the data too quickly, leaving\n", + "insufficient data at the next level down. Multiway splits can be\n", + "achieved by a series of binary split and this is normally preferred." + ] + }, + { + "cell_type": "markdown", + "id": "cfe6346e", + "metadata": { + "editable": true + }, + "source": [ + "## Computing a Tree using the Gini Index\n", + "\n", + "Consider the following example with attributes/features and two\n", + "possible outcomes (classes) for each attribute. Assume we wish to find some\n", + "correlations between the average grade of a student as function of the\n", + "number of hours studied and hours slept. We want also to correlate the\n", + "grade in a given course with the general trend, whether the students\n", + "recently has gotten grades below average or above.\n", + "\n", + "We have three features/attributes\n", + "1. Trend of average grades before present course, classified as either below or above the average grade of the whole class \n", + "\n", + "2. The number of hours studies, classified again as either higher (more than 3 hours per day) or lower . Here we have used a standard for one $ECTS$ which is scaled to 25-30 hours of work for a semester which lasts 18 weeks, with 15 weeks of lectures and 3 weeks for exams, assuming a total of 30 ECTS per semester. \n", + "\n", + "3. The number of hours slept as high for more than $8$ hours and below for less than 8 hours of sleep, classified again as either high or low\n", + "\n", + "4. The final grade whether it is above or below average" + ] + }, + { + "cell_type": "markdown", + "id": "a482c6f0", + "metadata": { + "editable": true + }, + "source": [ + "## The Table\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    Grade Trend Hours slept Hours Studied Grade
    Above Low High Above
    Below High Low Below
    Above Low High Above
    Above High High Above
    Below Low High Below
    Above Low Low Below
    Below High High Below
    Below Low High Below
    Above Low Low Below
    Above High High Above
    " + ] + }, + { + "cell_type": "markdown", + "id": "20fe13e0", + "metadata": { + "editable": true + }, + "source": [ + "## Computing the various Gini Indices\n", + "\n", + "In computations we will translate all classes into numbers. Being\n", + "these binary classes, they can easily be split into ones and zeros.\n", + "\n", + "**Gini index for Average trend.**\n", + "\n", + "See whiteboard notes from lecture November 11 at " + ] + }, + { + "cell_type": "markdown", + "id": "252813aa", + "metadata": { + "editable": true + }, + "source": [ + "## A possible code using Scikit-Learn" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "04414f65", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "\n", + "# 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(\"grades.csv\"),'r')\n", + "\n", + "# Read the experimental data with Pandas\n", + "from IPython.display import display\n", + "grades = pd.read_csv(infile)\n", + "grades = pd.DataFrame(grades)\n", + "display(grades)\n", + "# Features and targets\n", + "X = grades.loc[:, grades.columns != 'Grade'].values\n", + "y = grades.loc[:, grades.columns == 'Grade'].values\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/grade.dot\",\n", + " rounded=True,\n", + " filled=True\n", + ")\n", + "cmd = 'dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png'\n", + "os.system(cmd)" + ] + }, + { + "cell_type": "markdown", + "id": "65ce5bad", + "metadata": { + "editable": true + }, + "source": [ + "## Further example: Computing the Gini index\n", + "\n", + "The next 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", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    Day Outlook Temperature Humidity Wind Ride
    1 Sunny Hot High Weak 0
    2 Sunny Hot High Strong 1
    3 Overcast Hot High Weak 1
    4 Rain Mild High Weak 1
    5 Rain Cool Normal Weak 1
    6 Rain Cool Normal Strong 0
    7 Overcast Cool Normal Strong 1
    8 Sunny Mild High Weak 0
    9 Sunny Cool Normal Weak 1
    10 Rain Mild Normal Weak 1
    11 Sunny Mild Normal Strong 1
    12 Overcast Mild High Strong 1
    13 Overcast Hot Normal Weak 1
    14 Rain Mild High Strong 0
    " + ] + }, + { + "cell_type": "markdown", + "id": "ec8cfb80", + "metadata": { + "editable": true + }, + "source": [ + "## Simple Python Code to read in Data and perform Classification" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a46e6786", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "id": "c2b31b1e", + "metadata": { + "editable": true + }, + "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": 7, + "id": "ca7473c6", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "id": "3f5c14cf", + "metadata": { + "editable": true + }, + "source": [ + "## Regression trees" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "0c1057e7", + "metadata": { + "collapsed": false, + "editable": true + }, + "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": 9, + "id": "17a775d7", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "id": "b91522ff", + "metadata": { + "editable": true + }, + "source": [ + "## Final regressor code" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "485e7be4", + "metadata": { + "collapsed": false, + "editable": true + }, + "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": 11, + "id": "9a686cbe", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "id": "4305a9c8", + "metadata": { + "editable": true + }, + "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)" + ] + }, + { + "cell_type": "markdown", + "id": "cee5ffaa", + "metadata": { + "editable": true + }, + "source": [ + "## 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." + ] + }, + { + "cell_type": "markdown", + "id": "1b09495e", + "metadata": { + "editable": true + }, + "source": [ + "## 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." + ] + }, + { + "cell_type": "markdown", + "id": "f0b540cb", + "metadata": { + "editable": true + }, + "source": [ + "## An Overview of Ensemble Methods\n", + "\n", + "\n", + "\n", + "\n", + "

    Figure 1:

    \n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "bc94f49e", + "metadata": { + "editable": true + }, + "source": [ + "## Why Voting?\n", + "\n", + "The idea behind boosting, and voting as well can be phrased as follows:\n", + "**Can a group of people somehow arrive at highly\n", + "reasoned decisions, despite the weak judgement of the individual\n", + "members?**\n", + "\n", + "The aim is to create a good classifier by combining several weak classifiers.\n", + "**A weak classifier is a classifier which is able to produce results that are only slightly better than guessing at random.**\n", + "\n", + "The basic approach is to apply repeatedly (in boosting this is done in an iterative way) a weak classifier to modifications of the data.\n", + "In voting we simply apply the law of large numbers while in boosting we give more weight to misclassified data in\n", + "each iteration. \n", + "\n", + "Decision trees play an important role as our weak classifier. They serve as the basic method." + ] + }, + { + "cell_type": "markdown", + "id": "9e6cdb09", + "metadata": { + "editable": true + }, + "source": [ + "## Tossing coins\n", + "\n", + "The simplest case is a so-called voting ensemble. To illustrate this,\n", + "think of yourself tossing coins with a biased outcome of 51 per cent\n", + "for heads and 49% for tails. With only few tosses,\n", + "you may not clearly see this distribution for heads and tails. However, after some\n", + "thousands of tosses, there will be a clear majority of heads. With 2000 tosses\n", + "you should see approximately 1020 heads and 980 tails.\n", + "\n", + "We can then state that the outcome is a clear majority of heads. If\n", + "you do this ten thousand times, it is easy to see that there is a 97%\n", + "likelihood of a majority of heads.\n", + "\n", + "Another example would be to collect all polls before an\n", + "election. Different polls may show different likelihoods for a\n", + "candidate winning with say a majority of the popular vote. The majority vote\n", + "would then consist in many polls indicating that this candidate will\n", + "actually win.\n", + "\n", + "The example here shows how we can implement the coin tossing case,\n", + "clealry demostrating that after some tosses we see the [law of large](https://en.wikipedia.org/wiki/Law_of_large_numbers)\n", + "numbers kicking in." + ] + }, + { + "cell_type": "markdown", + "id": "12af1c51", + "metadata": { + "editable": true + }, + "source": [ + "## Standard imports first" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "6375249b", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], + "source": [ + "# Common imports\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", + "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')" + ] + }, + { + "cell_type": "markdown", + "id": "c42c84e2", + "metadata": { + "editable": true + }, + "source": [ + "## Simple Voting Example, head or tail" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "94b4c1b2", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], + "source": [ + "\n", + "# Common imports\n", + "import numpy as np\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", + "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", + "id": "aaa45dc1", + "metadata": { + "editable": true + }, + "source": [ + "## Using the Voting Classifier\n", + "\n", + "We can use the voting classifier on other data sets, here the exciting binary case of two distinct objects using the make moons functionality of **Scikit-Learn**." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "0c4aa73d", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "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", + "id": "032bf507", + "metadata": { + "editable": true + }, + "source": [ + "## Voting and Bagging" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "19477fa5", + "metadata": { + "collapsed": false, + "editable": true + }, + "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": 16, + "id": "361aa57e", + "metadata": { + "collapsed": false, + "editable": true + }, + "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": 17, + "id": "04176e05", + "metadata": { + "collapsed": false, + "editable": true + }, + "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": 18, + "id": "c6ff39f5", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "id": "bff9075d", + "metadata": { + "editable": true + }, + "source": [ + "## 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." + ] + }, + { + "cell_type": "markdown", + "id": "d3104865", + "metadata": { + "editable": true + }, + "source": [ + "## 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." + ] + }, + { + "cell_type": "markdown", + "id": "7d1bff00", + "metadata": { + "editable": true + }, + "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": 19, + "id": "edefa4d0", + "metadata": { + "collapsed": false, + "editable": true + }, + "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", + "print(\"Simple tree:\",mse_simpletree)\n", + "plt.xlim(1,maxdepth)\n", + "plt.plot(polydegree, error, label='MSE')\n", + "plt.plot(polydegree, bias, label='bias')\n", + "plt.plot(polydegree, variance, label='Variance')\n", + "plt.legend()\n", + "save_fig(\"baggingboot\")\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "id": "6ff8fa07", "metadata": { "editable": true }, @@ -88,7 +1919,7 @@ }, { "cell_type": "markdown", - "id": "9d12a8f5", + "id": "aff6c150", "metadata": { "editable": true }, @@ -100,7 +1931,7 @@ }, { "cell_type": "markdown", - "id": "731a62d7", + "id": "ad5c9432", "metadata": { "editable": true }, @@ -125,7 +1956,7 @@ }, { "cell_type": "markdown", - "id": "2cf15238", + "id": "501c940d", "metadata": { "editable": true }, @@ -151,7 +1982,7 @@ }, { "cell_type": "markdown", - "id": "4cfbf57b", + "id": "8715191e", "metadata": { "editable": true }, @@ -161,16 +1992,14 @@ }, { "cell_type": "code", - "execution_count": 1, - "id": "b6e6fead", + "execution_count": 20, + "id": "f2de8809", "metadata": { "collapsed": false, "editable": true }, "outputs": [], "source": [ - "%matplotlib inline\n", - "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split \n", @@ -236,7 +2065,7 @@ }, { "cell_type": "markdown", - "id": "e12c1ebb", + "id": "65f2eaec", "metadata": { "editable": true }, @@ -252,7 +2081,7 @@ }, { "cell_type": "markdown", - "id": "de289fb0", + "id": "d1bb3982", "metadata": { "editable": true }, @@ -262,8 +2091,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "id": "61f294aa", + "execution_count": 21, + "id": "6d9e9ac7", "metadata": { "collapsed": false, "editable": true @@ -277,8 +2106,8 @@ }, { "cell_type": "code", - "execution_count": 3, - "id": "949a57bb", + "execution_count": 22, + "id": "270d38bd", "metadata": { "collapsed": false, "editable": true @@ -296,7 +2125,7 @@ }, { "cell_type": "markdown", - "id": "9964115b", + "id": "a56166a8", "metadata": { "editable": true }, @@ -316,7 +2145,7 @@ }, { "cell_type": "markdown", - "id": "606beb6a", + "id": "9061d6c8", "metadata": { "editable": true }, @@ -330,7 +2159,7 @@ }, { "cell_type": "markdown", - "id": "f0971ffc", + "id": "f041f04f", "metadata": { "editable": true }, @@ -342,7 +2171,7 @@ }, { "cell_type": "markdown", - "id": "9ee694c3", + "id": "4e95c9ca", "metadata": { "editable": true }, @@ -359,7 +2188,7 @@ }, { "cell_type": "markdown", - "id": "ada1b0de", + "id": "49a3b146", "metadata": { "editable": true }, @@ -371,7 +2200,7 @@ }, { "cell_type": "markdown", - "id": "0ba4bbcc", + "id": "04396798", "metadata": { "editable": true }, @@ -385,7 +2214,7 @@ }, { "cell_type": "markdown", - "id": "6371d9b5", + "id": "8fee917c", "metadata": { "editable": true }, @@ -397,7 +2226,7 @@ }, { "cell_type": "markdown", - "id": "d3c09380", + "id": "bdc751ec", "metadata": { "editable": true }, @@ -410,7 +2239,7 @@ }, { "cell_type": "markdown", - "id": "a4acf418", + "id": "93782b13", "metadata": { "editable": true }, @@ -422,7 +2251,7 @@ }, { "cell_type": "markdown", - "id": "b69d25a8", + "id": "433a5bd0", "metadata": { "editable": true }, @@ -432,7 +2261,7 @@ }, { "cell_type": "markdown", - "id": "c53ab67e", + "id": "3363d871", "metadata": { "editable": true }, @@ -460,7 +2289,7 @@ }, { "cell_type": "markdown", - "id": "d6802884", + "id": "11d66d8d", "metadata": { "editable": true }, @@ -476,7 +2305,7 @@ }, { "cell_type": "markdown", - "id": "7ffb7b48", + "id": "2bb642a1", "metadata": { "editable": true }, @@ -488,7 +2317,7 @@ }, { "cell_type": "markdown", - "id": "2f8b8867", + "id": "d17df5e6", "metadata": { "editable": true }, @@ -499,7 +2328,7 @@ }, { "cell_type": "markdown", - "id": "ad70c00e", + "id": "ee8f7bb1", "metadata": { "editable": true }, @@ -511,7 +2340,7 @@ }, { "cell_type": "markdown", - "id": "c88b1fd3", + "id": "04fef36a", "metadata": { "editable": true }, @@ -521,7 +2350,7 @@ }, { "cell_type": "markdown", - "id": "a65c29da", + "id": "8e4e0f55", "metadata": { "editable": true }, @@ -533,7 +2362,7 @@ }, { "cell_type": "markdown", - "id": "81d6b2fd", + "id": "ec2b10b4", "metadata": { "editable": true }, @@ -543,7 +2372,7 @@ }, { "cell_type": "markdown", - "id": "c8678f9d", + "id": "8e96a2fe", "metadata": { "editable": true }, @@ -555,7 +2384,7 @@ }, { "cell_type": "markdown", - "id": "be75d260", + "id": "2b149e92", "metadata": { "editable": true }, @@ -565,7 +2394,7 @@ }, { "cell_type": "markdown", - "id": "458a8344", + "id": "d24497e6", "metadata": { "editable": true }, @@ -577,7 +2406,7 @@ }, { "cell_type": "markdown", - "id": "db4a8210", + "id": "80fbbf7b", "metadata": { "editable": true }, @@ -591,7 +2420,7 @@ }, { "cell_type": "markdown", - "id": "7107486f", + "id": "a3976bc4", "metadata": { "editable": true }, @@ -607,7 +2436,7 @@ }, { "cell_type": "markdown", - "id": "47de65d9", + "id": "05934627", "metadata": { "editable": true }, @@ -619,7 +2448,7 @@ }, { "cell_type": "markdown", - "id": "1298c21b", + "id": "799246ca", "metadata": { "editable": true }, @@ -635,7 +2464,7 @@ }, { "cell_type": "markdown", - "id": "15793551", + "id": "d4a343c8", "metadata": { "editable": true }, @@ -647,7 +2476,7 @@ }, { "cell_type": "markdown", - "id": "ef10739c", + "id": "ec0f2fcb", "metadata": { "editable": true }, @@ -657,7 +2486,7 @@ }, { "cell_type": "markdown", - "id": "148e64cd", + "id": "1801906d", "metadata": { "editable": true }, @@ -669,7 +2498,7 @@ }, { "cell_type": "markdown", - "id": "205647de", + "id": "49e2b090", "metadata": { "editable": true }, @@ -681,7 +2510,7 @@ }, { "cell_type": "markdown", - "id": "35c5a976", + "id": "7f97fab7", "metadata": { "editable": true }, @@ -693,7 +2522,7 @@ }, { "cell_type": "markdown", - "id": "dc0bca89", + "id": "2cfb5301", "metadata": { "editable": true }, @@ -704,7 +2533,7 @@ }, { "cell_type": "markdown", - "id": "a2922943", + "id": "8349219c", "metadata": { "editable": true }, @@ -716,7 +2545,7 @@ }, { "cell_type": "markdown", - "id": "a52d5355", + "id": "4f888a41", "metadata": { "editable": true }, @@ -727,7 +2556,7 @@ }, { "cell_type": "markdown", - "id": "3904b837", + "id": "ab403ec2", "metadata": { "editable": true }, @@ -739,7 +2568,7 @@ }, { "cell_type": "markdown", - "id": "669f2965", + "id": "5d998c45", "metadata": { "editable": true }, @@ -749,7 +2578,7 @@ }, { "cell_type": "markdown", - "id": "9675d6dc", + "id": "0193596d", "metadata": { "editable": true }, @@ -761,7 +2590,7 @@ }, { "cell_type": "markdown", - "id": "3addfeb2", + "id": "fd31b59e", "metadata": { "editable": true }, @@ -773,7 +2602,7 @@ }, { "cell_type": "markdown", - "id": "189e0747", + "id": "7c9e1deb", "metadata": { "editable": true }, @@ -785,7 +2614,7 @@ }, { "cell_type": "markdown", - "id": "29488ac2", + "id": "26a8d426", "metadata": { "editable": true }, @@ -797,7 +2626,7 @@ }, { "cell_type": "markdown", - "id": "8db81846", + "id": "19047f88", "metadata": { "editable": true }, @@ -807,7 +2636,7 @@ }, { "cell_type": "markdown", - "id": "c85b9e02", + "id": "d0c5a257", "metadata": { "editable": true }, @@ -819,7 +2648,7 @@ }, { "cell_type": "markdown", - "id": "90343e23", + "id": "0be1ba7c", "metadata": { "editable": true }, @@ -829,7 +2658,7 @@ }, { "cell_type": "markdown", - "id": "a7edcae3", + "id": "65a67361", "metadata": { "editable": true }, @@ -841,7 +2670,7 @@ }, { "cell_type": "markdown", - "id": "9fb02a1b", + "id": "074f2715", "metadata": { "editable": true }, @@ -851,7 +2680,7 @@ }, { "cell_type": "markdown", - "id": "6b2b8ecf", + "id": "6b9cd07f", "metadata": { "editable": true }, @@ -863,7 +2692,7 @@ }, { "cell_type": "markdown", - "id": "9741f944", + "id": "9e2d6b48", "metadata": { "editable": true }, @@ -873,7 +2702,7 @@ }, { "cell_type": "markdown", - "id": "aa889a40", + "id": "62ccf0a7", "metadata": { "editable": true }, @@ -885,7 +2714,7 @@ }, { "cell_type": "markdown", - "id": "d8a14e57", + "id": "eb44a82b", "metadata": { "editable": true }, @@ -895,7 +2724,7 @@ }, { "cell_type": "markdown", - "id": "7ce0b025", + "id": "77d3b6b6", "metadata": { "editable": true }, @@ -907,7 +2736,7 @@ }, { "cell_type": "markdown", - "id": "94974c8e", + "id": "19833c4c", "metadata": { "editable": true }, @@ -927,7 +2756,7 @@ }, { "cell_type": "markdown", - "id": "f674f00b", + "id": "16a64fa9", "metadata": { "editable": true }, @@ -939,7 +2768,7 @@ }, { "cell_type": "markdown", - "id": "5a245a7d", + "id": "59f6c55e", "metadata": { "editable": true }, @@ -949,7 +2778,7 @@ }, { "cell_type": "markdown", - "id": "08740179", + "id": "2924f5c3", "metadata": { "editable": true }, @@ -965,7 +2794,7 @@ }, { "cell_type": "markdown", - "id": "66f44c4f", + "id": "90cb44df", "metadata": { "editable": true }, @@ -977,7 +2806,7 @@ }, { "cell_type": "markdown", - "id": "fcb119c9", + "id": "b717034b", "metadata": { "editable": true }, @@ -1005,7 +2834,7 @@ }, { "cell_type": "markdown", - "id": "f209128c", + "id": "69a1c3b2", "metadata": { "editable": true }, @@ -1017,8 +2846,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "id": "3d2d00ca", + "execution_count": 23, + "id": "d9765eb2", "metadata": { "collapsed": false, "editable": true @@ -1043,7 +2872,107 @@ }, { "cell_type": "markdown", - "id": "2cda2825", + "id": "c83a724d", + "metadata": { + "editable": true + }, + "source": [ + "## Making an ADAboost code yourself" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "1e813e36", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "class DecisionStump:\n", + " def fit(self, X, y, weights):\n", + " m, n = X.shape\n", + " self.alpha = 0\n", + " self.threshold = None\n", + " self.polarity = 1\n", + "\n", + " min_error = float('inf')\n", + "\n", + " for feature in range(n):\n", + " feature_values = np.unique(X[:, feature])\n", + "\n", + " for threshold in feature_values:\n", + " for polarity in [1, -1]:\n", + " predictions = np.ones(m)\n", + " predictions[X[:, feature] < threshold] = -1\n", + " predictions *= polarity\n", + "\n", + " error = sum(weights[predictions != y])\n", + "\n", + " if error < min_error:\n", + " min_error = error\n", + " self.alpha = 0.5 * np.log((1 - error) / (error + 1e-10))\n", + " self.threshold = threshold\n", + " self.feature_index = feature\n", + " self.polarity = polarity\n", + "\n", + " def predict(self, X):\n", + " m = X.shape[0]\n", + " predictions = np.ones(m)\n", + " if self.polarity == 1:\n", + " predictions[X[:, self.feature_index] < self.threshold] = -1\n", + " else:\n", + " predictions[X[:, self.feature_index] >= self.threshold] = -1\n", + " return predictions\n", + "\n", + "class AdaBoost:\n", + " def fit(self, X, y, n_estimators):\n", + " m = X.shape[0]\n", + " self.alphas = []\n", + " self.models = []\n", + "\n", + " weights = np.ones(m) / m\n", + "\n", + " for _ in range(n_estimators):\n", + " stump = DecisionStump()\n", + " stump.fit(X, y, weights)\n", + " predictions = stump.predict(X)\n", + "\n", + " error = sum(weights[predictions != y])\n", + " if error == 0:\n", + " break\n", + "\n", + " self.models.append(stump)\n", + " self.alphas.append(stump.alpha)\n", + "\n", + " weights *= np.exp(-stump.alpha * y * predictions)\n", + " weights /= np.sum(weights)\n", + "\n", + " def predict(self, X):\n", + " final_predictions = np.zeros(X.shape[0])\n", + " for alpha, model in zip(self.alphas, self.models):\n", + " final_predictions += alpha * model.predict(X)\n", + " return np.sign(final_predictions)\n", + "\n", + "# Example dataset (X, y)\n", + "X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])\n", + "y = np.array([-1, -1, -1, -1, 1, 1, 1, 1, 1, 1]) # Labels must be -1 or 1\n", + "\n", + "# Train AdaBoost\n", + "ada = AdaBoost()\n", + "ada.fit(X, y, n_estimators=10)\n", + "\n", + "# Predictions\n", + "predictions = ada.predict(X)\n", + "print(\"Predictions:\", predictions)" + ] + }, + { + "cell_type": "markdown", + "id": "6fd7e9e4", "metadata": { "editable": true }, @@ -1061,7 +2990,7 @@ }, { "cell_type": "markdown", - "id": "7c398707", + "id": "909ccb01", "metadata": { "editable": true }, @@ -1074,7 +3003,7 @@ }, { "cell_type": "markdown", - "id": "fc761c25", + "id": "b78e9b5e", "metadata": { "editable": true }, @@ -1086,7 +3015,7 @@ }, { "cell_type": "markdown", - "id": "4a78b015", + "id": "7c4fbf62", "metadata": { "editable": true }, @@ -1096,7 +3025,7 @@ }, { "cell_type": "markdown", - "id": "af3823da", + "id": "39741e57", "metadata": { "editable": true }, @@ -1108,7 +3037,7 @@ }, { "cell_type": "markdown", - "id": "337fa120", + "id": "65eba5f8", "metadata": { "editable": true }, @@ -1118,7 +3047,7 @@ }, { "cell_type": "markdown", - "id": "7eb4109d", + "id": "be094bbb", "metadata": { "editable": true }, @@ -1130,7 +3059,7 @@ }, { "cell_type": "markdown", - "id": "f5fc4800", + "id": "206a8a07", "metadata": { "editable": true }, @@ -1143,7 +3072,7 @@ }, { "cell_type": "markdown", - "id": "b6d8f355", + "id": "9aee47fb", "metadata": { "editable": true }, @@ -1155,7 +3084,7 @@ }, { "cell_type": "markdown", - "id": "58e310d7", + "id": "64a79891", "metadata": { "editable": true }, @@ -1167,7 +3096,7 @@ }, { "cell_type": "markdown", - "id": "a057d173", + "id": "aee21863", "metadata": { "editable": true }, @@ -1179,7 +3108,7 @@ }, { "cell_type": "markdown", - "id": "cd98348c", + "id": "3e74a92e", "metadata": { "editable": true }, @@ -1189,7 +3118,7 @@ }, { "cell_type": "markdown", - "id": "f95e2934", + "id": "6847f65a", "metadata": { "editable": true }, @@ -1201,7 +3130,7 @@ }, { "cell_type": "markdown", - "id": "1c2912fa", + "id": "a88e52a5", "metadata": { "editable": true }, @@ -1211,7 +3140,7 @@ }, { "cell_type": "markdown", - "id": "00ef4601", + "id": "24091bb6", "metadata": { "editable": true }, @@ -1227,7 +3156,7 @@ }, { "cell_type": "markdown", - "id": "2b1512f7", + "id": "90708c63", "metadata": { "editable": true }, @@ -1239,7 +3168,7 @@ }, { "cell_type": "markdown", - "id": "cc37b592", + "id": "4256d51b", "metadata": { "editable": true }, @@ -1260,7 +3189,7 @@ }, { "cell_type": "markdown", - "id": "268a79de", + "id": "e7599597", "metadata": { "editable": true }, @@ -1270,8 +3199,8 @@ }, { "cell_type": "code", - "execution_count": 5, - "id": "8a1059a0", + "execution_count": 25, + "id": "17685585", "metadata": { "collapsed": false, "editable": true @@ -1323,7 +3252,7 @@ }, { "cell_type": "markdown", - "id": "43552f84", + "id": "5b4d25ad", "metadata": { "editable": true }, @@ -1333,8 +3262,8 @@ }, { "cell_type": "code", - "execution_count": 6, - "id": "4cd4ea0e", + "execution_count": 26, + "id": "54e22258", "metadata": { "collapsed": false, "editable": true @@ -1385,7 +3314,7 @@ }, { "cell_type": "markdown", - "id": "334fcd69", + "id": "c3376d0e", "metadata": { "editable": true }, @@ -1408,7 +3337,7 @@ }, { "cell_type": "markdown", - "id": "aacc9c9a", + "id": "94fc7ffc", "metadata": { "editable": true }, @@ -1418,8 +3347,8 @@ }, { "cell_type": "code", - "execution_count": 7, - "id": "ed0345c3", + "execution_count": 27, + "id": "8587790e", "metadata": { "collapsed": false, "editable": true @@ -1471,7 +3400,7 @@ }, { "cell_type": "markdown", - "id": "5dc99bb1", + "id": "418026aa", "metadata": { "editable": true }, @@ -1483,8 +3412,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "id": "7b34fbc4", + "execution_count": 28, + "id": "49c68c87", "metadata": { "collapsed": false, "editable": true @@ -1544,6 +3473,104 @@ "save_fig(\"xgparams\")\n", "plt.show()" ] + }, + { + "cell_type": "markdown", + "id": "83b8ac13", + "metadata": { + "editable": true + }, + "source": [ + "## Gradient boosting, making our own code for a regression case" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "005435c1", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "class DecisionTreeRegressor:\n", + " def __init__(self, max_depth=3):\n", + " self.max_depth = max_depth\n", + " self.tree = None\n", + " def fit(self, X, y):\n", + " self.tree = self._grow_tree(X, y)\n", + " def _grow_tree(self, X, y, depth=0):\n", + " n_samples, n_features = X.shape\n", + " if depth < self.max_depth:\n", + " best_feature, best_threshold = self._best_split(X, y)\n", + " if best_feature is not None:\n", + " left_indices = X[:, best_feature] < best_threshold\n", + " right_indices = X[:, best_feature] >= best_threshold\n", + " left_child = self._grow_tree(X[left_indices], y[left_indices], depth + 1)\n", + " right_child = self._grow_tree(X[right_indices], y[right_indices], depth + 1)\n", + " return (best_feature, best_threshold, left_child, right_child)\n", + " return np.mean(y)\n", + " def _best_split(self, X, y):\n", + " best_mse = float('inf')\n", + " best_feature, best_threshold = None, None\n", + " n_samples, n_features = X.shape\n", + " \n", + " for feature in range(n_features):\n", + " thresholds = np.unique(X[:, feature])\n", + " for threshold in thresholds:\n", + " left_indices = X[:, feature] < threshold\n", + " right_indices = X[:, feature] >= threshold\n", + " if len(y[left_indices]) > 0 and len(y[right_indices]) > 0:\n", + " left_mse = np.mean((y[left_indices] - np.mean(y[left_indices])) ** 2)\n", + " right_mse = np.mean((y[right_indices] - np.mean(y[right_indices])) ** 2)\n", + " mse = (len(y[left_indices]) * left_mse + len(y[right_indices]) * right_mse) / n_samples\n", + " \n", + " if mse < best_mse:\n", + " best_mse = mse\n", + " best_feature = feature\n", + " best_threshold = threshold\n", + " return best_feature, best_threshold\n", + " def predict(self, X):\n", + " return np.array([self._predict_sample(sample, self.tree) for sample in X])\n", + " def _predict_sample(self, sample, node):\n", + " if isinstance(node, tuple):\n", + " feature, threshold, left_child, right_child = node\n", + " if sample[feature] < threshold:\n", + " return self._predict_sample(sample, left_child)\n", + " else:\n", + " return self._predict_sample(sample, right_child)\n", + " return node\n", + "class GradientBoostingRegressor:\n", + " def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):\n", + " self.n_estimators = n_estimators\n", + " self.learning_rate = learning_rate\n", + " self.max_depth = max_depth\n", + " self.models = []\n", + " def fit(self, X, y):\n", + " y_pred = np.zeros(y.shape)\n", + " for _ in range(self.n_estimators):\n", + " residuals = y - y_pred\n", + " model = DecisionTreeRegressor(max_depth=self.max_depth)\n", + " model.fit(X, residuals)\n", + " y_pred += self.learning_rate * model.predict(X)\n", + " self.models.append(model)\n", + " def predict(self, X):\n", + " y_pred = np.zeros(X.shape[0])\n", + " for model in self.models:\n", + " y_pred += self.learning_rate * model.predict(X)\n", + " return y_pred\n", + "# Example usage\n", + "if __name__ == \"__main__\":\n", + " # Sample data\n", + " X = np.array([[1], [2], [3], [4], [5]])\n", + " y = np.array([1.5, 1.7, 3.5, 3.7, 5.0])\n", + " model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=2)\n", + " model.fit(X, y)\n", + " predictions = model.predict(X)\n", + " print(\"Predictions:\", predictions)" + ] } ], "metadata": {}, diff --git a/doc/src/week47/DataFiles/bank.csv b/doc/src/week47/DataFiles/bank.csv new file mode 100644 index 000000000..930337395 --- /dev/null +++ b/doc/src/week47/DataFiles/bank.csv @@ -0,0 +1,1372 @@ +3.6216,8.6661,-2.8073,-0.44699,0 +4.5459,8.1674,-2.4586,-1.4621,0 +3.866,-2.6383,1.9242,0.10645,0 +3.4566,9.5228,-4.0112,-3.5944,0 +0.32924,-4.4552,4.5718,-0.9888,0 +4.3684,9.6718,-3.9606,-3.1625,0 +3.5912,3.0129,0.72888,0.56421,0 +2.0922,-6.81,8.4636,-0.60216,0 +3.2032,5.7588,-0.75345,-0.61251,0 +1.5356,9.1772,-2.2718,-0.73535,0 +1.2247,8.7779,-2.2135,-0.80647,0 +3.9899,-2.7066,2.3946,0.86291,0 +1.8993,7.6625,0.15394,-3.1108,0 +-1.5768,10.843,2.5462,-2.9362,0 +3.404,8.7261,-2.9915,-0.57242,0 +4.6765,-3.3895,3.4896,1.4771,0 +2.6719,3.0646,0.37158,0.58619,0 +0.80355,2.8473,4.3439,0.6017,0 +1.4479,-4.8794,8.3428,-2.1086,0 +5.2423,11.0272,-4.353,-4.1013,0 +5.7867,7.8902,-2.6196,-0.48708,0 +0.3292,-4.4552,4.5718,-0.9888,0 +3.9362,10.1622,-3.8235,-4.0172,0 +0.93584,8.8855,-1.6831,-1.6599,0 +4.4338,9.887,-4.6795,-3.7483,0 +0.7057,-5.4981,8.3368,-2.8715,0 +1.1432,-3.7413,5.5777,-0.63578,0 +-0.38214,8.3909,2.1624,-3.7405,0 +6.5633,9.8187,-4.4113,-3.2258,0 +4.8906,-3.3584,3.4202,1.0905,0 +-0.24811,-0.17797,4.9068,0.15429,0 +1.4884,3.6274,3.308,0.48921,0 +4.2969,7.617,-2.3874,-0.96164,0 +-0.96511,9.4111,1.7305,-4.8629,0 +-1.6162,0.80908,8.1628,0.60817,0 +2.4391,6.4417,-0.80743,-0.69139,0 +2.6881,6.0195,-0.46641,-0.69268,0 +3.6289,0.81322,1.6277,0.77627,0 +4.5679,3.1929,-2.1055,0.29653,0 +3.4805,9.7008,-3.7541,-3.4379,0 +4.1711,8.722,-3.0224,-0.59699,0 +-0.2062,9.2207,-3.7044,-6.8103,0 +-0.0068919,9.2931,-0.41243,-1.9638,0 +0.96441,5.8395,2.3235,0.066365,0 +2.8561,6.9176,-0.79372,0.48403,0 +-0.7869,9.5663,-3.7867,-7.5034,0 +2.0843,6.6258,0.48382,-2.2134,0 +-0.7869,9.5663,-3.7867,-7.5034,0 +3.9102,6.065,-2.4534,-0.68234,0 +1.6349,3.286,2.8753,0.087054,0 +4.3239,-4.8835,3.4356,-0.5776,0 +5.262,3.9834,-1.5572,1.0103,0 +3.1452,5.825,-0.51439,-1.4944,0 +2.549,6.1499,-1.1605,-1.2371,0 +4.9264,5.496,-2.4774,-0.50648,0 +4.8265,0.80287,1.6371,1.1875,0 +2.5635,6.7769,-0.61979,0.38576,0 +5.807,5.0097,-2.2384,0.43878,0 +3.1377,-4.1096,4.5701,0.98963,0 +-0.78289,11.3603,-0.37644,-7.0495,0 +2.888,0.44696,4.5907,-0.24398,0 +0.49665,5.527,1.7785,-0.47156,0 +4.2586,11.2962,-4.0943,-4.3457,0 +1.7939,-1.1174,1.5454,-0.26079,0 +5.4021,3.1039,-1.1536,1.5651,0 +2.5367,2.599,2.0938,0.20085,0 +4.6054,-4.0765,2.7587,0.31981,0 +2.4235,9.5332,-3.0789,-2.7746,0 +1.0009,7.7846,-0.28219,-2.6608,0 +0.12326,8.9848,-0.9351,-2.4332,0 +3.9529,-2.3548,2.3792,0.48274,0 +4.1373,0.49248,1.093,1.8276,0 +4.7181,10.0153,-3.9486,-3.8582,0 +4.1654,-3.4495,3.643,1.0879,0 +4.4069,10.9072,-4.5775,-4.4271,0 +2.3066,3.5364,0.57551,0.41938,0 +3.7935,7.9853,-2.5477,-1.872,0 +0.049175,6.1437,1.7828,-0.72113,0 +0.24835,7.6439,0.9885,-0.87371,0 +1.1317,3.9647,3.3979,0.84351,0 +2.8033,9.0862,-3.3668,-1.0224,0 +4.4682,2.2907,0.95766,0.83058,0 +5.0185,8.5978,-2.9375,-1.281,0 +1.8664,7.7763,-0.23849,-2.9634,0 +3.245,6.63,-0.63435,0.86937,0 +4.0296,2.6756,0.80685,0.71679,0 +-1.1313,1.9037,7.5339,1.022,0 +0.87603,6.8141,0.84198,-0.17156,0 +4.1197,-2.7956,2.0707,0.67412,0 +3.8027,0.81529,2.1041,1.0245,0 +1.4806,7.6377,-2.7876,-1.0341,0 +4.0632,3.584,0.72545,0.39481,0 +4.3064,8.2068,-2.7824,-1.4336,0 +2.4486,-6.3175,7.9632,0.20602,0 +3.2718,1.7837,2.1161,0.61334,0 +-0.64472,-4.6062,8.347,-2.7099,0 +2.9543,1.076,0.64577,0.89394,0 +2.1616,-6.8804,8.1517,-0.081048,0 +3.82,10.9279,-4.0112,-5.0284,0 +-2.7419,11.4038,2.5394,-5.5793,0 +3.3669,-5.1856,3.6935,-1.1427,0 +4.5597,-2.4211,2.6413,1.6168,0 +5.1129,-0.49871,0.62863,1.1189,0 +3.3397,-4.6145,3.9823,-0.23751,0 +4.2027,0.22761,0.96108,0.97282,0 +3.5438,1.2395,1.997,2.1547,0 +2.3136,10.6651,-3.5288,-4.7672,0 +-1.8584,7.886,-1.6643,-1.8384,0 +3.106,9.5414,-4.2536,-4.003,0 +2.9163,10.8306,-3.3437,-4.122,0 +3.9922,-4.4676,3.7304,-0.1095,0 +1.518,5.6946,0.094818,-0.026738,0 +3.2351,9.647,-3.2074,-2.5948,0 +4.2188,6.8162,-1.2804,0.76076,0 +1.7819,6.9176,-1.2744,-1.5759,0 +2.5331,2.9135,-0.822,-0.12243,0 +3.8969,7.4163,-1.8245,0.14007,0 +2.108,6.7955,-0.1708,0.4905,0 +2.8969,0.70768,2.29,1.8663,0 +0.9297,-3.7971,4.6429,-0.2957,0 +3.4642,10.6878,-3.4071,-4.109,0 +4.0713,10.4023,-4.1722,-4.7582,0 +-1.4572,9.1214,1.7425,-5.1241,0 +-1.5075,1.9224,7.1466,0.89136,0 +-0.91718,9.9884,1.1804,-5.2263,0 +2.994,7.2011,-1.2153,0.3211,0 +-2.343,12.9516,3.3285,-5.9426,0 +3.7818,-2.8846,2.2558,-0.15734,0 +4.6689,1.3098,0.055404,1.909,0 +3.4663,1.1112,1.7425,1.3388,0 +3.2697,-4.3414,3.6884,-0.29829,0 +5.1302,8.6703,-2.8913,-1.5086,0 +2.0139,6.1416,0.37929,0.56938,0 +0.4339,5.5395,2.033,-0.40432,0 +-1.0401,9.3987,0.85998,-5.3336,0 +4.1605,11.2196,-3.6136,-4.0819,0 +5.438,9.4669,-4.9417,-3.9202,0 +5.032,8.2026,-2.6256,-1.0341,0 +5.2418,10.5388,-4.1174,-4.2797,0 +-0.2062,9.2207,-3.7044,-6.8103,0 +2.0911,0.94358,4.5512,1.234,0 +1.7317,-0.34765,4.1905,-0.99138,0 +4.1736,3.3336,-1.4244,0.60429,0 +3.9232,-3.2467,3.4579,0.83705,0 +3.8481,10.1539,-3.8561,-4.2228,0 +0.5195,-3.2633,3.0895,-0.9849,0 +3.8584,0.78425,1.1033,1.7008,0 +1.7496,-0.1759,5.1827,1.2922,0 +3.6277,0.9829,0.68861,0.63403,0 +2.7391,7.4018,0.071684,-2.5302,0 +4.5447,8.2274,-2.4166,-1.5875,0 +-1.7599,11.9211,2.6756,-3.3241,0 +5.0691,0.21313,0.20278,1.2095,0 +3.4591,11.112,-4.2039,-5.0931,0 +1.9358,8.1654,-0.023425,-2.2586,0 +2.486,-0.99533,5.3404,-0.15475,0 +2.4226,-4.5752,5.947,0.21507,0 +3.9479,-3.7723,2.883,0.019813,0 +2.2634,-4.4862,3.6558,-0.61251,0 +1.3566,4.2358,2.1341,0.3211,0 +5.0452,3.8964,-1.4304,0.86291,0 +3.5499,8.6165,-3.2794,-1.2009,0 +0.17346,7.8695,0.26876,-3.7883,0 +2.4008,9.3593,-3.3565,-3.3526,0 +4.8851,1.5995,-0.00029081,1.6401,0 +4.1927,-3.2674,2.5839,0.21766,0 +1.1166,8.6496,-0.96252,-1.8112,0 +1.0235,6.901,-2.0062,-2.7125,0 +-1.803,11.8818,2.0458,-5.2728,0 +0.11739,6.2761,-1.5495,-2.4746,0 +0.5706,-0.0248,1.2421,-0.5621,0 +4.0552,-2.4583,2.2806,1.0323,0 +-1.6952,1.0657,8.8294,0.94955,0 +-1.1193,10.7271,2.0938,-5.6504,0 +1.8799,2.4707,2.4931,0.37671,0 +3.583,-3.7971,3.4391,-0.12501,0 +0.19081,9.1297,-3.725,-5.8224,0 +3.6582,5.6864,-1.7157,-0.23751,0 +-0.13144,-1.7775,8.3316,0.35214,0 +2.3925,9.798,-3.0361,-2.8224,0 +1.6426,3.0149,0.22849,-0.147,0 +-0.11783,-1.5789,8.03,-0.028031,0 +-0.69572,8.6165,1.8419,-4.3289,0 +2.9421,7.4101,-0.97709,-0.88406,0 +-1.7559,11.9459,3.0946,-4.8978,0 +-1.2537,10.8803,1.931,-4.3237,0 +3.2585,-4.4614,3.8024,-0.15087,0 +1.8314,6.3672,-0.036278,0.049554,0 +4.5645,-3.6275,2.8684,0.27714,0 +2.7365,-5.0325,6.6608,-0.57889,0 +0.9297,-3.7971,4.6429,-0.2957,0 +3.9663,10.1684,-4.1131,-4.6056,0 +1.4578,-0.08485,4.1785,0.59136,0 +4.8272,3.0687,0.68604,0.80731,0 +-2.341,12.3784,0.70403,-7.5836,0 +-1.8584,7.886,-1.6643,-1.8384,0 +4.1454,7.257,-1.9153,-0.86078,0 +1.9157,6.0816,0.23705,-2.0116,0 +4.0215,-2.1914,2.4648,1.1409,0 +5.8862,5.8747,-2.8167,-0.30087,0 +-2.0897,10.8265,2.3603,-3.4198,0 +4.0026,-3.5943,3.5573,0.26809,0 +-0.78689,9.5663,-3.7867,-7.5034,0 +4.1757,10.2615,-3.8552,-4.3056,0 +0.83292,7.5404,0.65005,-0.92544,0 +4.8077,2.2327,-0.26334,1.5534,0 +5.3063,5.2684,-2.8904,-0.52716,0 +2.5605,9.2683,-3.5913,-1.356,0 +2.1059,7.6046,-0.47755,-1.8461,0 +2.1721,-0.73874,5.4672,-0.72371,0 +4.2899,9.1814,-4.6067,-4.3263,0 +3.5156,10.1891,-4.2759,-4.978,0 +2.614,8.0081,-3.7258,-1.3069,0 +0.68087,2.3259,4.9085,0.54998,0 +4.1962,0.74493,0.83256,0.753,0 +6.0919,2.9673,-1.3267,1.4551,0 +1.3234,3.2964,0.2362,-0.11984,0 +1.3264,1.0326,5.6566,-0.41337,0 +-0.16735,7.6274,1.2061,-3.6241,0 +-1.3,10.2678,-2.953,-5.8638,0 +-2.2261,12.5398,2.9438,-3.5258,0 +2.4196,6.4665,-0.75688,0.228,0 +1.0987,0.6394,5.989,-0.58277,0 +4.6464,10.5326,-4.5852,-4.206,0 +-0.36038,4.1158,3.1143,-0.37199,0 +1.3562,3.2136,4.3465,0.78662,0 +0.5706,-0.0248,1.2421,-0.5621,0 +-2.6479,10.1374,-1.331,-5.4707,0 +3.1219,-3.137,1.9259,-0.37458,0 +5.4944,1.5478,0.041694,1.9284,0 +-1.3389,1.552,7.0806,1.031,0 +-2.3361,11.9604,3.0835,-5.4435,0 +2.2596,-0.033118,4.7355,-0.2776,0 +0.46901,-0.63321,7.3848,0.36507,0 +2.7296,2.8701,0.51124,0.5099,0 +2.0466,2.03,2.1761,-0.083634,0 +-1.3274,9.498,2.4408,-5.2689,0 +3.8905,-2.1521,2.6302,1.1047,0 +3.9994,0.90427,1.1693,1.6892,0 +2.3952,9.5083,-3.1783,-3.0086,0 +3.2704,6.9321,-1.0456,0.23447,0 +-1.3931,1.5664,7.5382,0.78403,0 +1.6406,3.5488,1.3964,-0.36424,0 +2.7744,6.8576,-1.0671,0.075416,0 +2.4287,9.3821,-3.2477,-1.4543,0 +4.2134,-2.806,2.0116,0.67412,0 +1.6472,0.48213,4.7449,1.225,0 +2.0597,-0.99326,5.2119,-0.29312,0 +0.3798,0.7098,0.7572,-0.4444,0 +1.0135,8.4551,-1.672,-2.0815,0 +4.5691,-4.4552,3.1769,0.0042961,0 +0.57461,10.1105,-1.6917,-4.3922,0 +0.5734,9.1938,-0.9094,-1.872,0 +5.2868,3.257,-1.3721,1.1668,0 +4.0102,10.6568,-4.1388,-5.0646,0 +4.1425,-3.6792,3.8281,1.6297,0 +3.0934,-2.9177,2.2232,0.22283,0 +2.2034,5.9947,0.53009,0.84998,0 +3.744,0.79459,0.95851,1.0077,0 +3.0329,2.2948,2.1135,0.35084,0 +3.7731,7.2073,-1.6814,-0.94742,0 +3.1557,2.8908,0.59693,0.79825,0 +1.8114,7.6067,-0.9788,-2.4668,0 +4.988,7.2052,-3.2846,-1.1608,0 +2.483,6.6155,-0.79287,-0.90863,0 +1.594,4.7055,1.3758,0.081882,0 +-0.016103,9.7484,0.15394,-1.6134,0 +3.8496,9.7939,-4.1508,-4.4582,0 +0.9297,-3.7971,4.6429,-0.2957,0 +4.9342,2.4107,-0.17594,1.6245,0 +3.8417,10.0215,-4.2699,-4.9159,0 +5.3915,9.9946,-3.8081,-3.3642,0 +4.4072,-0.070365,2.0416,1.1319,0 +2.6946,6.7976,-0.40301,0.44912,0 +5.2756,0.13863,0.12138,1.1435,0 +3.4312,6.2637,-1.9513,-0.36165,0 +4.052,-0.16555,0.45383,0.51248,0 +1.3638,-4.7759,8.4182,-1.8836,0 +0.89566,7.7763,-2.7473,-1.9353,0 +1.9265,7.7557,-0.16823,-3.0771,0 +0.20977,-0.46146,7.7267,0.90946,0 +4.068,-2.9363,2.1992,0.50084,0 +2.877,-4.0599,3.6259,-0.32544,0 +0.3223,-0.89808,8.0883,0.69222,0 +-1.3,10.2678,-2.953,-5.8638,0 +1.7747,-6.4334,8.15,-0.89828,0 +1.3419,-4.4221,8.09,-1.7349,0 +0.89606,10.5471,-1.4175,-4.0327,0 +0.44125,2.9487,4.3225,0.7155,0 +3.2422,6.2265,0.12224,-1.4466,0 +2.5678,3.5136,0.61406,-0.40691,0 +-2.2153,11.9625,0.078538,-7.7853,0 +4.1349,6.1189,-2.4294,-0.19613,0 +1.934,-9.2828e-06,4.816,-0.33967,0 +2.5068,1.1588,3.9249,0.12585,0 +2.1464,6.0795,-0.5778,-2.2302,0 +0.051979,7.0521,-2.0541,-3.1508,0 +1.2706,8.035,-0.19651,-2.1888,0 +1.143,0.83391,5.4552,-0.56984,0 +2.2928,9.0386,-3.2417,-1.2991,0 +0.3292,-4.4552,4.5718,-0.9888,0 +2.9719,6.8369,-0.2702,0.71291,0 +1.6849,8.7489,-1.2641,-1.3858,0 +-1.9177,11.6894,2.5454,-3.2763,0 +2.3729,10.4726,-3.0087,-3.2013,0 +1.0284,9.767,-1.3687,-1.7853,0 +0.27451,9.2186,-3.2863,-4.8448,0 +1.6032,-4.7863,8.5193,-2.1203,0 +4.616,10.1788,-4.2185,-4.4245,0 +4.2478,7.6956,-2.7696,-1.0767,0 +4.0215,-2.7004,2.4957,0.36636,0 +5.0297,-4.9704,3.5025,-0.23751,0 +1.5902,2.2948,3.2403,0.18404,0 +2.1274,5.1939,-1.7971,-1.1763,0 +1.1811,8.3847,-2.0567,-0.90345,0 +0.3292,-4.4552,4.5718,-0.9888,0 +5.7353,5.2808,-2.2598,0.075416,0 +2.6718,5.6574,0.72974,-1.4892,0 +1.5799,-4.7076,7.9186,-1.5487,0 +2.9499,2.2493,1.3458,-0.037083,0 +0.5195,-3.2633,3.0895,-0.9849,0 +3.7352,9.5911,-3.9032,-3.3487,0 +-1.7344,2.0175,7.7618,0.93532,0 +3.884,10.0277,-3.9298,-4.0819,0 +3.5257,1.2829,1.9276,1.7991,0 +4.4549,2.4976,1.0313,0.96894,0 +-0.16108,-6.4624,8.3573,-1.5216,0 +4.2164,9.4607,-4.9288,-5.2366,0 +3.5152,6.8224,-0.67377,-0.46898,0 +1.6988,2.9094,2.9044,0.11033,0 +1.0607,2.4542,2.5188,-0.17027,0 +2.0421,1.2436,4.2171,0.90429,0 +3.5594,1.3078,1.291,1.6556,0 +3.0009,5.8126,-2.2306,-0.66553,0 +3.9294,1.4112,1.8076,0.89782,0 +3.4667,-4.0724,4.2882,1.5418,0 +3.966,3.9213,0.70574,0.33662,0 +1.0191,2.33,4.9334,0.82929,0 +0.96414,5.616,2.2138,-0.12501,0 +1.8205,6.7562,0.0099913,0.39481,0 +4.9923,7.8653,-2.3515,-0.71984,0 +-1.1804,11.5093,0.15565,-6.8194,0 +4.0329,0.23175,0.89082,1.1823,0 +0.66018,10.3878,-1.4029,-3.9151,0 +3.5982,7.1307,-1.3035,0.21248,0 +-1.8584,7.886,-1.6643,-1.8384,0 +4.0972,0.46972,1.6671,0.91593,0 +3.3299,0.91254,1.5806,0.39352,0 +3.1088,3.1122,0.80857,0.4336,0 +-4.2859,8.5234,3.1392,-0.91639,0 +-1.2528,10.2036,2.1787,-5.6038,0 +0.5195,-3.2633,3.0895,-0.9849,0 +0.3292,-4.4552,4.5718,-0.9888,0 +0.88872,5.3449,2.045,-0.19355,0 +3.5458,9.3718,-4.0351,-3.9564,0 +-0.21661,8.0329,1.8848,-3.8853,0 +2.7206,9.0821,-3.3111,-0.96811,0 +3.2051,8.6889,-2.9033,-0.7819,0 +2.6917,10.8161,-3.3,-4.2888,0 +-2.3242,11.5176,1.8231,-5.375,0 +2.7161,-4.2006,4.1914,0.16981,0 +3.3848,3.2674,0.90967,0.25128,0 +1.7452,4.8028,2.0878,0.62627,0 +2.805,0.57732,1.3424,1.2133,0 +5.7823,5.5788,-2.4089,-0.056479,0 +3.8999,1.734,1.6011,0.96765,0 +3.5189,6.332,-1.7791,-0.020273,0 +3.2294,7.7391,-0.37816,-2.5405,0 +3.4985,3.1639,0.22677,-0.1651,0 +2.1948,1.3781,1.1582,0.85774,0 +2.2526,9.9636,-3.1749,-2.9944,0 +4.1529,-3.9358,2.8633,-0.017686,0 +0.74307,11.17,-1.3824,-4.0728,0 +1.9105,8.871,-2.3386,-0.75604,0 +-1.5055,0.070346,6.8681,-0.50648,0 +0.58836,10.7727,-1.3884,-4.3276,0 +3.2303,7.8384,-3.5348,-1.2151,0 +-1.9922,11.6542,2.6542,-5.2107,0 +2.8523,9.0096,-3.761,-3.3371,0 +4.2772,2.4955,0.48554,0.36119,0 +1.5099,0.039307,6.2332,-0.30346,0 +5.4188,10.1457,-4.084,-3.6991,0 +0.86202,2.6963,4.2908,0.54739,0 +3.8117,10.1457,-4.0463,-4.5629,0 +0.54777,10.3754,-1.5435,-4.1633,0 +2.3718,7.4908,0.015989,-1.7414,0 +-2.4953,11.1472,1.9353,-3.4638,0 +4.6361,-2.6611,2.8358,1.1991,0 +-2.2527,11.5321,2.5899,-3.2737,0 +3.7982,10.423,-4.1602,-4.9728,0 +-0.36279,8.2895,-1.9213,-3.3332,0 +2.1265,6.8783,0.44784,-2.2224,0 +0.86736,5.5643,1.6765,-0.16769,0 +3.7831,10.0526,-3.8869,-3.7366,0 +-2.2623,12.1177,0.28846,-7.7581,0 +1.2616,4.4303,-1.3335,-1.7517,0 +2.6799,3.1349,0.34073,0.58489,0 +-0.39816,5.9781,1.3912,-1.1621,0 +4.3937,0.35798,2.0416,1.2004,0 +2.9695,5.6222,0.27561,-1.1556,0 +1.3049,-0.15521,6.4911,-0.75346,0 +2.2123,-5.8395,7.7687,-0.85302,0 +1.9647,6.9383,0.57722,0.66377,0 +3.0864,-2.5845,2.2309,0.30947,0 +0.3798,0.7098,0.7572,-0.4444,0 +0.58982,7.4266,1.2353,-2.9595,0 +0.14783,7.946,1.0742,-3.3409,0 +-0.062025,6.1975,1.099,-1.131,0 +4.223,1.1319,0.72202,0.96118,0 +0.64295,7.1018,0.3493,-0.41337,0 +1.941,0.46351,4.6472,1.0879,0 +4.0047,0.45937,1.3621,1.6181,0 +3.7767,9.7794,-3.9075,-3.5323,0 +3.4769,-0.15314,2.53,2.4495,0 +1.9818,9.2621,-3.521,-1.872,0 +3.8023,-3.8696,4.044,0.95343,0 +4.3483,11.1079,-4.0857,-4.2539,0 +1.1518,1.3864,5.2727,-0.43536,0 +-1.2576,1.5892,7.0078,0.42455,0 +1.9572,-5.1153,8.6127,-1.4297,0 +-2.484,12.1611,2.8204,-3.7418,0 +-1.1497,1.2954,7.701,0.62627,0 +4.8368,10.0132,-4.3239,-4.3276,0 +-0.12196,8.8068,0.94566,-4.2267,0 +1.9429,6.3961,0.092248,0.58102,0 +1.742,-4.809,8.2142,-2.0659,0 +-1.5222,10.8409,2.7827,-4.0974,0 +-1.3,10.2678,-2.953,-5.8638,0 +3.4246,-0.14693,0.80342,0.29136,0 +2.5503,-4.9518,6.3729,-0.41596,0 +1.5691,6.3465,-0.1828,-2.4099,0 +1.3087,4.9228,2.0013,0.22024,0 +5.1776,8.2316,-3.2511,-1.5694,0 +2.229,9.6325,-3.1123,-2.7164,0 +5.6272,10.0857,-4.2931,-3.8142,0 +1.2138,8.7986,-2.1672,-0.74182,0 +0.3798,0.7098,0.7572,-0.4444,0 +0.5415,6.0319,1.6825,-0.46122,0 +4.0524,5.6802,-1.9693,0.026279,0 +4.7285,2.1065,-0.28305,1.5625,0 +3.4359,0.66216,2.1041,1.8922,0 +0.86816,10.2429,-1.4912,-4.0082,0 +3.359,9.8022,-3.8209,-3.7133,0 +3.6702,2.9942,0.85141,0.30688,0 +1.3349,6.1189,0.46497,0.49826,0 +3.1887,-3.4143,2.7742,-0.2026,0 +2.4527,2.9653,0.20021,-0.056479,0 +3.9121,2.9735,0.92852,0.60558,0 +3.9364,10.5885,-3.725,-4.3133,0 +3.9414,-3.2902,3.1674,1.0866,0 +3.6922,-3.9585,4.3439,1.3517,0 +5.681,7.795,-2.6848,-0.92544,0 +0.77124,9.0862,-1.2281,-1.4996,0 +3.5761,9.7753,-3.9795,-3.4638,0 +1.602,6.1251,0.52924,0.47886,0 +2.6682,10.216,-3.4414,-4.0069,0 +2.0007,1.8644,2.6491,0.47369,0 +0.64215,3.1287,4.2933,0.64696,0 +4.3848,-3.0729,3.0423,1.2741,0 +0.77445,9.0552,-2.4089,-1.3884,0 +0.96574,8.393,-1.361,-1.4659,0 +3.0948,8.7324,-2.9007,-0.96682,0 +4.9362,7.6046,-2.3429,-0.85302,0 +-1.9458,11.2217,1.9079,-3.4405,0 +5.7403,-0.44284,0.38015,1.3763,0 +-2.6989,12.1984,0.67661,-8.5482,0 +1.1472,3.5985,1.9387,-0.43406,0 +2.9742,8.96,-2.9024,-1.0379,0 +4.5707,7.2094,-3.2794,-1.4944,0 +0.1848,6.5079,2.0133,-0.87242,0 +0.87256,9.2931,-0.7843,-2.1978,0 +0.39559,6.8866,1.0588,-0.67587,0 +3.8384,6.1851,-2.0439,-0.033204,0 +2.8209,7.3108,-0.81857,-1.8784,0 +2.5817,9.7546,-3.1749,-2.9957,0 +3.8213,0.23175,2.0133,2.0564,0 +0.3798,0.7098,0.7572,-0.4444,0 +3.4893,6.69,-1.2042,-0.38751,0 +-1.7781,0.8546,7.1303,0.027572,0 +2.0962,2.4769,1.9379,-0.040962,0 +0.94732,-0.57113,7.1903,-0.67587,0 +2.8261,9.4007,-3.3034,-1.0509,0 +0.0071249,8.3661,0.50781,-3.8155,0 +0.96788,7.1907,1.2798,-2.4565,0 +4.7432,2.1086,0.1368,1.6543,0 +3.6575,7.2797,-2.2692,-1.144,0 +3.8832,6.4023,-2.432,-0.98363,0 +3.4776,8.811,-3.1886,-0.92285,0 +1.1315,7.9212,1.093,-2.8444,0 +2.8237,2.8597,0.19678,0.57196,0 +1.9321,6.0423,0.26019,-2.053,0 +3.0632,-3.3315,5.1305,0.8267,0 +-1.8411,10.8306,2.769,-3.0901,0 +2.8084,11.3045,-3.3394,-4.4194,0 +2.5698,-4.4076,5.9856,0.078002,0 +-0.12624,10.3216,-3.7121,-6.1185,0 +3.3756,-4.0951,4.367,1.0698,0 +-0.048008,-1.6037,8.4756,0.75558,0 +0.5706,-0.0248,1.2421,-0.5621,0 +0.88444,6.5906,0.55837,-0.44182,0 +3.8644,3.7061,0.70403,0.35214,0 +1.2999,2.5762,2.0107,-0.18967,0 +2.0051,-6.8638,8.132,-0.2401,0 +4.9294,0.27727,0.20792,0.33662,0 +2.8297,6.3485,-0.73546,-0.58665,0 +2.565,8.633,-2.9941,-1.3082,0 +2.093,8.3061,0.022844,-3.2724,0 +4.6014,5.6264,-2.1235,0.19309,0 +5.0617,-0.35799,0.44698,0.99868,0 +-0.2951,9.0489,-0.52725,-2.0789,0 +3.577,2.4004,1.8908,0.73231,0 +3.9433,2.5017,1.5215,0.903,0 +2.6648,10.754,-3.3994,-4.1685,0 +5.9374,6.1664,-2.5905,-0.36553,0 +2.0153,1.8479,3.1375,0.42843,0 +5.8782,5.9409,-2.8544,-0.60863,0 +-2.3983,12.606,2.9464,-5.7888,0 +1.762,4.3682,2.1384,0.75429,0 +4.2406,-2.4852,1.608,0.7155,0 +3.4669,6.87,-1.0568,-0.73147,0 +3.1896,5.7526,-0.18537,-0.30087,0 +0.81356,9.1566,-2.1492,-4.1814,0 +0.52855,0.96427,4.0243,-1.0483,0 +2.1319,-2.0403,2.5574,-0.061652,0 +0.33111,4.5731,2.057,-0.18967,0 +1.2746,8.8172,-1.5323,-1.7957,0 +2.2091,7.4556,-1.3284,-3.3021,0 +2.5328,7.528,-0.41929,-2.6478,0 +3.6244,1.4609,1.3501,1.9284,0 +-1.3885,12.5026,0.69118,-7.5487,0 +5.7227,5.8312,-2.4097,-0.24527,0 +3.3583,10.3567,-3.7301,-3.6991,0 +2.5227,2.2369,2.7236,0.79438,0 +0.045304,6.7334,1.0708,-0.9332,0 +4.8278,7.7598,-2.4491,-1.2216,0 +1.9476,-4.7738,8.527,-1.8668,0 +2.7659,0.66216,4.1494,-0.28406,0 +-0.10648,-0.76771,7.7575,0.64179,0 +0.72252,-0.053811,5.6703,-1.3509,0 +4.2475,1.4816,-0.48355,0.95343,0 +3.9772,0.33521,2.2566,2.1625,0 +3.6667,4.302,0.55923,0.33791,0 +2.8232,10.8513,-3.1466,-3.9784,0 +-1.4217,11.6542,-0.057699,-7.1025,0 +4.2458,1.1981,0.66633,0.94696,0 +4.1038,-4.8069,3.3491,-0.49225,0 +1.4507,8.7903,-2.2324,-0.65259,0 +3.4647,-3.9172,3.9746,0.36119,0 +1.8533,6.1458,1.0176,-2.0401,0 +3.5288,0.71596,1.9507,1.9375,0 +3.9719,1.0367,0.75973,1.0013,0 +3.534,9.3614,-3.6316,-1.2461,0 +3.6894,9.887,-4.0788,-4.3664,0 +3.0672,-4.4117,3.8238,-0.81682,0 +2.6463,-4.8152,6.3549,0.003003,0 +2.2893,3.733,0.6312,-0.39786,0 +1.5673,7.9274,-0.056842,-2.1694,0 +4.0405,0.51524,1.0279,1.106,0 +4.3846,-4.8794,3.3662,-0.029324,0 +2.0165,-0.25246,5.1707,1.0763,0 +4.0446,11.1741,-4.3582,-4.7401,0 +-0.33729,-0.64976,7.6659,0.72326,0 +-2.4604,12.7302,0.91738,-7.6418,0 +4.1195,10.9258,-3.8929,-4.1802,0 +2.0193,0.82356,4.6369,1.4202,0 +1.5701,7.9129,0.29018,-2.1953,0 +2.6415,7.586,-0.28562,-1.6677,0 +5.0214,8.0764,-3.0515,-1.7155,0 +4.3435,3.3295,0.83598,0.64955,0 +1.8238,-6.7748,8.3873,-0.54139,0 +3.9382,0.9291,0.78543,0.6767,0 +2.2517,-5.1422,4.2916,-1.2487,0 +5.504,10.3671,-4.413,-4.0211,0 +2.8521,9.171,-3.6461,-1.2047,0 +1.1676,9.1566,-2.0867,-0.80647,0 +2.6104,8.0081,-0.23592,-1.7608,0 +0.32444,10.067,-1.1982,-4.1284,0 +3.8962,-4.7904,3.3954,-0.53751,0 +2.1752,-0.8091,5.1022,-0.67975,0 +1.1588,8.9331,-2.0807,-1.1272,0 +4.7072,8.2957,-2.5605,-1.4905,0 +-1.9667,11.8052,-0.40472,-7.8719,0 +4.0552,0.40143,1.4563,0.65343,0 +2.3678,-6.839,8.4207,-0.44829,0 +0.33565,6.8369,0.69718,-0.55691,0 +4.3398,-5.3036,3.8803,-0.70432,0 +1.5456,8.5482,0.4187,-2.1784,0 +1.4276,8.3847,-2.0995,-1.9677,0 +-0.27802,8.1881,-3.1338,-2.5276,0 +0.93611,8.6413,-1.6351,-1.3043,0 +4.6352,-3.0087,2.6773,1.212,0 +1.5268,-5.5871,8.6564,-1.722,0 +0.95626,2.4728,4.4578,0.21636,0 +-2.7914,1.7734,6.7756,-0.39915,0 +5.2032,3.5116,-1.2538,1.0129,0 +3.1836,7.2321,-1.0713,-2.5909,0 +0.65497,5.1815,1.0673,-0.42113,0 +5.6084,10.3009,-4.8003,-4.3534,0 +1.105,7.4432,0.41099,-3.0332,0 +3.9292,-2.9156,2.2129,0.30817,0 +1.1558,6.4003,1.5506,0.6961,0 +2.5581,2.6218,1.8513,0.40257,0 +2.7831,10.9796,-3.557,-4.4039,0 +3.7635,2.7811,0.66119,0.34179,0 +-2.6479,10.1374,-1.331,-5.4707,0 +1.0652,8.3682,-1.4004,-1.6509,0 +-1.4275,11.8797,0.41613,-6.9978,0 +5.7456,10.1808,-4.7857,-4.3366,0 +5.086,3.2798,-1.2701,1.1189,0 +3.4092,5.4049,-2.5228,-0.89958,0 +-0.2361,9.3221,2.1307,-4.3793,0 +3.8197,8.9951,-4.383,-4.0327,0 +-1.1391,1.8127,6.9144,0.70127,0 +4.9249,0.68906,0.77344,1.2095,0 +2.5089,6.841,-0.029423,0.44912,0 +-0.2062,9.2207,-3.7044,-6.8103,0 +3.946,6.8514,-1.5443,-0.5582,0 +-0.278,8.1881,-3.1338,-2.5276,0 +1.8592,3.2074,-0.15966,-0.26208,0 +0.56953,7.6294,1.5754,-3.2233,0 +3.4626,-4.449,3.5427,0.15429,0 +3.3951,1.1484,2.1401,2.0862,0 +5.0429,-0.52974,0.50439,1.106,0 +3.7758,7.1783,-1.5195,0.40128,0 +4.6562,7.6398,-2.4243,-1.2384,0 +4.0948,-2.9674,2.3689,0.75429,0 +1.8384,6.063,0.54723,0.51248,0 +2.0153,0.43661,4.5864,-0.3151,0 +3.5251,0.7201,1.6928,0.64438,0 +3.757,-5.4236,3.8255,-1.2526,0 +2.5989,3.5178,0.7623,0.81119,0 +1.8994,0.97462,4.2265,0.81377,0 +3.6941,-3.9482,4.2625,1.1577,0 +4.4295,-2.3507,1.7048,0.90946,0 +6.8248,5.2187,-2.5425,0.5461,0 +1.8967,-2.5163,2.8093,-0.79742,0 +2.1526,-6.1665,8.0831,-0.34355,0 +3.3004,7.0811,-1.3258,0.22283,0 +2.7213,7.05,-0.58808,0.41809,0 +3.8846,-3.0336,2.5334,0.20214,0 +4.1665,-0.4449,0.23448,0.27843,0 +0.94225,5.8561,1.8762,-0.32544,0 +5.1321,-0.031048,0.32616,1.1151,0 +0.38251,6.8121,1.8128,-0.61251,0 +3.0333,-2.5928,2.3183,0.303,0 +2.9233,6.0464,-0.11168,-0.58665,0 +1.162,10.2926,-1.2821,-4.0392,0 +3.7791,2.5762,1.3098,0.5655,0 +0.77765,5.9781,1.1941,-0.3526,0 +-0.38388,-1.0471,8.0514,0.49567,0 +0.21084,9.4359,-0.094543,-1.859,0 +2.9571,-4.5938,5.9068,0.57196,0 +4.6439,-3.3729,2.5976,0.55257,0 +3.3577,-4.3062,6.0241,0.18274,0 +3.5127,2.9073,1.0579,0.40774,0 +2.6562,10.7044,-3.3085,-4.0767,0 +-1.3612,10.694,1.7022,-2.9026,0 +-0.278,8.1881,-3.1338,-2.5276,0 +1.04,-6.9321,8.2888,-1.2991,0 +2.1881,2.7356,1.3278,-0.1832,0 +4.2756,-2.6528,2.1375,0.94437,0 +-0.11996,6.8741,0.91995,-0.6694,0 +2.9736,8.7944,-3.6359,-1.3754,0 +3.7798,-3.3109,2.6491,0.066365,0 +5.3586,3.7557,-1.7345,1.0789,0 +1.8373,6.1292,0.84027,0.55257,0 +1.2262,0.89599,5.7568,-0.11596,0 +-0.048008,-0.56078,7.7215,0.453,0 +0.5706,-0.024841,1.2421,-0.56208,0 +4.3634,0.46351,1.4281,2.0202,0 +3.482,-4.1634,3.5008,-0.078462,0 +0.51947,-3.2633,3.0895,-0.98492,0 +2.3164,-2.628,3.1529,-0.08622,0 +-1.8348,11.0334,3.1863,-4.8888,0 +1.3754,8.8793,-1.9136,-0.53751,0 +-0.16682,5.8974,0.49839,-0.70044,0 +0.29961,7.1328,-0.31475,-1.1828,0 +0.25035,9.3262,-3.6873,-6.2543,0 +2.4673,1.3926,1.7125,0.41421,0 +0.77805,6.6424,-1.1425,-1.0573,0 +3.4465,2.9508,1.0271,0.5461,0 +2.2429,-4.1427,5.2333,-0.40173,0 +3.7321,-3.884,3.3577,-0.0060486,0 +4.3365,-3.584,3.6884,0.74912,0 +-2.0759,10.8223,2.6439,-4.837,0 +4.0715,7.6398,-2.0824,-1.1698,0 +0.76163,5.8209,1.1959,-0.64613,0 +-0.53966,7.3273,0.46583,-1.4543,0 +2.6213,5.7919,0.065686,-1.5759,0 +3.0242,-3.3378,2.5865,-0.54785,0 +5.8519,5.3905,-2.4037,-0.061652,0 +0.5706,-0.0248,1.2421,-0.5621,0 +3.9771,11.1513,-3.9272,-4.3444,0 +1.5478,9.1814,-1.6326,-1.7375,0 +0.74054,0.36625,2.1992,0.48403,0 +0.49571,10.2243,-1.097,-4.0159,0 +1.645,7.8612,-0.87598,-3.5569,0 +3.6077,6.8576,-1.1622,0.28231,0 +3.2403,-3.7082,5.2804,0.41291,0 +3.9166,10.2491,-4.0926,-4.4659,0 +3.9262,6.0299,-2.0156,-0.065531,0 +5.591,10.4643,-4.3839,-4.3379,0 +3.7522,-3.6978,3.9943,1.3051,0 +1.3114,4.5462,2.2935,0.22541,0 +3.7022,6.9942,-1.8511,-0.12889,0 +4.364,-3.1039,2.3757,0.78532,0 +3.5829,1.4423,1.0219,1.4008,0 +4.65,-4.8297,3.4553,-0.25174,0 +5.1731,3.9606,-1.983,0.40774,0 +3.2692,3.4184,0.20706,-0.066824,0 +2.4012,1.6223,3.0312,0.71679,0 +1.7257,-4.4697,8.2219,-1.8073,0 +4.7965,6.9859,-1.9967,-0.35001,0 +4.0962,10.1891,-3.9323,-4.1827,0 +2.5559,3.3605,2.0321,0.26809,0 +3.4916,8.5709,-3.0326,-0.59182,0 +0.5195,-3.2633,3.0895,-0.9849,0 +2.9856,7.2673,-0.409,-2.2431,0 +4.0932,5.4132,-1.8219,0.23576,0 +1.7748,-0.76978,5.5854,1.3039,0 +5.2012,0.32694,0.17965,1.1797,0 +-0.45062,-1.3678,7.0858,-0.40303,0 +4.8451,8.1116,-2.9512,-1.4724,0 +0.74841,7.2756,1.1504,-0.5388,0 +5.1213,8.5565,-3.3917,-1.5474,0 +3.6181,-3.7454,2.8273,-0.71208,0 +0.040498,8.5234,1.4461,-3.9306,0 +-2.6479,10.1374,-1.331,-5.4707,0 +0.37984,0.70975,0.75716,-0.44441,0 +-0.95923,0.091039,6.2204,-1.4828,0 +2.8672,10.0008,-3.2049,-3.1095,0 +1.0182,9.109,-0.62064,-1.7129,0 +-2.7143,11.4535,2.1092,-3.9629,0 +3.8244,-3.1081,2.4537,0.52024,0 +2.7961,2.121,1.8385,0.38317,0 +3.5358,6.7086,-0.81857,0.47886,0 +-0.7056,8.7241,2.2215,-4.5965,0 +4.1542,7.2756,-2.4766,-1.2099,0 +0.92703,9.4318,-0.66263,-1.6728,0 +1.8216,-6.4748,8.0514,-0.41855,0 +-2.4473,12.6247,0.73573,-7.6612,0 +3.5862,-3.0957,2.8093,0.24481,0 +0.66191,9.6594,-0.28819,-1.6638,0 +4.7926,1.7071,-0.051701,1.4926,0 +4.9852,8.3516,-2.5425,-1.2823,0 +0.75736,3.0294,2.9164,-0.068117,0 +4.6499,7.6336,-1.9427,-0.37458,0 +-0.023579,7.1742,0.78457,-0.75734,0 +0.85574,0.0082678,6.6042,-0.53104,0 +0.88298,0.66009,6.0096,-0.43277,0 +4.0422,-4.391,4.7466,1.137,0 +2.2546,8.0992,-0.24877,-3.2698,0 +0.38478,6.5989,-0.3336,-0.56466,0 +3.1541,-5.1711,6.5991,0.57455,0 +2.3969,0.23589,4.8477,1.437,0 +4.7114,2.0755,-0.2702,1.2379,0 +4.0127,10.1477,-3.9366,-4.0728,0 +2.6606,3.1681,1.9619,0.18662,0 +3.931,1.8541,-0.023425,1.2314,0 +0.01727,8.693,1.3989,-3.9668,0 +3.2414,0.40971,1.4015,1.1952,0 +2.2504,3.5757,0.35273,0.2836,0 +-1.3971,3.3191,-1.3927,-1.9948,1 +0.39012,-0.14279,-0.031994,0.35084,1 +-1.6677,-7.1535,7.8929,0.96765,1 +-3.8483,-12.8047,15.6824,-1.281,1 +-3.5681,-8.213,10.083,0.96765,1 +-2.2804,-0.30626,1.3347,1.3763,1 +-1.7582,2.7397,-2.5323,-2.234,1 +-0.89409,3.1991,-1.8219,-2.9452,1 +0.3434,0.12415,-0.28733,0.14654,1 +-0.9854,-6.661,5.8245,0.5461,1 +-2.4115,-9.1359,9.3444,-0.65259,1 +-1.5252,-6.2534,5.3524,0.59912,1 +-0.61442,-0.091058,-0.31818,0.50214,1 +-0.36506,2.8928,-3.6461,-3.0603,1 +-5.9034,6.5679,0.67661,-6.6797,1 +-1.8215,2.7521,-0.72261,-2.353,1 +-0.77461,-1.8768,2.4023,1.1319,1 +-1.8187,-9.0366,9.0162,-0.12243,1 +-3.5801,-12.9309,13.1779,-2.5677,1 +-1.8219,-6.8824,5.4681,0.057313,1 +-0.3481,-0.38696,-0.47841,0.62627,1 +0.47368,3.3605,-4.5064,-4.0431,1 +-3.4083,4.8587,-0.76888,-4.8668,1 +-1.6662,-0.30005,1.4238,0.024986,1 +-2.0962,-7.1059,6.6188,-0.33708,1 +-2.6685,-10.4519,9.1139,-1.7323,1 +-0.47465,-4.3496,1.9901,0.7517,1 +1.0552,1.1857,-2.6411,0.11033,1 +1.1644,3.8095,-4.9408,-4.0909,1 +-4.4779,7.3708,-0.31218,-6.7754,1 +-2.7338,0.45523,2.4391,0.21766,1 +-2.286,-5.4484,5.8039,0.88231,1 +-1.6244,-6.3444,4.6575,0.16981,1 +0.50813,0.47799,-1.9804,0.57714,1 +1.6408,4.2503,-4.9023,-2.6621,1 +0.81583,4.84,-5.2613,-6.0823,1 +-5.4901,9.1048,-0.38758,-5.9763,1 +-3.2238,2.7935,0.32274,-0.86078,1 +-2.0631,-1.5147,1.219,0.44524,1 +-0.91318,-2.0113,-0.19565,0.066365,1 +0.6005,1.9327,-3.2888,-0.32415,1 +0.91315,3.3377,-4.0557,-1.6741,1 +-0.28015,3.0729,-3.3857,-2.9155,1 +-3.6085,3.3253,-0.51954,-3.5737,1 +-6.2003,8.6806,0.0091344,-3.703,1 +-4.2932,3.3419,0.77258,-0.99785,1 +-3.0265,-0.062088,0.68604,-0.055186,1 +-1.7015,-0.010356,-0.99337,-0.53104,1 +-0.64326,2.4748,-2.9452,-1.0276,1 +-0.86339,1.9348,-2.3729,-1.0897,1 +-2.0659,1.0512,-0.46298,-1.0974,1 +-2.1333,1.5685,-0.084261,-1.7453,1 +-1.2568,-1.4733,2.8718,0.44653,1 +-3.1128,-6.841,10.7402,-1.0172,1 +-4.8554,-5.9037,10.9818,-0.82199,1 +-2.588,3.8654,-0.3336,-1.2797,1 +0.24394,1.4733,-1.4192,-0.58535,1 +-1.5322,-5.0966,6.6779,0.17498,1 +-4.0025,-13.4979,17.6772,-3.3202,1 +-4.0173,-8.3123,12.4547,-1.4375,1 +-3.0731,-0.53181,2.3877,0.77627,1 +-1.979,3.2301,-1.3575,-2.5819,1 +-0.4294,-0.14693,0.044265,-0.15605,1 +-2.234,-7.0314,7.4936,0.61334,1 +-4.211,-12.4736,14.9704,-1.3884,1 +-3.8073,-8.0971,10.1772,0.65084,1 +-2.5912,-0.10554,1.2798,1.0414,1 +-2.2482,3.0915,-2.3969,-2.6711,1 +-1.4427,3.2922,-1.9702,-3.4392,1 +-0.39416,-0.020702,-0.066267,-0.44699,1 +-1.522,-6.6383,5.7491,-0.10691,1 +-2.8267,-9.0407,9.0694,-0.98233,1 +-1.7263,-6.0237,5.2419,0.29524,1 +-0.94255,0.039307,-0.24192,0.31593,1 +-0.89569,3.0025,-3.6067,-3.4457,1 +-6.2815,6.6651,0.52581,-7.0107,1 +-2.3211,3.166,-1.0002,-2.7151,1 +-1.3414,-2.0776,2.8093,0.60688,1 +-2.258,-9.3263,9.3727,-0.85949,1 +-3.8858,-12.8461,12.7957,-3.1353,1 +-1.8969,-6.7893,5.2761,-0.32544,1 +-0.52645,-0.24832,-0.45613,0.41938,1 +0.0096613,3.5612,-4.407,-4.4103,1 +-3.8826,4.898,-0.92311,-5.0801,1 +-2.1405,-0.16762,1.321,-0.20906,1 +-2.4824,-7.3046,6.839,-0.59053,1 +-2.9098,-10.0712,8.4156,-1.9948,1 +-0.60975,-4.002,1.8471,0.6017,1 +0.83625,1.1071,-2.4706,-0.062945,1 +0.60731,3.9544,-4.772,-4.4853,1 +-4.8861,7.0542,-0.17252,-6.959,1 +-3.1366,0.42212,2.6225,-0.064238,1 +-2.5754,-5.6574,6.103,0.65214,1 +-1.8782,-6.5865,4.8486,-0.021566,1 +0.24261,0.57318,-1.9402,0.44007,1 +1.296,4.2855,-4.8457,-2.9013,1 +0.25943,5.0097,-5.0394,-6.3862,1 +-5.873,9.1752,-0.27448,-6.0422,1 +-3.4605,2.6901,0.16165,-1.0224,1 +-2.3797,-1.4402,1.1273,0.16076,1 +-1.2424,-1.7175,-0.52553,-0.21036,1 +0.20216,1.9182,-3.2828,-0.61768,1 +0.59823,3.5012,-3.9795,-1.7841,1 +-0.77995,3.2322,-3.282,-3.1004,1 +-4.1409,3.4619,-0.47841,-3.8879,1 +-6.5084,8.7696,0.23191,-3.937,1 +-4.4996,3.4288,0.56265,-1.1672,1 +-3.3125,0.10139,0.55323,-0.2957,1 +-1.9423,0.3766,-1.2898,-0.82458,1 +-0.75793,2.5349,-3.0464,-1.2629,1 +-0.95403,1.9824,-2.3163,-1.1957,1 +-2.2173,1.4671,-0.72689,-1.1724,1 +-2.799,1.9679,-0.42357,-2.1125,1 +-1.8629,-0.84841,2.5377,0.097399,1 +-3.5916,-6.2285,10.2389,-1.1543,1 +-5.1216,-5.3118,10.3846,-1.0612,1 +-3.2854,4.0372,-0.45356,-1.8228,1 +-0.56877,1.4174,-1.4252,-1.1246,1 +-2.3518,-4.8359,6.6479,-0.060358,1 +-4.4861,-13.2889,17.3087,-3.2194,1 +-4.3876,-7.7267,11.9655,-1.4543,1 +-3.3604,-0.32696,2.1324,0.6017,1 +-1.0112,2.9984,-1.1664,-1.6185,1 +0.030219,-1.0512,1.4024,0.77369,1 +-1.6514,-8.4985,9.1122,1.2379,1 +-3.2692,-12.7406,15.5573,-0.14182,1 +-2.5701,-6.8452,8.9999,2.1353,1 +-1.3066,0.25244,0.7623,1.7758,1 +-1.6637,3.2881,-2.2701,-2.2224,1 +-0.55008,2.8659,-1.6488,-2.4319,1 +0.21431,-0.69529,0.87711,0.29653,1 +-0.77288,-7.4473,6.492,0.36119,1 +-1.8391,-9.0883,9.2416,-0.10432,1 +-0.63298,-5.1277,4.5624,1.4797,1 +0.0040545,0.62905,-0.64121,0.75817,1 +-0.28696,3.1784,-3.5767,-3.1896,1 +-5.2406,6.6258,-0.19908,-6.8607,1 +-1.4446,2.1438,-0.47241,-1.6677,1 +-0.65767,-2.8018,3.7115,0.99739,1 +-1.5449,-10.1498,9.6152,-1.2332,1 +-2.8957,-12.0205,11.9149,-2.7552,1 +-0.81479,-5.7381,4.3919,0.3211,1 +0.50225,0.65388,-1.1793,0.39998,1 +0.74521,3.6357,-4.4044,-4.1414,1 +-2.9146,4.0537,-0.45699,-4.0327,1 +-1.3907,-1.3781,2.3055,-0.021566,1 +-1.786,-8.1157,7.0858,-1.2112,1 +-1.7322,-9.2828,7.719,-1.7168,1 +0.55298,-3.4619,1.7048,1.1008,1 +2.031,1.852,-3.0121,0.003003,1 +1.2279,4.0309,-4.6435,-3.9125,1 +-4.2249,6.2699,0.15822,-5.5457,1 +-2.5346,-0.77392,3.3602,0.00171,1 +-1.749,-6.332,6.0987,0.14266,1 +-0.539,-5.167,3.4399,0.052141,1 +1.5631,0.89599,-1.9702,0.65472,1 +2.3917,4.5565,-4.9888,-2.8987,1 +0.89512,4.7738,-4.8431,-5.5909,1 +-5.4808,8.1819,0.27818,-5.0323,1 +-2.8833,1.7713,0.68946,-0.4638,1 +-1.4174,-2.2535,1.518,0.61981,1 +0.4283,-0.94981,-1.0731,0.3211,1 +1.5904,2.2121,-3.1183,-0.11725,1 +1.7425,3.6833,-4.0129,-1.7207,1 +-0.23356,3.2405,-3.0669,-2.7784,1 +-3.6227,3.9958,-0.35845,-3.9047,1 +-6.1536,7.9295,0.61663,-3.2646,1 +-3.9172,2.6652,0.78886,-0.7819,1 +-2.2214,-0.23798,0.56008,0.05602,1 +-0.49241,0.89392,-1.6283,-0.56854,1 +0.26517,2.4066,-2.8416,-0.59958,1 +-0.10234,1.8189,-2.2169,-0.56725,1 +-1.6176,1.0926,-0.35502,-0.59958,1 +-1.8448,1.254,0.27218,-1.0728,1 +-1.2786,-2.4087,4.5735,0.47627,1 +-2.902,-7.6563,11.8318,-0.84268,1 +-4.3773,-5.5167,10.939,-0.4082,1 +-2.0529,3.8385,-0.79544,-1.2138,1 +0.18868,0.70148,-0.51182,0.0055892,1 +-1.7279,-6.841,8.9494,0.68058,1 +-3.3793,-13.7731,17.9274,-2.0323,1 +-3.1273,-7.1121,11.3897,-0.083634,1 +-2.121,-0.05588,1.949,1.353,1 +-1.7697,3.4329,-1.2144,-2.3789,1 +-0.0012852,0.13863,-0.19651,0.0081754,1 +-1.682,-6.8121,7.1398,1.3323,1 +-3.4917,-12.1736,14.3689,-0.61639,1 +-3.1158,-8.6289,10.4403,0.97153,1 +-2.0891,-0.48422,1.704,1.7435,1 +-1.6936,2.7852,-2.1835,-1.9276,1 +-1.2846,3.2715,-1.7671,-3.2608,1 +-0.092194,0.39315,-0.32846,-0.13794,1 +-1.0292,-6.3879,5.5255,0.79955,1 +-2.2083,-9.1069,8.9991,-0.28406,1 +-1.0744,-6.3113,5.355,0.80472,1 +-0.51003,-0.23591,0.020273,0.76334,1 +-0.36372,3.0439,-3.4816,-2.7836,1 +-6.3979,6.4479,1.0836,-6.6176,1 +-2.2501,3.3129,-0.88369,-2.8974,1 +-1.1859,-1.2519,2.2635,0.77239,1 +-1.8076,-8.8131,8.7086,-0.21682,1 +-3.3863,-12.9889,13.0545,-2.7202,1 +-1.4106,-7.108,5.6454,0.31335,1 +-0.21394,-0.68287,0.096532,1.1965,1 +0.48797,3.5674,-4.3882,-3.8116,1 +-3.8167,5.1401,-0.65063,-5.4306,1 +-1.9555,0.20692,1.2473,-0.3707,1 +-2.1786,-6.4479,6.0344,-0.20777,1 +-2.3299,-9.9532,8.4756,-1.8733,1 +0.0031201,-4.0061,1.7956,0.91722,1 +1.3518,1.0595,-2.3437,0.39998,1 +1.2309,3.8923,-4.8277,-4.0069,1 +-5.0301,7.5032,-0.13396,-7.5034,1 +-3.0799,0.60836,2.7039,-0.23751,1 +-2.2987,-5.227,5.63,0.91722,1 +-1.239,-6.541,4.8151,-0.033204,1 +0.75896,0.29176,-1.6506,0.83834,1 +1.6799,4.2068,-4.5398,-2.3931,1 +0.63655,5.2022,-5.2159,-6.1211,1 +-6.0598,9.2952,-0.43642,-6.3694,1 +-3.518,2.8763,0.1548,-1.2086,1 +-2.0336,-1.4092,1.1582,0.36507,1 +-0.69745,-1.7672,-0.34474,-0.12372,1 +0.75108,1.9161,-3.1098,-0.20518,1 +0.84546,3.4826,-3.6307,-1.3961,1 +-0.55648,3.2136,-3.3085,-2.7965,1 +-3.6817,3.2239,-0.69347,-3.4004,1 +-6.7526,8.8172,-0.061983,-3.725,1 +-4.577,3.4515,0.66719,-0.94742,1 +-2.9883,0.31245,0.45041,0.068951,1 +-1.4781,0.14277,-1.1622,-0.48579,1 +-0.46651,2.3383,-2.9812,-1.0431,1 +-0.8734,1.6533,-2.1964,-0.78061,1 +-2.1234,1.1815,-0.55552,-0.81165,1 +-2.3142,2.0838,-0.46813,-1.6767,1 +-1.4233,-0.98912,2.3586,0.39481,1 +-3.0866,-6.6362,10.5405,-0.89182,1 +-4.7331,-6.1789,11.388,-1.0741,1 +-2.8829,3.8964,-0.1888,-1.1672,1 +-0.036127,1.525,-1.4089,-0.76121,1 +-1.7104,-4.778,6.2109,0.3974,1 +-3.8203,-13.0551,16.9583,-2.3052,1 +-3.7181,-8.5089,12.363,-0.95518,1 +-2.899,-0.60424,2.6045,1.3776,1 +-0.98193,2.7956,-1.2341,-1.5668,1 +-0.17296,-1.1816,1.3818,0.7336,1 +-1.9409,-8.6848,9.155,0.94049,1 +-3.5713,-12.4922,14.8881,-0.47027,1 +-2.9915,-6.6258,8.6521,1.8198,1 +-1.8483,0.31038,0.77344,1.4189,1 +-2.2677,3.2964,-2.2563,-2.4642,1 +-0.50816,2.868,-1.8108,-2.2612,1 +0.14329,-1.0885,1.0039,0.48791,1 +-0.90784,-7.9026,6.7807,0.34179,1 +-2.0042,-9.3676,9.3333,-0.10303,1 +-0.93587,-5.1008,4.5367,1.3866,1 +-0.40804,0.54214,-0.52725,0.6586,1 +-0.8172,3.3812,-3.6684,-3.456,1 +-4.8392,6.6755,-0.24278,-6.5775,1 +-1.2792,2.1376,-0.47584,-1.3974,1 +-0.66008,-3.226,3.8058,1.1836,1 +-1.7713,-10.7665,10.2184,-1.0043,1 +-3.0061,-12.2377,11.9552,-2.1603,1 +-1.1022,-5.8395,4.5641,0.68705,1 +0.11806,0.39108,-0.98223,0.42843,1 +0.11686,3.735,-4.4379,-4.3741,1 +-2.7264,3.9213,-0.49212,-3.6371,1 +-1.2369,-1.6906,2.518,0.51636,1 +-1.8439,-8.6475,7.6796,-0.66682,1 +-1.8554,-9.6035,7.7764,-0.97716,1 +0.16358,-3.3584,1.3749,1.3569,1 +1.5077,1.9596,-3.0584,-0.12243,1 +0.67886,4.1199,-4.569,-4.1414,1 +-3.9934,5.8333,0.54723,-4.9379,1 +-2.3898,-0.78427,3.0141,0.76205,1 +-1.7976,-6.7686,6.6753,0.89912,1 +-0.70867,-5.5602,4.0483,0.903,1 +1.0194,1.1029,-2.3,0.59395,1 +1.7875,4.78,-5.1362,-3.2362,1 +0.27331,4.8773,-4.9194,-5.8198,1 +-5.1661,8.0433,0.044265,-4.4983,1 +-2.7028,1.6327,0.83598,-0.091393,1 +-1.4904,-2.2183,1.6054,0.89394,1 +-0.014902,-1.0243,-0.94024,0.64955,1 +0.88992,2.2638,-3.1046,-0.11855,1 +1.0637,3.6957,-4.1594,-1.9379,1 +-0.8471,3.1329,-3.0112,-2.9388,1 +-3.9594,4.0289,-0.35845,-3.8957,1 +-5.8818,7.6584,0.5558,-2.9155,1 +-3.7747,2.5162,0.83341,-0.30993,1 +-2.4198,-0.24418,0.70146,0.41809,1 +-0.83535,0.80494,-1.6411,-0.19225,1 +-0.30432,2.6528,-2.7756,-0.65647,1 +-0.60254,1.7237,-2.1501,-0.77027,1 +-2.1059,1.1815,-0.53324,-0.82716,1 +-2.0441,1.2271,0.18564,-1.091,1 +-1.5621,-2.2121,4.2591,0.27972,1 +-3.2305,-7.2135,11.6433,-0.94613,1 +-4.8426,-4.9932,10.4052,-0.53104,1 +-2.3147,3.6668,-0.6969,-1.2474,1 +-0.11716,0.60422,-0.38587,-0.059065,1 +-2.0066,-6.719,9.0162,0.099985,1 +-3.6961,-13.6779,17.5795,-2.6181,1 +-3.6012,-6.5389,10.5234,-0.48967,1 +-2.6286,0.18002,1.7956,0.97282,1 +-0.82601,2.9611,-1.2864,-1.4647,1 +0.31803,-0.99326,1.0947,0.88619,1 +-1.4454,-8.4385,8.8483,0.96894,1 +-3.1423,-13.0365,15.6773,-0.66165,1 +-2.5373,-6.959,8.8054,1.5289,1 +-1.366,0.18416,0.90539,1.5806,1 +-1.7064,3.3088,-2.2829,-2.1978,1 +-0.41965,2.9094,-1.7859,-2.2069,1 +0.37637,-0.82358,0.78543,0.74524,1 +-0.55355,-7.9233,6.7156,0.74394,1 +-1.6001,-9.5828,9.4044,0.081882,1 +-0.37013,-5.554,4.7749,1.547,1 +0.12126,0.22347,-0.47327,0.97024,1 +-0.27068,3.2674,-3.5562,-3.0888,1 +-5.119,6.6486,-0.049987,-6.5206,1 +-1.3946,2.3134,-0.44499,-1.4905,1 +-0.69879,-3.3771,4.1211,1.5043,1 +-1.48,-10.5244,9.9176,-0.5026,1 +-2.6649,-12.813,12.6689,-1.9082,1 +-0.62684,-6.301,4.7843,1.106,1 +0.518,0.25865,-0.84085,0.96118,1 +0.64376,3.764,-4.4738,-4.0483,1 +-2.9821,4.1986,-0.5898,-3.9642,1 +-1.4628,-1.5706,2.4357,0.49826,1 +-1.7101,-8.7903,7.9735,-0.45475,1 +-1.5572,-9.8808,8.1088,-1.0806,1 +0.74428,-3.7723,1.6131,1.5754,1 +2.0177,1.7982,-2.9581,0.2099,1 +1.164,3.913,-4.5544,-3.8672,1 +-4.3667,6.0692,0.57208,-5.4668,1 +-2.5919,-1.0553,3.8949,0.77757,1 +-1.8046,-6.8141,6.7019,1.1681,1 +-0.71868,-5.7154,3.8298,1.0233,1 +1.4378,0.66837,-2.0267,1.0271,1 +2.1943,4.5503,-4.976,-2.7254,1 +0.7376,4.8525,-4.7986,-5.6659,1 +-5.637,8.1261,0.13081,-5.0142,1 +-3.0193,1.7775,0.73745,-0.45346,1 +-1.6706,-2.09,1.584,0.71162,1 +-0.1269,-1.1505,-0.95138,0.57843,1 +1.2198,2.0982,-3.1954,0.12843,1 +1.4501,3.6067,-4.0557,-1.5966,1 +-0.40857,3.0977,-2.9607,-2.6892,1 +-3.8952,3.8157,-0.31304,-3.8194,1 +-6.3679,8.0102,0.4247,-3.2207,1 +-4.1429,2.7749,0.68261,-0.71984,1 +-2.6864,-0.097265,0.61663,0.061192,1 +-1.0555,0.79459,-1.6968,-0.46768,1 +-0.29858,2.4769,-2.9512,-0.66165,1 +-0.49948,1.7734,-2.2469,-0.68104,1 +-1.9881,0.99945,-0.28562,-0.70044,1 +-1.9389,1.5706,0.045979,-1.122,1 +-1.4375,-1.8624,4.026,0.55127,1 +-3.1875,-7.5756,11.8678,-0.57889,1 +-4.6765,-5.6636,10.969,-0.33449,1 +-2.0285,3.8468,-0.63435,-1.175,1 +0.26637,0.73252,-0.67891,0.03533,1 +-1.7589,-6.4624,8.4773,0.31981,1 +-3.5985,-13.6593,17.6052,-2.4927,1 +-3.3582,-7.2404,11.4419,-0.57113,1 +-2.3629,-0.10554,1.9336,1.1358,1 +-2.1802,3.3791,-1.2256,-2.6621,1 +-0.40951,-0.15521,0.060545,-0.088807,1 +-2.2918,-7.257,7.9597,0.9211,1 +-4.0214,-12.8006,15.6199,-0.95647,1 +-3.3884,-8.215,10.3315,0.98187,1 +-2.0046,-0.49457,1.333,1.6543,1 +-1.7063,2.7956,-2.378,-2.3491,1 +-1.6386,3.3584,-1.7302,-3.5646,1 +-0.41645,0.32487,-0.33617,-0.36036,1 +-1.5877,-6.6072,5.8022,0.31593,1 +-2.5961,-9.349,9.7942,-0.28018,1 +-1.5228,-6.4789,5.7568,0.87325,1 +-0.53072,-0.097265,-0.21793,1.0426,1 +-0.49081,2.8452,-3.6436,-3.1004,1 +-6.5773,6.8017,0.85483,-7.5344,1 +-2.4621,2.7645,-0.62578,-2.8573,1 +-1.3995,-1.9162,2.5154,0.59912,1 +-2.3221,-9.3304,9.233,-0.79871,1 +-3.73,-12.9723,12.9817,-2.684,1 +-1.6988,-7.1163,5.7902,0.16723,1 +-0.26654,-0.64562,-0.42014,0.89136,1 +0.33325,3.3108,-4.5081,-4.012,1 +-4.2091,4.7283,-0.49126,-5.2159,1 +-2.3142,-0.68494,1.9833,-0.44829,1 +-2.4835,-7.4494,6.8964,-0.64484,1 +-2.7611,-10.5099,9.0239,-1.9547,1 +-0.36025,-4.449,2.1067,0.94308,1 +1.0117,0.9022,-2.3506,0.42714,1 +0.96708,3.8426,-4.9314,-4.1323,1 +-5.2049,7.259,0.070827,-7.3004,1 +-3.3203,-0.02691,2.9618,-0.44958,1 +-2.565,-5.7899,6.0122,0.046968,1 +-1.5951,-6.572,4.7689,-0.94354,1 +0.7049,0.17174,-1.7859,0.36119,1 +1.7331,3.9544,-4.7412,-2.5017,1 +0.6818,4.8504,-5.2133,-6.1043,1 +-6.3364,9.2848,0.014275,-6.7844,1 +-3.8053,2.4273,0.6809,-1.0871,1 +-2.1979,-2.1252,1.7151,0.45171,1 +-0.87874,-2.2121,-0.051701,0.099985,1 +0.74067,1.7299,-3.1963,-0.1457,1 +0.98296,3.4226,-3.9692,-1.7116,1 +-0.3489,3.1929,-3.4054,-3.1832,1 +-3.8552,3.5219,-0.38415,-3.8608,1 +-6.9599,8.9931,0.2182,-4.572,1 +-4.7462,3.1205,1.075,-1.2966,1 +-3.2051,-0.14279,0.97565,0.045675,1 +-1.7549,-0.080711,-0.75774,-0.3707,1 +-0.59587,2.4811,-2.8673,-0.89828,1 +-0.89542,2.0279,-2.3652,-1.2746,1 +-2.0754,1.2767,-0.64206,-1.2642,1 +-3.2778,1.8023,0.1805,-2.3931,1 +-2.2183,-1.254,2.9986,0.36378,1 +-3.5895,-6.572,10.5251,-0.16381,1 +-5.0477,-5.8023,11.244,-0.3901,1 +-3.5741,3.944,-0.07912,-2.1203,1 +-0.7351,1.7361,-1.4938,-1.1582,1 +-2.2617,-4.7428,6.3489,0.11162,1 +-4.244,-13.0634,17.1116,-2.8017,1 +-4.0218,-8.304,12.555,-1.5099,1 +-3.0201,-0.67253,2.7056,0.85774,1 +-2.4941,3.5447,-1.3721,-2.8483,1 +-0.83121,0.039307,0.05369,-0.23105,1 +-2.5665,-6.8824,7.5416,0.70774,1 +-4.4018,-12.9371,15.6559,-1.6806,1 +-3.7573,-8.2916,10.3032,0.38059,1 +-2.4725,-0.40145,1.4855,1.1189,1 +-1.9725,2.8825,-2.3086,-2.3724,1 +-2.0149,3.6874,-1.9385,-3.8918,1 +-0.82053,0.65181,-0.48869,-0.52716,1 +-1.7886,-6.3486,5.6154,0.42584,1 +-2.9138,-9.4711,9.7668,-0.60216,1 +-1.8343,-6.5907,5.6429,0.54998,1 +-0.8734,-0.033118,-0.20165,0.55774,1 +-0.70346,2.957,-3.5947,-3.1457,1 +-6.7387,6.9879,0.67833,-7.5887,1 +-2.7723,3.2777,-0.9351,-3.1457,1 +-1.6641,-1.3678,1.997,0.52283,1 +-2.4349,-9.2497,8.9922,-0.50001,1 +-3.793,-12.7095,12.7957,-2.825,1 +-1.9551,-6.9756,5.5383,-0.12889,1 +-0.69078,-0.50077,-0.35417,0.47498,1 +0.025013,3.3998,-4.4327,-4.2655,1 +-4.3967,4.9601,-0.64892,-5.4719,1 +-2.456,-0.24418,1.4041,-0.45863,1 +-2.62,-6.8555,6.2169,-0.62285,1 +-2.9662,-10.3257,8.784,-2.1138,1 +-0.71494,-4.4448,2.2241,0.49826,1 +0.6005,0.99945,-2.2126,0.097399,1 +0.61652,3.8944,-4.7275,-4.3948,1 +-5.4414,7.2363,0.10938,-7.5642,1 +-3.5798,0.45937,2.3457,-0.45734,1 +-2.7769,-5.6967,5.9179,0.37671,1 +-1.8356,-6.7562,5.0585,-0.55044,1 +0.30081,0.17381,-1.7542,0.48921,1 +1.3403,4.1323,-4.7018,-2.5987,1 +0.26877,4.987,-5.1508,-6.3913,1 +-6.5235,9.6014,-0.25392,-6.9642,1 +-4.0679,2.4955,0.79571,-1.1039,1 +-2.564,-1.7051,1.5026,0.32757,1 +-1.3414,-1.9162,-0.15538,-0.11984,1 +0.23874,2.0879,-3.3522,-0.66553,1 +0.6212,3.6771,-4.0771,-2.0711,1 +-0.77848,3.4019,-3.4859,-3.5569,1 +-4.1244,3.7909,-0.6532,-4.1802,1 +-7.0421,9.2,0.25933,-4.6832,1 +-4.9462,3.5716,0.82742,-1.4957,1 +-3.5359,0.30417,0.6569,-0.2957,1 +-2.0662,0.16967,-1.0054,-0.82975,1 +-0.88728,2.808,-3.1432,-1.2035,1 +-1.0941,2.3072,-2.5237,-1.4453,1 +-2.4458,1.6285,-0.88541,-1.4802,1 +-3.551,1.8955,0.1865,-2.4409,1 +-2.2811,-0.85669,2.7185,0.044382,1 +-3.6053,-5.974,10.0916,-0.82846,1 +-5.0676,-5.1877,10.4266,-0.86725,1 +-3.9204,4.0723,-0.23678,-2.1151,1 +-1.1306,1.8458,-1.3575,-1.3806,1 +-2.4561,-4.5566,6.4534,-0.056479,1 +-4.4775,-13.0303,17.0834,-3.0345,1 +-4.1958,-8.1819,12.1291,-1.6017,1 +-3.38,-0.7077,2.5325,0.71808,1 +-2.4365,3.6026,-1.4166,-2.8948,1 +-0.77688,0.13036,-0.031137,-0.35389,1 +-2.7083,-6.8266,7.5339,0.59007,1 +-4.5531,-12.5854,15.4417,-1.4983,1 +-3.8894,-7.8322,9.8208,0.47498,1 +-2.5084,-0.22763,1.488,1.2069,1 +-2.1652,3.0211,-2.4132,-2.4241,1 +-1.8974,3.5074,-1.7842,-3.8491,1 +-0.62043,0.5587,-0.38587,-0.66423,1 +-1.8387,-6.301,5.6506,0.19567,1 +-3,-9.1566,9.5766,-0.73018,1 +-1.9116,-6.1603,5.606,0.48533,1 +-1.005,0.084831,-0.2462,0.45688,1 +-0.87834,3.257,-3.6778,-3.2944,1 +-6.651,6.7934,0.68604,-7.5887,1 +-2.5463,3.1101,-0.83228,-3.0358,1 +-1.4377,-1.432,2.1144,0.42067,1 +-2.4554,-9.0407,8.862,-0.86983,1 +-3.9411,-12.8792,13.0597,-3.3125,1 +-2.1241,-6.8969,5.5992,-0.47156,1 +-0.74324,-0.32902,-0.42785,0.23317,1 +-0.071503,3.7412,-4.5415,-4.2526,1 +-4.2333,4.9166,-0.49212,-5.3207,1 +-2.3675,-0.43663,1.692,-0.43018,1 +-2.5526,-7.3625,6.9255,-0.66811,1 +-3.0986,-10.4602,8.9717,-2.3427,1 +-0.89809,-4.4862,2.2009,0.50731,1 +0.56232,1.0015,-2.2726,-0.0060486,1 +0.53936,3.8944,-4.8166,-4.3418,1 +-5.3012,7.3915,0.029699,-7.3987,1 +-3.3553,0.35591,2.6473,-0.37846,1 +-2.7908,-5.7133,5.953,0.45946,1 +-1.9983,-6.6072,4.8254,-0.41984,1 +0.15423,0.11794,-1.6823,0.59524,1 +1.208,4.0744,-4.7635,-2.6129,1 +0.2952,4.8856,-5.149,-6.2323,1 +-6.4247,9.5311,0.022844,-6.8517,1 +-3.9933,2.6218,0.62863,-1.1595,1 +-2.659,-1.6058,1.3647,0.16464,1 +-1.4094,-2.1252,-0.10397,-0.19225,1 +0.11032,1.9741,-3.3668,-0.65259,1 +0.52374,3.644,-4.0746,-1.9909,1 +-0.76794,3.4598,-3.4405,-3.4276,1 +-3.9698,3.6812,-0.60008,-4.0133,1 +-7.0364,9.2931,0.16594,-4.5396,1 +-4.9447,3.3005,1.063,-1.444,1 +-3.5933,0.22968,0.7126,-0.3332,1 +-2.1674,0.12415,-1.0465,-0.86208,1 +-0.9607,2.6963,-3.1226,-1.3121,1 +-1.0802,2.1996,-2.5862,-1.2759,1 +-2.3277,1.4381,-0.82114,-1.2862,1 +-3.7244,1.9037,-0.035421,-2.5095,1 +-2.5724,-0.95602,2.7073,-0.16639,1 +-3.9297,-6.0816,10.0958,-1.0147,1 +-5.2943,-5.1463,10.3332,-1.1181,1 +-3.8953,4.0392,-0.3019,-2.1836,1 +-1.2244,1.7485,-1.4801,-1.4181,1 +-2.6406,-4.4159,5.983,-0.13924,1 +-4.6338,-12.7509,16.7166,-3.2168,1 +-4.2887,-7.8633,11.8387,-1.8978,1 +-3.3458,-0.50491,2.6328,0.53705,1 +-1.1188,3.3357,-1.3455,-1.9573,1 +0.55939,-0.3104,0.18307,0.44653,1 +-1.5078,-7.3191,7.8981,1.2289,1 +-3.506,-12.5667,15.1606,-0.75216,1 +-2.9498,-8.273,10.2646,1.1629,1 +-1.6029,-0.38903,1.62,1.9103,1 +-1.2667,2.8183,-2.426,-1.8862,1 +-0.49281,3.0605,-1.8356,-2.834,1 +0.66365,-0.045533,-0.18794,0.23447,1 +-0.72068,-6.7583,5.8408,0.62369,1 +-1.9966,-9.5001,9.682,-0.12889,1 +-0.97325,-6.4168,5.6026,1.0323,1 +-0.025314,-0.17383,-0.11339,1.2198,1 +0.062525,2.9301,-3.5467,-2.6737,1 +-5.525,6.3258,0.89768,-6.6241,1 +-1.2943,2.6735,-0.84085,-2.0323,1 +-0.24037,-1.7837,2.135,1.2418,1 +-1.3968,-9.6698,9.4652,-0.34872,1 +-2.9672,-13.2869,13.4727,-2.6271,1 +-1.1005,-7.2508,6.0139,0.36895,1 +0.22432,-0.52147,-0.40386,1.2017,1 +0.90407,3.3708,-4.4987,-3.6965,1 +-2.8619,4.5193,-0.58123,-4.2629,1 +-1.0833,-0.31247,1.2815,0.41291,1 +-1.5681,-7.2446,6.5537,-0.1276,1 +-2.0545,-10.8679,9.4926,-1.4116,1 +0.2346,-4.5152,2.1195,1.4448,1 +1.581,0.86909,-2.3138,0.82412,1 +1.5514,3.8013,-4.9143,-3.7483,1 +-4.1479,7.1225,-0.083404,-6.4172,1 +-2.2625,-0.099335,2.8127,0.48662,1 +-1.7479,-5.823,5.8699,1.212,1 +-0.95923,-6.7128,4.9857,0.32886,1 +1.3451,0.23589,-1.8785,1.3258,1 +2.2279,4.0951,-4.8037,-2.1112,1 +1.2572,4.8731,-5.2861,-5.8741,1 +-5.3857,9.1214,-0.41929,-5.9181,1 +-2.9786,2.3445,0.52667,-0.40173,1 +-1.5851,-2.1562,1.7082,0.9017,1 +-0.21888,-2.2038,-0.0954,0.56421,1 +1.3183,1.9017,-3.3111,0.065071,1 +1.4896,3.4288,-4.0309,-1.4259,1 +0.11592,3.2219,-3.4302,-2.8457,1 +-3.3924,3.3564,-0.72004,-3.5233,1 +-6.1632,8.7096,-0.21621,-3.6345,1 +-4.0786,2.9239,0.87026,-0.65389,1 +-2.5899,-0.3911,0.93452,0.42972,1 +-1.0116,-0.19038,-0.90597,0.003003,1 +0.066129,2.4914,-2.9401,-0.62156,1 +-0.24745,1.9368,-2.4697,-0.80518,1 +-1.5732,1.0636,-0.71232,-0.8388,1 +-2.1668,1.5933,0.045122,-1.678,1 +-1.1667,-1.4237,2.9241,0.66119,1 +-2.8391,-6.63,10.4849,-0.42113,1 +-4.5046,-5.8126,10.8867,-0.52846,1 +-2.41,3.7433,-0.40215,-1.2953,1 +0.40614,1.3492,-1.4501,-0.55949,1 +-1.3887,-4.8773,6.4774,0.34179,1 +-3.7503,-13.4586,17.5932,-2.7771,1 +-3.5637,-8.3827,12.393,-1.2823,1 +-2.5419,-0.65804,2.6842,1.1952,1 \ No newline at end of file diff --git a/doc/src/week47/DataFiles/cancer.dot b/doc/src/week47/DataFiles/cancer.dot new file mode 100644 index 000000000..5b4b48a9b --- /dev/null +++ b/doc/src/week47/DataFiles/cancer.dot @@ -0,0 +1,57 @@ +digraph Tree { +node [shape=box, style="filled, rounded", color="black", fontname=helvetica] ; +edge [fontname=helvetica] ; +0 [label="worst perimeter <= 106.05\ngini = 0.465\nsamples = 426\nvalue = [[269, 157]\n[157, 269]]", fillcolor="#e5813908"] ; +1 [label="worst concave points <= 0.159\ngini = 0.067\nsamples = 259\nvalue = [[250, 9]\n[9, 250]]", fillcolor="#e58139db"] ; +0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ; +2 [label="worst concave points <= 0.135\ngini = 0.031\nsamples = 253\nvalue = [[249, 4]\n[4, 249]]", fillcolor="#e58139ee"] ; +1 -> 2 ; +3 [label="radius error <= 0.643\ngini = 0.008\nsamples = 242\nvalue = [[241, 1]\n[1, 241]]", fillcolor="#e58139fb"] ; +2 -> 3 ; +4 [label="gini = 0.0\nsamples = 239\nvalue = [[239, 0]\n[0, 239]]", fillcolor="#e58139ff"] ; +3 -> 4 ; +5 [label="worst symmetry <= 0.208\ngini = 0.444\nsamples = 3\nvalue = [[2, 1]\n[1, 2]]", fillcolor="#e5813913"] ; +3 -> 5 ; +6 [label="gini = 0.0\nsamples = 1\nvalue = [[0, 1]\n[1, 0]]", fillcolor="#e58139ff"] ; +5 -> 6 ; +7 [label="gini = 0.0\nsamples = 2\nvalue = [[2, 0]\n[0, 2]]", fillcolor="#e58139ff"] ; +5 -> 7 ; +8 [label="worst texture <= 29.455\ngini = 0.397\nsamples = 11\nvalue = [[8, 3]\n[3, 8]]", fillcolor="#e581392c"] ; +2 -> 8 ; +9 [label="gini = 0.0\nsamples = 8\nvalue = [[8, 0]\n[0, 8]]", fillcolor="#e58139ff"] ; +8 -> 9 ; +10 [label="gini = 0.0\nsamples = 3\nvalue = [[0, 3]\n[3, 0]]", fillcolor="#e58139ff"] ; +8 -> 10 ; +11 [label="mean texture <= 16.22\ngini = 0.278\nsamples = 6\nvalue = [[1, 5]\n[5, 1]]", fillcolor="#e581396b"] ; +1 -> 11 ; +12 [label="gini = 0.0\nsamples = 1\nvalue = [[1, 0]\n[0, 1]]", fillcolor="#e58139ff"] ; +11 -> 12 ; +13 [label="gini = 0.0\nsamples = 5\nvalue = [[0, 5]\n[5, 0]]", fillcolor="#e58139ff"] ; +11 -> 13 ; +14 [label="worst texture <= 20.645\ngini = 0.202\nsamples = 167\nvalue = [[19, 148]\n[148, 19]]", fillcolor="#e5813994"] ; +0 -> 14 [labeldistance=2.5, labelangle=-45, headlabel="False"] ; +15 [label="worst radius <= 17.74\ngini = 0.375\nsamples = 16\nvalue = [[12, 4]\n[4, 12]]", fillcolor="#e5813938"] ; +14 -> 15 ; +16 [label="gini = 0.0\nsamples = 11\nvalue = [[11, 0]\n[0, 11]]", fillcolor="#e58139ff"] ; +15 -> 16 ; +17 [label="mean texture <= 13.745\ngini = 0.32\nsamples = 5\nvalue = [[1, 4]\n[4, 1]]", fillcolor="#e5813955"] ; +15 -> 17 ; +18 [label="gini = 0.0\nsamples = 1\nvalue = [[1, 0]\n[0, 1]]", fillcolor="#e58139ff"] ; +17 -> 18 ; +19 [label="gini = 0.0\nsamples = 4\nvalue = [[0, 4]\n[4, 0]]", fillcolor="#e58139ff"] ; +17 -> 19 ; +20 [label="mean concave points <= 0.049\ngini = 0.088\nsamples = 151\nvalue = [[7, 144]\n[144, 7]]", fillcolor="#e58139d0"] ; +14 -> 20 ; +21 [label="concave points error <= 0.01\ngini = 0.48\nsamples = 15\nvalue = [[6, 9]\n[9, 6]]", fillcolor="#e5813900"] ; +20 -> 21 ; +22 [label="gini = 0.0\nsamples = 9\nvalue = [[0, 9]\n[9, 0]]", fillcolor="#e58139ff"] ; +21 -> 22 ; +23 [label="gini = 0.0\nsamples = 6\nvalue = [[6, 0]\n[0, 6]]", fillcolor="#e58139ff"] ; +21 -> 23 ; +24 [label="worst smoothness <= 0.096\ngini = 0.015\nsamples = 136\nvalue = [[1, 135]\n[135, 1]]", fillcolor="#e58139f7"] ; +20 -> 24 ; +25 [label="gini = 0.0\nsamples = 1\nvalue = [[1, 0]\n[0, 1]]", fillcolor="#e58139ff"] ; +24 -> 25 ; +26 [label="gini = 0.0\nsamples = 135\nvalue = [[0, 135]\n[135, 0]]", fillcolor="#e58139ff"] ; +24 -> 26 ; +} \ No newline at end of file diff --git a/doc/src/week47/DataFiles/cancer.png b/doc/src/week47/DataFiles/cancer.png new file mode 100644 index 000000000..2ceb5e1f8 Binary files /dev/null and b/doc/src/week47/DataFiles/cancer.png differ diff --git a/doc/src/week47/DataFiles/ensembleoverview.png b/doc/src/week47/DataFiles/ensembleoverview.png new file mode 100644 index 000000000..dce581ee6 Binary files /dev/null and b/doc/src/week47/DataFiles/ensembleoverview.png differ diff --git a/doc/src/week47/DataFiles/grades.csv b/doc/src/week47/DataFiles/grades.csv new file mode 100644 index 000000000..eda2ed442 --- /dev/null +++ b/doc/src/week47/DataFiles/grades.csv @@ -0,0 +1,11 @@ +Grade Trend,Hours slept,Hours Studied,Grade +1,0,1,1 +0,1,0,0 +1,0,1,1 +1,1,1,1 +0,0,1,0 +1,0,0,0 +0,1,1,0 +0,0,1,0 +1,0,0,0 +1,1,1,1 diff --git a/doc/src/week47/DataFiles/ride.csv b/doc/src/week47/DataFiles/ride.csv new file mode 100644 index 000000000..d03d4ca16 --- /dev/null +++ b/doc/src/week47/DataFiles/ride.csv @@ -0,0 +1,15 @@ +Outlook,Temperature,Humidity,Wind,Ride +0,0,0,0,0 +0,0,0,1,1 +1,0,0,0,1 +2,1,0,0,1 +2,2,1,0,1 +2,2,1,1,0 +1,2,1,1,1 +0,1,0,0,0 +0,2,1,0,1 +2,1,1,0,1 +0,1,1,1,1 +1,1,0,1,1 +1,0,1,0,1 +2,1,0,1,0 diff --git a/doc/src/week47/DataFiles/ride.dot b/doc/src/week47/DataFiles/ride.dot new file mode 100644 index 000000000..50aaa7638 --- /dev/null +++ b/doc/src/week47/DataFiles/ride.dot @@ -0,0 +1,13 @@ +digraph Tree { +node [shape=box, style="filled, rounded", color="black", fontname=helvetica] ; +edge [fontname=helvetica] ; +0 [label="X[7] <= 0.5\ngini = 0.48\nsamples = 15\nvalue = [4, 10, 1]", fillcolor="#39e5818b"] ; +1 [label="X[1] <= 0.5\ngini = 0.408\nsamples = 14\nvalue = [4, 10, 0]", fillcolor="#39e58199"] ; +0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ; +2 [label="gini = 0.48\nsamples = 10\nvalue = [4, 6, 0]", fillcolor="#39e58155"] ; +1 -> 2 ; +3 [label="gini = 0.0\nsamples = 4\nvalue = [0, 4, 0]", fillcolor="#39e581ff"] ; +1 -> 3 ; +4 [label="gini = 0.0\nsamples = 1\nvalue = [0, 0, 1]", fillcolor="#8139e5ff"] ; +0 -> 4 [labeldistance=2.5, labelangle=-45, headlabel="False"] ; +} \ No newline at end of file diff --git a/doc/src/week47/DataFiles/rideclass.csv b/doc/src/week47/DataFiles/rideclass.csv new file mode 100644 index 000000000..a0831ac7e --- /dev/null +++ b/doc/src/week47/DataFiles/rideclass.csv @@ -0,0 +1,15 @@ +Day,Outlook,Temperature,Humidity,Wind,Ride +1,Sunny,Hot,High,Weak,0 +2,Sunny,Hot,High,Strong,1 +3,Overcast,Hot,High,Weak,1 +4,Rain,Mild,High,Weak,1 +5,Rain,Cool,Normal,Weak,1 +6,Rain,Cool,Normal,Strong,0 +7,Overcast,Cool,Normal,Strong,1 +8,Sunny,Mild,High,Weak,0 +9,Sunny,Cool,Normal,Weak,1 +10,Rain,Mild,Normal,Weak,1 +11,Sunny,Mild,Normal,Strong,1 +12,Overcast,Mild,High,Strong,1 +13,Overcast,Hot,Normal,Weak,1 +14,Rain,Mild,High,Strong,0 diff --git a/doc/src/week47/DataFiles/zoo.csv b/doc/src/week47/DataFiles/zoo.csv new file mode 100644 index 000000000..ca71f7d21 --- /dev/null +++ b/doc/src/week47/DataFiles/zoo.csv @@ -0,0 +1,101 @@ +aardvark,1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,1 +antelope,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1 +bass,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,4 +bear,1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,1 +boar,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +buffalo,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1 +calf,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,1 +carp,0,0,1,0,0,1,0,1,1,0,0,1,0,1,1,0,4 +catfish,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,4 +cavy,1,0,0,1,0,0,0,1,1,1,0,0,4,0,1,0,1 +cheetah,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +chicken,0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,2 +chub,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,4 +clam,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,7 +crab,0,0,1,0,0,1,1,0,0,0,0,0,4,0,0,0,7 +crayfish,0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,7 +crow,0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,2 +deer,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1 +dogfish,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,4 +dolphin,0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1 +dove,0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,2 +duck,0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,0,2 +elephant,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1 +flamingo,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,1,2 +flea,0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,6 +frog,0,0,1,0,0,1,1,1,1,1,0,0,4,0,0,0,5 +frog,0,0,1,0,0,1,1,1,1,1,1,0,4,0,0,0,5 +fruitbat,1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,1 +giraffe,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1 +girl,1,0,0,1,0,0,1,1,1,1,0,0,2,0,1,1,1 +gnat,0,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,6 +goat,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,1 +gorilla,1,0,0,1,0,0,0,1,1,1,0,0,2,0,0,1,1 +gull,0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,2 +haddock,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,4 +hamster,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,0,1 +hare,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,1 +hawk,0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,2 +herring,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,4 +honeybee,1,0,1,0,1,0,0,0,0,1,1,0,6,0,1,0,6 +housefly,1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,6 +kiwi,0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,0,2 +ladybird,0,0,1,0,1,0,1,0,0,1,0,0,6,0,0,0,6 +lark,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,2 +leopard,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +lion,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +lobster,0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,7 +lynx,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +mink,1,0,0,1,0,1,1,1,1,1,0,0,4,1,0,1,1 +mole,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,1 +mongoose,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +moth,1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,6 +newt,0,0,1,0,0,1,1,1,1,1,0,0,4,1,0,0,5 +octopus,0,0,1,0,0,1,1,0,0,0,0,0,8,0,0,1,7 +opossum,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,1 +oryx,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1 +ostrich,0,1,1,0,0,0,0,0,1,1,0,0,2,1,0,1,2 +parakeet,0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,2 +penguin,0,1,1,0,0,1,1,0,1,1,0,0,2,1,0,1,2 +pheasant,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,2 +pike,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,4 +piranha,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,4 +pitviper,0,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,3 +platypus,1,0,1,1,0,1,1,0,1,1,0,0,4,1,0,1,1 +polecat,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +pony,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,1 +porpoise,0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1 +puma,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +pussycat,1,0,0,1,0,0,1,1,1,1,0,0,4,1,1,1,1 +raccoon,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +reindeer,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,1 +rhea,0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,1,2 +scorpion,0,0,0,0,0,0,1,0,0,1,1,0,8,1,0,0,7 +seahorse,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,4 +seal,1,0,0,1,0,1,1,1,1,1,0,1,0,0,0,1,1 +sealion,1,0,0,1,0,1,1,1,1,1,0,1,2,1,0,1,1 +seasnake,0,0,0,0,0,1,1,1,1,0,1,0,0,1,0,0,3 +seawasp,0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,7 +skimmer,0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,2 +skua,0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,2 +slowworm,0,0,1,0,0,0,1,1,1,1,0,0,0,1,0,0,3 +slug,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,7 +sole,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,4 +sparrow,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,2 +squirrel,1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,0,1 +starfish,0,0,1,0,0,1,1,0,0,0,0,0,5,0,0,0,7 +stingray,0,0,1,0,0,1,1,1,1,0,1,1,0,1,0,1,4 +swan,0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,1,2 +termite,0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,6 +toad,0,0,1,0,0,1,0,1,1,1,0,0,4,0,0,0,5 +tortoise,0,0,1,0,0,0,0,0,1,1,0,0,4,1,0,1,3 +tuatara,0,0,1,0,0,0,1,1,1,1,0,0,4,1,0,0,3 +tuna,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,4 +vampire,1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,1 +vole,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,1 +vulture,0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,1,2 +wallaby,1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,1,1 +wasp,1,0,1,0,1,0,0,0,0,1,1,0,6,0,0,0,6 +wolf,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1 +worm,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,7 +wren,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,2 diff --git a/doc/src/week47/week47.do.txt b/doc/src/week47/week47.do.txt index 218a20118..ac861482e 100644 --- a/doc/src/week47/week47.do.txt +++ b/doc/src/week47/week47.do.txt @@ -15,7 +15,7 @@ DATE: November 18-22, 2024 o Basics of decision trees, classification and regression algorithms and ensemble models o Readings and Videos: o These lecture notes at URL:"https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week47/ipynb/week47.ipynb" - o See also lecture notes from week 46 at URL:"https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb" + o See also lecture notes from week 46 at URL:"https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb". The lecture on Monday starts with a repetition on how to make a decision tree. # * "Video of Lecture":"https://youtu.be/SpWXsvn5I9E" # * "Whiteboard notes":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2023/NotesNov23.pdf" o Video on Decision trees URL:"https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn" @@ -468,29 +468,10 @@ In computations we will translate all classes into numbers. Being these binary classes, they can easily be split into ones and zeros. !bblock Gini index for Average trend -"See handwritten notes November 3":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2022/NotesNov32022.pdf" +See whiteboard notes from lecture November 11 at URL:"https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf" !eblock -!split -===== Computing the various Gini Indices, Hours slept ===== - - -!bblock Gini index for hour slept -"See handwritten notes November 3":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2022/NotesNov32022.pdf" -!eblock - - -!split -===== Computing the various Gini Indices, Hours studied ===== - - -!bblock Gini index for hour studied -"See handwritten notes November 3":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2022/NotesNov32022.pdf" -!eblock - -For final tree, see the above handwritten notes - !split ===== A possible code using Scikit-Learn ===== @@ -1714,7 +1695,90 @@ skplt.metrics.plot_cumulative_gain(y_test, y_probas) plt.show() !ec +!split +===== Making an ADAboost code yourself ===== +!bc pycod +import numpy as np + +class DecisionStump: + def fit(self, X, y, weights): + m, n = X.shape + self.alpha = 0 + self.threshold = None + self.polarity = 1 + + min_error = float('inf') + + for feature in range(n): + feature_values = np.unique(X[:, feature]) + + for threshold in feature_values: + for polarity in [1, -1]: + predictions = np.ones(m) + predictions[X[:, feature] < threshold] = -1 + predictions *= polarity + + error = sum(weights[predictions != y]) + + if error < min_error: + min_error = error + self.alpha = 0.5 * np.log((1 - error) / (error + 1e-10)) + self.threshold = threshold + self.feature_index = feature + self.polarity = polarity + + def predict(self, X): + m = X.shape[0] + predictions = np.ones(m) + if self.polarity == 1: + predictions[X[:, self.feature_index] < self.threshold] = -1 + else: + predictions[X[:, self.feature_index] >= self.threshold] = -1 + return predictions + +class AdaBoost: + def fit(self, X, y, n_estimators): + m = X.shape[0] + self.alphas = [] + self.models = [] + + weights = np.ones(m) / m + + for _ in range(n_estimators): + stump = DecisionStump() + stump.fit(X, y, weights) + predictions = stump.predict(X) + + error = sum(weights[predictions != y]) + if error == 0: + break + + self.models.append(stump) + self.alphas.append(stump.alpha) + + weights *= np.exp(-stump.alpha * y * predictions) + weights /= np.sum(weights) + + def predict(self, X): + final_predictions = np.zeros(X.shape[0]) + for alpha, model in zip(self.alphas, self.models): + final_predictions += alpha * model.predict(X) + return np.sign(final_predictions) + +# Example dataset (X, y) +X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]) +y = np.array([-1, -1, -1, -1, 1, 1, 1, 1, 1, 1]) # Labels must be -1 or 1 + +# Train AdaBoost +ada = AdaBoost() +ada.fit(X, y, n_estimators=10) + +# Predictions +predictions = ada.predict(X) +print("Predictions:", predictions) + +!ec !split ===== Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent ===== @@ -2024,4 +2088,85 @@ plt.show() +!split +===== Gradient boosting, making our own code for a regression case ===== +!bc pycod +import numpy as np +class DecisionTreeRegressor: + def __init__(self, max_depth=3): + self.max_depth = max_depth + self.tree = None + def fit(self, X, y): + self.tree = self._grow_tree(X, y) + def _grow_tree(self, X, y, depth=0): + n_samples, n_features = X.shape + if depth < self.max_depth: + best_feature, best_threshold = self._best_split(X, y) + if best_feature is not None: + left_indices = X[:, best_feature] < best_threshold + right_indices = X[:, best_feature] >= best_threshold + left_child = self._grow_tree(X[left_indices], y[left_indices], depth + 1) + right_child = self._grow_tree(X[right_indices], y[right_indices], depth + 1) + return (best_feature, best_threshold, left_child, right_child) + return np.mean(y) + def _best_split(self, X, y): + best_mse = float('inf') + best_feature, best_threshold = None, None + n_samples, n_features = X.shape + + for feature in range(n_features): + thresholds = np.unique(X[:, feature]) + for threshold in thresholds: + left_indices = X[:, feature] < threshold + right_indices = X[:, feature] >= threshold + if len(y[left_indices]) > 0 and len(y[right_indices]) > 0: + left_mse = np.mean((y[left_indices] - np.mean(y[left_indices])) ** 2) + right_mse = np.mean((y[right_indices] - np.mean(y[right_indices])) ** 2) + mse = (len(y[left_indices]) * left_mse + len(y[right_indices]) * right_mse) / n_samples + + if mse < best_mse: + best_mse = mse + best_feature = feature + best_threshold = threshold + return best_feature, best_threshold + def predict(self, X): + return np.array([self._predict_sample(sample, self.tree) for sample in X]) + def _predict_sample(self, sample, node): + if isinstance(node, tuple): + feature, threshold, left_child, right_child = node + if sample[feature] < threshold: + return self._predict_sample(sample, left_child) + else: + return self._predict_sample(sample, right_child) + return node +class GradientBoostingRegressor: + def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3): + self.n_estimators = n_estimators + self.learning_rate = learning_rate + self.max_depth = max_depth + self.models = [] + def fit(self, X, y): + y_pred = np.zeros(y.shape) + for _ in range(self.n_estimators): + residuals = y - y_pred + model = DecisionTreeRegressor(max_depth=self.max_depth) + model.fit(X, residuals) + y_pred += self.learning_rate * model.predict(X) + self.models.append(model) + def predict(self, X): + y_pred = np.zeros(X.shape[0]) + for model in self.models: + y_pred += self.learning_rate * model.predict(X) + return y_pred +# Example usage +if __name__ == "__main__": + # Sample data + X = np.array([[1], [2], [3], [4], [5]]) + y = np.array([1.5, 1.7, 3.5, 3.7, 5.0]) + model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=2) + model.fit(X, y) + predictions = model.predict(X) + print("Predictions:", predictions) + +!ec