SwadhyAI Notes

All Python notes

Decision Tree 🌳

Making Decisions

A Decision Tree is a flowchart-like structure where an internal node represents a feature (or attribute), the branch represents a decision rule, and each leaf node represents the outcome.

✨ How it Works

The algorithm searches for the best feature to split the data, aiming to make the resulting groups as pure as possible.

python
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier

# ... (Data Loading) ...

dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)

# Visualize the tree
tree.plot_tree(dtree, feature_names=features)

🔮 Prediction

python
# Should I go see a 40-year-old US comedian with 10 years exp and rank 7?
print(dtree.predict([[40, 10, 7, 1]])) 
# Output: [1] (Yes)

💡 Gini Impurity

The decision tree uses "Gini" to calculate the quality of a split. Gini varies between 0 (pure node, all same class) and 0.5 (impure, 50/50 split).