
Many models are black boxes. A decision tree is the opposite: it learns a set of if/else questions you can print, read and explain to your manager. It works on the same kind of table you keep in Excel.
In this article
Train one in 8 lines
“`python
# pip install scikit-learn pandas
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text
X, y = load_iris(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42, stratify=y)
tree = DecisionTreeClassifier(max_depth=3, random_state=42).fit(X_train, y_train)
print(“test accuracy:”, round(tree.score(X_test, y_test), 3))
print(export_text(tree, feature_names=list(X.columns)))
“`
The iris dataset (flower measurements β species) ships with scikit-learn, so this runs anywhere. export_text prints rules like:
“`text
|— petal length (cm) <= 2.45
| |--- class: 0
|--- petal length (cm) > 2.45
| |— petal width (cm) <= 1.75
| | |--- class: 1
```
How the tree chooses questions
At each step the algorithm tries every column and many split points, and picks the question that makes the resulting groups most “pure” (mostly one class). By default it measures purity with Gini impurity. Then it repeats inside each group.
Why the test split matters
Scoring a model on the data it trained on is like marking your own exam with the answer key in hand. train_test_split holds back 25% of rows the tree never sees; the accuracy on those is the honest number.
Overfitting in one experiment
“`python
for depth in [1, 2, 3, 5, None]:
t = DecisionTreeClassifier(max_depth=depth, random_state=42).fit(X_train, y_train)
print(depth, “train”, round(t.score(X_train, y_train), 3), “test”, round(t.score(X_test, y_test), 3))
“`
With no depth limit the tree can reach 100% on training data by memorising every row β but test accuracy stops improving or falls. Limiting depth (or requiring a minimum number of rows per leaf with min_samples_leaf) keeps the rules general.
Your own Excel data
“`python
import pandas as pd
df = pd.read_excel(“loans.xlsx”) # e.g. Income, Age, Existing_EMI, Approved
X = pd.get_dummies(df.drop(columns=”Approved”)) # text columns -> 0/1 columns
y = df[“Approved”]
“`
get_dummies converts text categories (City, Employment type) into numeric columns, because trees in scikit-learn need numbers.
Feature importance
“`python
for name, imp in sorted(zip(X.columns, tree.feature_importances_), key=lambda t: -t[1]):
print(f”{name:25s} {imp:.2f}”)
“`
When to use something stronger
Single trees are readable but a little unstable. Random forests and gradient boosting (many trees voting) are usually more accurate on business tables β swap in RandomForestClassifier with the same code. You lose the one-page rule list but gain accuracy.