TITLE: Week 45: Decisions Trees, Random Forests, Bagging and Boosting AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University DATE: today !split ===== Overview of week 45 ===== * Thursday: Basics of Decision Trees, Bagging and Voting * Friday: More on Bagging, Voting, Random Forests and start Boosting !bblock Videos o "Video on Decision trees":"https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn" o "Video on boosting methods by Hastie":"https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai". !eblock !bblock Reading o 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. !eblock !split ===== Decision trees, overarching aims ===== We start here with the most basic algorithm, the so-called decision tree. With this basic algorithm we can in turn build more complex networks, spanning from homogeneous and heterogenous forests (bagging, random forests and more) to one of the most popular supervised algorithms nowadays, the extreme gradient boosting, or just XGBoost. But let us start with the simplest possible ingredient. Decision trees are supervised learning algorithms used for both, classification and regression tasks. The main idea of decision trees is to find those descriptive features which contain the most _information_ regarding the target feature and then split the dataset along the values of these features such that the target feature values for the resulting underlying datasets are as pure as possible. The descriptive features which reproduce best the target/output features are normally said to be the most informative ones. The process of finding the _most informative_ feature is done until we accomplish a stopping criteria where we then finally end up in so called _leaf nodes_. !split ===== Basics of a tree ===== A decision tree is typically divided into a _root node_, the _interior nodes_, and the final _leaf nodes_ or just _leaves_. These entities are then connected by so-called _branches_. The leaf nodes contain the predictions we will make for new query instances presented to our trained model. This is possible since the model has learned the underlying structure of the training data and hence can, given some assumptions, make predictions about the target feature value (class) of unseen query instances. !split ===== A Sketch of a Tree, Regression problem ===== #FIGURE: [DataFiles/Regsimpletree.png, width=600 frac=0.8] !split ===== A Sketch of a Tree, Classification problem ===== #FIGURE: [DataFiles/Classimpletree.png, width=600 frac=0.8] !split ===== A typical Decision Tree with its pertinent Jargon, Classification Problem ===== FIGURE: [DataFiles/cancer.png, width=600 frac=0.8] This tree was produced using the Wisconsin cancer data (discussed here as well, see code examples below) using _Scikit-Learn_'s decision tree classifier. Here we have used the so-called _gini_ index (see below) to split the various branches. !split ===== General Features ===== The overarching approach to decision trees is a top-down approach. * A leaf provides the classification of a given instance. * A node specifies a test of some attribute of the instance. * A branch corresponds to a possible values of an attribute. * An instance is classified by starting at the root node of the tree, testing the attribute specified by this node, then moving down the tree branch corresponding to the value of the attribute in the given example. This process is then repeated for the subtree rooted at the new node. !split ===== How do we set it up? ===== In simplified terms, the process of training a decision tree and predicting the target features of query instances is as follows: o Present a dataset containing of a number of training instances characterized by a number of descriptive features and a target feature o Train the decision tree model by continuously splitting the target feature along the values of the descriptive features using a measure of information gain during the training process o Grow the tree until we accomplish a stopping criteria create leaf nodes which represent the *predictions* we want to make for new query instances o Show query instances to the tree and run down the tree until we arrive at leaf nodes Then we are essentially done! !split ===== Decision trees and Regression ===== !bc pycod import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression steps=250 distance=0 x=0 distance_list=[] steps_list=[] while x 0).astype(np.float32) * 2 angle = np.pi/4 rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]) Xsr = Xs.dot(rotation_matrix) tree_clf_s = DecisionTreeClassifier(random_state=42) tree_clf_s.fit(Xs, ys) tree_clf_sr = DecisionTreeClassifier(random_state=42) tree_clf_sr.fit(Xsr, ys) plt.figure(figsize=(11, 4)) plt.subplot(121) plot_decision_boundary(tree_clf_s, Xs, ys, axes=[-0.7, 0.7, -0.7, 0.7], iris=False) plt.subplot(122) plot_decision_boundary(tree_clf_sr, Xsr, ys, axes=[-0.7, 0.7, -0.7, 0.7], iris=False) plt.show() !ec !split ===== Regression trees ===== !bc pycod # 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 !ec !bc pycod from sklearn.tree import DecisionTreeRegressor tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42) tree_reg.fit(X, y) !ec !split ===== Final regressor code ===== !bc pycod 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() !ec !bc pycod 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() !ec !split ===== 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) !split ===== 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. !split ===== 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 o Voting classifiers o Bagging and Pasting o Random forests o Boosting methods, from adaptive to Extreme Gradient Boosting (XGBoost) We discuss these methods here. !split ===== An Overview of Ensemble Methods ===== FIGURE: [DataFiles/ensembleoverview.png, width=600 frac=0.8] !split ===== 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. !split ===== 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. !split ===== 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$). !bc pycod 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 = 1000 n_boostraps = 100 maxdepth = 10 # 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) # we produce a simple tree first as benchmark, no scaling simpletree = DecisionTreeRegressor(max_depth=3) simpletree.fit(X_train, y_train) simpleprediction = simpletree.predict(X_test) 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, y_train) model.fit(x_, y_) y_pred[:, i] = model.predict(X_test)#.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(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() !ec !split ===== 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. !split ===== 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":"https://en.wikipedia.org/wiki/Law_of_large_numbers" numbers kicking in. !split ===== Standard imports first ===== !bc pycod # 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') !ec !split ===== Simple Voting Example, head or tail ===== !bc pycod # 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() !ec !split ===== 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_. !bc pycod 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)) !ec !split ===== Voting and Bagging ===== !bc pycod 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) !ec !bc pycod 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)) !ec !bc pycod 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) !ec !bc pycod 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)) !ec !split ===== Random forests ===== 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 !bt \[ m\approx \sqrt{p}. \] !et 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. !split ===== 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. o For $b=1:B$ * Draw a bootstrap sample from the training data organized in our $\bm{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 o we select $m \le p$ variables at random from the $p$ predictors/features o pick the best split point among the $m$ features using for example the CART algorithm and create a new node o split the node into daughter nodes o 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. !split ===== Random Forests Compared with other Methods on the Cancer Data ===== !bc pycod 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) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) # Support vector machine svm = SVC(gamma='auto', C=100) svm.fit(X_train, y_train) print("Test set accuracy with SVM: {:.2f}".format(svm.score(X_test,y_test))) # Decision Trees deep_tree_clf = DecisionTreeClassifier(max_depth=None) deep_tree_clf.fit(X_train, y_train) print("Test set accuracy with Decision Trees: {:.2f}".format(deep_tree_clf.score(X_test,y_test))) #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) # 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() !ec 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. !split ===== Compare Bagging on Trees with Random Forests ===== !bc pycod 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) !ec !bc pycod 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) !ec !split ===== Boosting, a Bird's Eye View ===== 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. 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. !split ===== What is boosting? Additive Modelling/Iterative Fitting ===== 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 !bt \[ f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m), \] !et 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$. 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 !bt \[ \sigma(t) = \frac{1}{1+\exp{(-t)}}, \] !et 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 !bt \[ C(\bm{y},\bm{f}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-f(x_i))^2. \] !et In this case the function $f(x)$ was replaced by the design matrix $\bm{X}$ and the unknown linear regression parameters $\bm{\beta}$, that is $\bm{f}=\bm{X}\bm{\beta}$. In linear regression we can simply invert a matrix and obtain the parameters $\beta$ by !bt \[ \bm{\beta}=\left(\bm{X}^T\bm{X}\right)^{-1}\bm{X}^T\bm{y}. \] !et In iterative fitting or additive modeling, we minimize the cost function with respect to the parameters $\beta_m$ and $\gamma_m$. !split ===== Iterative Fitting, Regression and Squared-error Cost Function ===== The way we proceed is as follows (here we specialize to the squared-error cost function) o Establish a cost function, here ${\cal C}(\bm{y},\bm{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)$. o Initialize with a guess $f_0(x)$. It could be one or even zero or some random numbers. o For $m=1:M$ o minimize $\sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta b(x;\gamma))^2$ wrt $\gamma$ and $\beta$ o This gives the optimal values $\beta_m$ and $\gamma_m$ o 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. !split ===== Squared-Error Example and Iterative Fitting ===== 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 !bt \[ (\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. \] !et We start our iteration by simply setting $f_0(x)=0$. Taking the derivatives with respect to $\beta$ and $\gamma$ we obtain !bt \[ \frac{\partial {\cal C}}{\partial \beta} = -2\sum_{i}(1+\gamma x_i)(y_i-\beta(1+\gamma x_i))=0, \] !et and !bt \[ \frac{\partial {\cal C}}{\partial \gamma} =-2\sum_{i}\beta x_i(y_i-\beta(1+\gamma x_i))=0. \] !et We can then rewrite these equations as (defining $\bm{w}=\bm{e}+\gamma \bm{x})$ with $\bm{e}$ being the unit vector) !bt \[ \gamma \bm{w}^T(\bm{y}-\beta\gamma \bm{w})=0, \] !et which gives us $\beta = \bm{w}^T\bm{y}/(\bm{w}^T\bm{w})$. Similarly we have !bt \[ \beta\gamma \bm{x}^T(\bm{y}-\beta(1+\gamma \bm{x}))=0, \] !et which leads to $\gamma =(\bm{x}^T\bm{y}-\beta\bm{x}^T\bm{e})/(\beta\bm{x}^T\bm{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$. !split ===== Iterative Fitting, Classification and AdaBoost ===== 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\}$. The error rate of the training sample is then !bt \[ \mathrm{\overline{err}}=\frac{1}{n} \sum_{i=0}^{n-1} I(y_i\ne G(x_i)). \] !et 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 !bt \[ f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m), \] !et will be a function of !bt \[ G_M(x) = \mathrm{sign} \sum_{i=1}^M \alpha_m G_m(x). \] !et !split ===== Adaptive Boosting, AdaBoost ===== In our iterative procedure we define thus !bt \[ f_m(x) = f_{m-1}(x)+\beta_mG_m(x). \] !et 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 !bt \[ C(\bm{y},\bm{f}) = \sum_{i=0}^{n-1}\exp{(-y_i(f_{m-1}(x_i)+\beta G(x_i))}. \] !et 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 !bt \[ C(\bm{y},\bm{f}) = \sum_{i=0}^{n-1}w_i^{m}\exp{(-y_i\beta G(x_i))}, \] !et where we have defined $w_i^m= \exp{(-y_if_{m-1}(x_i))}$. !split ===== Building up AdaBoost ===== First, for any $\beta > 0$, we optimize $G$ by setting !bt \[ G_m(x) = \mathrm{sign} \sum_{i=0}^{n-1} w_i^m I(y_i \ne G_(x_i)), \] !et which is the classifier that minimizes the weighted error rate in predicting $y$. We can do this by rewriting !bt \[ \exp{-(\beta)}\sum_{y_i=G(x_i)}w_i^m+\exp{(\beta)}\sum_{y_i\ne G(x_i)}w_i^m, \] !et which can be rewritten as !bt \[ (\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, \] !et which leads to !bt \[ \beta_m = \frac{1}{2}\log{\frac{1-\mathrm{\overline{err}}}{\mathrm{\overline{err}}}}, \] !et where we have redefined the error as !bt \[ \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}, \] !et which leads to an update of !bt \[ f_m(x) = f_{m-1}(x) +\beta_m G_m(x). \] !et This leads to the new weights !bt \[ w_i^{m+1} = w_i^m \exp{(-y_i\beta_m G_m(x_i))} \] !et !split ===== 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 $\bm{X}=[\bm{x}_0\bm{x}_1\dots\bm{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 $\bm{y}$. We have already defined the misclassification error $\mathrm{err}$ as !bt \[ \mathrm{err}=\frac{1}{n}\sum_{i=0}^{n-1}I(y_i\ne G(x_i)), \] !et where the function $I()$ is one if we misclassify and zero if we classify correctly. !split ===== Basic Steps of AdaBoost ===== 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. o 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$. o We rewrite the misclassification error as !bt \[ \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}, \] !et o 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. o Fit then a given classifier to the training set using the weights $w_i$. o Compute then $\mathrm{err}$ and figure out which events are classified properly and which are classified wrongly. o Define a quantity $\alpha_{m} = \log{(1-\mathrm{\overline{err}}_m)/\mathrm{\overline{err}}_m}$ o Set the new weights to $w_i = w_i\times \exp{(\alpha_m I(y_i\ne G(x_i)}$. o Compute the new classifier $G(x)= \sum_{i=0}^{n-1}\alpha_m I(y_i\ne G(x_i)$. 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. !split ===== AdaBoost Examples ===== Using _Scikit-Learn_ it is easy to apply the adaptive boosting algorithm, as done here. !bc pycod from sklearn.ensemble import AdaBoostClassifier ada_clf = AdaBoostClassifier( DecisionTreeClassifier(max_depth=1), n_estimators=200, algorithm="SAMME.R", learning_rate=0.5, random_state=42) ada_clf.fit(X_train, y_train) from sklearn.ensemble import AdaBoostClassifier ada_clf = AdaBoostClassifier( DecisionTreeClassifier(max_depth=1), n_estimators=200, algorithm="SAMME.R", learning_rate=0.5, random_state=42) ada_clf.fit(X_train_scaled, y_train) y_pred = ada_clf.predict(X_test_scaled) skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) plt.show() y_probas = ada_clf.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() !ec !split ===== Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent ===== 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. !split ===== The Squared-Error again! Steepest Descent ===== We start again with our cost function ${\cal C}(\bm{y}m\bm{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 !bt \[ (\hat{\bm{f}}) = \mathrm{argmin}_{\bm{f}}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i-f(x_i))^2. \] !et We define a real function $h_m(x)$ that defines our final function $f_M(x)$ as !bt \[ f_M(x) = \sum_{m=0}^M h_m(x). \] !et 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 !bt \[ 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)}. \] !et 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 !bt \[ (\rho_1) = \mathrm{argmin}_{\rho}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i+2\rho y_i)^2. \] !et !split ===== Steepest Descent Example ===== Optimizing with respect to $\rho$ we obtain (taking the derivative) that $\rho_1 = -1/2$. We have then that !bt \[ f_1(x) = f_{0}(x) -\rho_1 g_1(x)=-y_i. \] !et We can then proceed and compute !bt \[ 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, \] !et 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_. !split ===== Gradient Boosting, algorithm ===== 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. 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 !bt \[ C(\bm{y},\bm{f})=\sum_{i=0}^{n-1}(y_i-f(x_i))^2. \] !et The way we proceed in an iterative fashion is to o Initialize our estimate $f_0(x)$. o For $m=1:M$, we o compute the negative gradient vector $\bm{u}_m = -\partial C(\bm{y},\bm{f})/\partial \bm{f}(x)$ at $f(x) = f_{m-1}(x)$; o fit the so-called base-learner to the negative gradient $h_m(u_m,x)$; o update the estimate $f_m(x) = f_{m-1}(x)+h_m(u_m,x)$; o The final estimate is then $f_M(x) = \sum_{m=1}^M h_m(u_m,x)$. !split ===== Gradient Boosting, Examples of Regression ===== !bc pycod import matplotlib.pyplot as plt import numpy as np from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingRegressor from sklearn.preprocessing import StandardScaler 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) scaler = StandardScaler() scaler.fit(X_train) X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) for degree in range(1,maxdegree): model = GradientBoostingRegressor(max_depth=degree, n_estimators=100, learning_rate=1.0) model.fit(X_train_scaled,y_train) y_pred = model.predict(X_test_scaled) 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() !ec !split ===== Gradient Boosting, Classification Example ===== !bc pycod 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 Random Forests 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() !ec !split ===== XGBoost: Extreme Gradient Boosting ===== "XGBoost":"https://github.com/dmlc/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":"https://arxiv.org/abs/1603.02754". 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!!! !split ===== Regression Case ===== !bc pycod import matplotlib.pyplot as plt import numpy as np from sklearn.model_selection import train_test_split import xgboost as xgb from sklearn.preprocessing import StandardScaler 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) scaler = StandardScaler() scaler.fit(X_train) X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) 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_scaled,y_train) y_pred = model.predict(X_test_scaled) 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() !ec !split ===== 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. !bc pycod 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 Random Forests 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() !ec