bhylu.com

Advertisements

Decision Tree

A decision tree partitions feature space with a sequence of split rules. A small tree is easy to inspect, while a large unrestricted tree can memorise training examples.

Understand the idea

max_depth limits this toy classifier's complexity. Classification labels are categories rather than numeric distances. A prediction from a tiny hand-written dataset is an API demonstration; it does not justify deploying a rule for people or business decisions. Evaluate generalisation and consider class balance and failure costs.

Where to try it

Run locally with scikit-learn and NumPy installed in your virtual environment (py -m pip install scikit-learn numpy). These examples are executed during course review, but the online runner does not currently preload scikit-learn. The small data is for learning the API, not evidence of production model quality.

A worked example

Python

from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=1, random_state=7)
model.fit([[1], [2], [8], [9]], ["small", "small", "large", "large"])
print(model.predict([[3], [7]]).tolist())

Read the result

The predictions are small and large based on the learned split.

Try one change

Add a noisy conflicting example and inspect whether a deeper tree improves validation or only training fit.

For package options, see the scikit-learn documentation.