A Step-by-Step Guide to Decision Tree Classifiers Using Scikit-Learn
A Decision Tree Classifier is a type of supervised learning algorithm used for classifying data by iteratively partitioning it based on feature specific decision criteria. Insid...
A Decision Tree Classifier is a type of supervised learning algorithm used for classifying data by iteratively partitioning it based on feature-specific decision criteria. Inside the tree, each node represents a condition on a feature, branches indicate the result of these conditions, and leaf nodes assign final class labels.
Understanding DecisionTreeClassifier
Scikit-learn's DecisionTreeClassifier class facilitates the creation of decision tree models. Below is the basic syntax for initializing the class:
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(
criterion='gini',
splitter='best',
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.0,
max_features=None,
random_state=None,
max_leaf_nodes=None,
min_impurity_decrease=0.0,
class_weight=None,
ccp_alpha=0.0,
monotonic_cst=None
)
Key Parameters:
- criterion: The function to measure the quality of a split (options: 'gini', 'entropy').
- splitter: The strategy used to choose the split at each node (options: 'best', 'random').
- max_features: The number of features to consider for each split.
- max_depth: The maximum depth of the tree.
- min_samples_split: The minimum number of samples required to split an internal node.
- min_samples_leaf: The minimum number of samples required to be at a leaf node.
- max_leaf_nodes: The maximum number of leaf nodes.
- min_impurity_decrease: The minimum impurity decrease to split a node.
- class_weight: Weights associated with classes to balance the class distribution.
- ccp_alpha: A complexity parameter used for Minimal Cost-Complexity Pruning.

This structure enables the model to be interpretable and effective for classification tasks.
Step-by-Step Implementation
Below is a practical implementation of a Decision Tree Classifier using Scikit-Learn.
1. Importing Libraries
First, import the necessary libraries for machine learning tasks.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
2. Loading the Dataset
To perform classification, load a dataset. Scikit-learn offers sample datasets like Iris or Breast Cancer.
data = load_iris()
X = data.data
y = data.target
3. Splitting the Dataset
Use train_test_split to divide the dataset into training and testing sets.
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=99)
4. Defining the Model
Create an instance of the Decision Tree Classifier.
clf = DecisionTreeClassifier(random_state=1)
5. Training the Model
Train the classifier using the fit method on the training data.
clf.fit(X_train, y_train)
6. Making Predictions
Use the predict method on the test data to generate predictions and calculate accuracy.
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')
7. Hyperparameter Tuning with GridSearchCV
Hyperparameters control how the decision tree learns from the data. Proper tuning can enhance model accuracy, reduce overfitting, and improve generalization. Techniques like Grid Search evaluate multiple parameter combinations to find the optimal configuration.
Here's how to use Scikit-Learn's GridSearchCV for hyperparameter tuning:
from sklearn.model_selection import GridSearchCV
param_grid = {
'max_depth': range(1, 10, 1),
'min_samples_leaf': range(1, 20, 2),
'min_samples_split': range(2, 20, 2),
'criterion': ["entropy", "gini"]
}
tree = DecisionTreeClassifier(random_state=1)
grid_search = GridSearchCV(estimator=tree, param_grid=param_grid,
cv=5, verbose=True)
grid_search.fit(X_train, y_train)
print("best accuracy", grid_search.best_score_)
print(grid_search.best_estimator_)
8. Visualizing the Decision Tree Classifier
Visualizing the Decision Tree helps in interpreting the model's decisions. Plot the feature importance from the Decision Tree model to identify the most influential features.
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
tree_clf = grid_search.best_estimator_
plt.figure(figsize=(18, 15))
plot_tree(tree_clf, filled=True, feature_names=data.feature_names,
class_names=data.target_names)
plt.show()
The tree begins from the root node, checking if the feature, such as flower petal width, meets certain conditions to classify the data points into their respective categories.