Machine Learning with Tree-Based Models in Python
Elie Kawerk
Data Scientist
Decision-Tree: data structure consisting of a hierarchy of nodes.
Node: question or prediction.
Three kinds of nodes:
Root: no parent node, question giving rise to two children nodes.
Internal node: one parent node, question giving rise to two children nodes.
Leaf: one parent node, no children nodes --> prediction.
Criteria to measure the impurity of a node $I (node)$:
Nodes are grown recursively.
At each node, split the data based on:
If $IG (\text{node})$= 0, declare the node a leaf.
...
# Import DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier
# Import train_test_split
from sklearn.model_selection import train_test_split
# Import accuracy_score
from sklearn.metrics import accuracy_score
# Split dataset into 80% train, 20% test
X_train, X_test, y_train, y_test= train_test_split(X, y,
test_size=0.2,
stratify=y,
random_state=1)
# Instantiate dt, set 'criterion' to 'gini'
dt = DecisionTreeClassifier(criterion='gini', random_state=1)
# Fit dt to the training set
dt.fit(X_train,y_train)
# Predict test-set labels
y_pred= dt.predict(X_test)
# Evaluate test-set accuracy
accuracy_score(y_test, y_pred)
0.92105263157894735
Machine Learning with Tree-Based Models in Python