Decision Trees are non-parametric supervised learning models that partition feature space into recursive axis-aligned decision boundaries using metric splits like Gini Impurity or Entropy.
flowchart TD
Root["Root Node: Feature X <= 2.5?"] -- "Yes" --> Left["Leaf Node: Class 0 (Pure)"]
Root -- "No" --> Right["Sub-Node: Feature Y <= 50.0?"]
Right -- "Yes" --> Leaf1["Leaf Node: Class 1"]
Right -- "No" --> Leaf2["Leaf Node: Class 0"]
import numpy as np
def calculate_gini_impurity(y: np.ndarray) -> float:
"""Calculate Gini Impurity for a target class array."""
if len(y) == 0:
return 0.0
_, counts = np.unique(y, return_counts=True)
probabilities = counts / len(y)
gini = 1.0 - np.sum(probabilities ** 2)
return float(gini)
if __name__ == "__main__":
# Pure node (all class 1)
pure_labels = np.array([1, 1, 1, 1])
# Impure split node (half class 0, half class 1)
impure_labels = np.array([0, 0, 1, 1])
print(f"Gini Impurity (Pure Node) : {calculate_gini_impurity(pure_labels):.4f}")
print(f"Gini Impurity (Impure Node) : {calculate_gini_impurity(impure_labels):.4f}")
max_depth: Unconstrained decision trees grow until every leaf node is pure, leading to extreme overfitting. Constrain tree depth with max_depth or min_samples_split.Calculate the Gini Impurity of a label array [0, 1, 1, 1] using the calculate_gini_impurity() function.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With