diff --git a/doc/pub/DecisionTrees/html/DecisionTrees-bs.html b/doc/pub/DecisionTrees/html/DecisionTrees-bs.html index 37fe3d3ad..12855f998 100644 --- a/doc/pub/DecisionTrees/html/DecisionTrees-bs.html +++ b/doc/pub/DecisionTrees/html/DecisionTrees-bs.html @@ -56,11 +56,13 @@ Automatically generated HTML file from DocOnce source ('A schematic procedure', 2, None, '___sec10'), ('A classification tree', 2, None, '___sec11'), ('Growing a classification tree', 2, None, '___sec12'), - ('Pros and cons of trees, pros', 2, None, '___sec13'), - ('Disadvantages', 2, None, '___sec14'), - ('Bagging', 2, None, '___sec15'), - ('Random forests', 2, None, '___sec16'), - ('A simple scikit-learn example', 2, None, '___sec17')]} + ('The zoo data', 2, None, '___sec13'), + ('Pros and cons of trees, pros', 2, None, '___sec14'), + ('Disadvantages', 2, None, '___sec15'), + ('Bagging', 2, None, '___sec16'), + ('Random forests', 2, None, '___sec17'), + ('A simple scikit-learn example', 2, None, '___sec18'), + ('Boosting', 2, None, '___sec19')]} end of tocinfo -->
@@ -111,11 +113,13 @@ MathJax.Hub.Config({The tuning parameter \( \alpha \) controls a trade-off between the subtree’s @@ -583,14 +587,16 @@ subtree corresponding to \( \alpha \).
-
+ + +
import pandas as pd
+import numpy as np
+from pprint import pprint
+from sklearn.tree import DecisionTreeClassifier
+
+#Import the dataset
+dataset = pd.read_csv('data/zoo.csv')
+#We drop the animal names since this is not a good feature to split the data on
+#dataset=dataset.drop('animal_name',axis=1)
+#Split the data into a training and a testing set
+train_features = dataset.iloc[:80,:-1]
+test_features = dataset.iloc[80:,:-1]
+train_targets = dataset.iloc[:80,-1]
+test_targets = dataset.iloc[80:,-1]
+#Train the model
+tree = DecisionTreeClassifier(criterion = 'entropy').fit(train_features,train_targets)
+#Predict the classes of new, unseen data
+prediction = tree.predict(test_features)
+#Check the accuracy
+print("The prediction accuracy is: ",tree.score(test_features,test_targets)*100,"%")
++ + +
-
The plain decision trees suffer from high @@ -717,7 +751,7 @@ predictor, averaged over all \( B \) trees.
-
Random forests provide an improvement over bagged trees by way of a @@ -759,7 +793,7 @@ setting.
-
@@ -775,6 +809,10 @@ Random_Forest_model = RandomForestClassifier accuracy = cross_validate(Random_Forest_model,X,Y,cv=10)['test_score']
+ + +
The tuning parameter \( \alpha \) controls a trade-off between the subtree’s @@ -594,13 +594,18 @@ subtree corresponding to \( \alpha \).
+ + +
import pandas as pd
+import numpy as np
+from pprint import pprint
+from sklearn.tree import DecisionTreeClassifier
+
+#Import the dataset
+dataset = pd.read_csv('data/zoo.csv')
+#We drop the animal names since this is not a good feature to split the data on
+#dataset=dataset.drop('animal_name',axis=1)
+#Split the data into a training and a testing set
+train_features = dataset.iloc[:80,:-1]
+test_features = dataset.iloc[80:,:-1]
+train_targets = dataset.iloc[:80,-1]
+test_targets = dataset.iloc[80:,-1]
+#Train the model
+tree = DecisionTreeClassifier(criterion = 'entropy').fit(train_features,train_targets)
+#Predict the classes of new, unseen data
+prediction = tree.predict(test_features)
+#Check the accuracy
+print("The prediction accuracy is: ",tree.score(test_features,test_targets)*100,"%")
+
The plain decision trees suffer from high
@@ -727,7 +761,7 @@ predictor, averaged over all \( B \) trees.
Random forests provide an improvement over bagged trees by way of a
@@ -771,7 +805,7 @@ setting.
@@ -789,6 +823,12 @@ accuracy = cross_validate(Random_Forest_model,X,Y,cv=Boosting
+More material to come here.
+
The tuning parameter \( \alpha \) controls a trade-off between the subtree’s
@@ -552,14 +554,16 @@ subtree corresponding to \( \alpha \).
+
+
+
+
The plain decision trees suffer from high
@@ -685,7 +717,7 @@ predictor, averaged over all \( B \) trees.
Random forests provide an improvement over bagged trees by way of a
@@ -727,7 +759,7 @@ setting.
@@ -743,6 +775,10 @@ Random_Forest_model = RandomForestClassifier(n_estimators=10)['test_score']
+
+
+
The tuning parameter \( \alpha \) controls a trade-off between the subtree’s
@@ -557,14 +559,16 @@ subtree corresponding to \( \alpha \).
+
+
+
+
The plain decision trees suffer from high
@@ -690,7 +722,7 @@ predictor, averaged over all \( B \) trees.
Random forests provide an improvement over bagged trees by way of a
@@ -732,7 +764,7 @@ setting.
@@ -748,6 +780,10 @@ Random_Forest_model = RandomForestClassifier
accuracy = cross_validate(Random_Forest_model,X,Y,cv=10)['test_score']
+
+
+Random forests
+Random forests
A simple scikit-learn example
+A simple scikit-learn example
-
+
+
-
-Pros and cons of trees, pros
+The zoo data
+import pandas as pd
+import numpy as np
+from pprint import pprint
+from sklearn.tree import DecisionTreeClassifier
+
+#Import the dataset
+dataset = pd.read_csv('data/zoo.csv')
+#We drop the animal names since this is not a good feature to split the data on
+#dataset=dataset.drop('animal_name',axis=1)
+#Split the data into a training and a testing set
+train_features = dataset.iloc[:80,:-1]
+test_features = dataset.iloc[80:,:-1]
+train_targets = dataset.iloc[:80,-1]
+test_targets = dataset.iloc[80:,-1]
+#Train the model
+tree = DecisionTreeClassifier(criterion = 'entropy').fit(train_features,train_targets)
+#Predict the classes of new, unseen data
+prediction = tree.predict(test_features)
+#Check the accuracy
+print("The prediction accuracy is: ",tree.score(test_features,test_targets)*100,"%")
+
+
+Pros and cons of trees, pros
-Disadvantages
+Disadvantages
-Bagging
+Bagging
-Random forests
+Random forests
-A simple scikit-learn example
+A simple scikit-learn example
Boosting
+More material to come here.
diff --git a/doc/pub/DecisionTrees/html/DecisionTrees.html b/doc/pub/DecisionTrees/html/DecisionTrees.html
index bc478fe7c..f2ecaf576 100644
--- a/doc/pub/DecisionTrees/html/DecisionTrees.html
+++ b/doc/pub/DecisionTrees/html/DecisionTrees.html
@@ -81,11 +81,13 @@ div { text-align: justify; text-justify: inter-word; }
('A schematic procedure', 2, None, '___sec10'),
('A classification tree', 2, None, '___sec11'),
('Growing a classification tree', 2, None, '___sec12'),
- ('Pros and cons of trees, pros', 2, None, '___sec13'),
- ('Disadvantages', 2, None, '___sec14'),
- ('Bagging', 2, None, '___sec15'),
- ('Random forests', 2, None, '___sec16'),
- ('A simple scikit-learn example', 2, None, '___sec17')]}
+ ('The zoo data', 2, None, '___sec13'),
+ ('Pros and cons of trees, pros', 2, None, '___sec14'),
+ ('Disadvantages', 2, None, '___sec15'),
+ ('Bagging', 2, None, '___sec16'),
+ ('Random forests', 2, None, '___sec17'),
+ ('A simple scikit-learn example', 2, None, '___sec18'),
+ ('Boosting', 2, None, '___sec19')]}
end of tocinfo -->
@@ -526,7 +528,7 @@ $$
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.
+rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node.
-
+
+
-
-Pros and cons of trees, pros
+The zoo data
+import pandas as pd
+import numpy as np
+from pprint import pprint
+from sklearn.tree import DecisionTreeClassifier
+
+#Import the dataset
+dataset = pd.read_csv('data/zoo.csv')
+#We drop the animal names since this is not a good feature to split the data on
+#dataset=dataset.drop('animal_name',axis=1)
+#Split the data into a training and a testing set
+train_features = dataset.iloc[:80,:-1]
+test_features = dataset.iloc[80:,:-1]
+train_targets = dataset.iloc[:80,-1]
+test_targets = dataset.iloc[80:,-1]
+#Train the model
+tree = DecisionTreeClassifier(criterion = 'entropy').fit(train_features,train_targets)
+#Predict the classes of new, unseen data
+prediction = tree.predict(test_features)
+#Check the accuracy
+print("The prediction accuracy is: ",tree.score(test_features,test_targets)*100,"%")
+
+
+Pros and cons of trees, pros
-Disadvantages
+Disadvantages
-Bagging
+Bagging
-Random forests
+Random forests
-A simple scikit-learn example
+A simple scikit-learn example
Boosting
+More material to come here.
diff --git a/doc/pub/DecisionTrees/ipynb/ipynb-DecisionTrees-src.tar.gz b/doc/pub/DecisionTrees/ipynb/ipynb-DecisionTrees-src.tar.gz
index 46ada96f8..e1262d3cd 100644
Binary files a/doc/pub/DecisionTrees/ipynb/ipynb-DecisionTrees-src.tar.gz and b/doc/pub/DecisionTrees/ipynb/ipynb-DecisionTrees-src.tar.gz differ
diff --git a/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer-handouts2x3.pdf b/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer-handouts2x3.pdf
index 2810e4545..8833d3f98 100644
Binary files a/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer-handouts2x3.pdf and b/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer-handouts2x3.pdf differ
diff --git a/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer.pdf b/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer.pdf
index ed923f053..6a968974b 100644
Binary files a/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer.pdf and b/doc/pub/DecisionTrees/pdf/DecisionTrees-beamer.pdf differ
diff --git a/doc/pub/DecisionTrees/pdf/DecisionTrees-minted.pdf b/doc/pub/DecisionTrees/pdf/DecisionTrees-minted.pdf
index ea351361c..41f9b70c7 100644
Binary files a/doc/pub/DecisionTrees/pdf/DecisionTrees-minted.pdf and b/doc/pub/DecisionTrees/pdf/DecisionTrees-minted.pdf differ
diff --git a/doc/src/DecisionTrees/DecisionTrees.do.txt b/doc/src/DecisionTrees/DecisionTrees.do.txt
index 2691dbe60..a2b602cbb 100644
--- a/doc/src/DecisionTrees/DecisionTrees.do.txt
+++ b/doc/src/DecisionTrees/DecisionTrees.do.txt
@@ -370,7 +370,7 @@ For each value of $\alpha$ there corresponds a subtree $T \in T_0$ such that
!et
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.
+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
com- plexity and its fit to the training data. When $\alpha = 0$, then the
@@ -395,9 +395,11 @@ subtree corresponding to $\alpha$.
!bblock Building a Regression Tree
o 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.
-o Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of $\alpha$.
-o 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 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$.
-o Then we average the results for each value of $alpha$, and pick $\alpha$ to minimize the average error.
+o Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of $\alpha$.
+o 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:
+ * 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.
o Return the subtree from Step 2 that corresponds to the chosen value of $\alpha$.
!eblock
@@ -439,6 +441,31 @@ split, since these two approaches are more sensitive to node purity
than is the classification error rate.
+!split
+===== The zoo data =====
+!bc pycod
+import pandas as pd
+import numpy as np
+from pprint import pprint
+from sklearn.tree import DecisionTreeClassifier
+
+#Import the dataset
+dataset = pd.read_csv('data/zoo.csv')
+#We drop the animal names since this is not a good feature to split the data on
+#dataset=dataset.drop('animal_name',axis=1)
+#Split the data into a training and a testing set
+train_features = dataset.iloc[:80,:-1]
+test_features = dataset.iloc[80:,:-1]
+train_targets = dataset.iloc[:80,-1]
+test_targets = dataset.iloc[80:,-1]
+#Train the model
+tree = DecisionTreeClassifier(criterion = 'entropy').fit(train_features,train_targets)
+#Predict the classes of new, unseen data
+prediction = tree.predict(test_features)
+#Check the accuracy
+print("The prediction accuracy is: ",tree.score(test_features,test_targets)*100,"%")
+
+!ec
!split
===== Pros and cons of trees, pros =====
@@ -553,3 +580,8 @@ Random_Forest_model = RandomForestClassifier(n_estimators=100,criterion="entropy
#Cross validation
accuracy = cross_validate(Random_Forest_model,X,Y,cv=10)['test_score']
!ec
+
+
+!split
+===== Boosting =====
+More material to come here.