Lab 7: Decision Tree Classification

CILO: 2
Weeks: 26–29
Lab #7

Aim

Train a decision tree classifier on toy data and classify a new sample.

Objectives

Algorithm / Procedure

  1. Create X (features) and y (labels).
  2. Instantiate DecisionTreeClassifier and fit.
  3. Predict a test sample [1,2].
  4. 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