Lab 7: Decision Tree Classification
Aim
Train a decision tree classifier on toy data and classify a new sample.
Objectives
- Prepare labeled samples.
- Fit DecisionTreeClassifier.
- Predict new sample and discuss decision boundaries.
Algorithm / Procedure
- Create X (features) and y (labels).
- Instantiate DecisionTreeClassifier and fit.
- Predict a test sample [1,2].
- Optionally visualize the tree.
Python Code
from sklearn.tree import DecisionTreeClassifier
X = [[0,0],[1,1],[2,2],[3,3]]
y = [0,1,1,0]
clf = DecisionTreeClassifier(random_state=0).fit(X, y)
print("Prediction for [1,2]:", clf.predict([[1,2]])[0])
Sample Output (expected)
Prediction for [1,2]: 1