This lesson is being piloted (Beta version)

Training Machine Learning model using Tree-based model

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How to train a Machine Learning model using Tree-based model

Objectives
  • Learn to use different Tree-based algorithm for Machine Learning training

Supervised Learning training

Train model using Decision Tree

Spliting algorithm

More information on how to apply the spliting algorithm to split the data can be found here

Pros & Cons

image

Implementation

Here we will use iris data

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.6,random_state=123)

Next we will train using DecisionTree with gini splitting algorithm:

from sklearn.tree import DecisionTreeClassifier
model_DT = DecisionTreeClassifier(max_depth=3,criterion="gini").fit(X_train,y_train)

Once done, we can visualize the tree:

from sklearn import tree
tree.plot_tree(model_DT)

However, in order to have a nicer plot:

import graphviz
dot_data = tree.export_graphviz(model_DT, out_file=None,                      
                      filled=True, rounded=True,
                      feature_names=iris.feature_names,
                      special_characters=True)  
graph = graphviz.Source(dot_data) 
graph

image

Apply decision tree model to predic output of testing data

from sklearn import metrics
y_pred_DT = model_DT.predict(X_test)
metrics.accuracy_score(y_test,y_pred_DT)

The accuracy=0.95

More information on Decision Tree can be found here

Train model using Random Forest

image

image

Pros & Cons of Random Forest

image

Implementation of Random Forest

from sklearn.ensemble import RandomForestClassifier
model_RF = RandomForestClassifier(n_estimators=20,criterion="gini").fit(X_train,y_train)
y_pred_RF = model_RF.predict(X_test)
metrics.accuracy_score(y_test,y_pred_RF)

The accuracy=0.97

In this example, we use n_estimators=20 to grow n number of trees in the forest. We can see that Random Forest result has better prediction than Decision Tree.

More information on Random Forest can be found here

Key Points

  • Decision Tree, Random Forest