Imagine you want to split your data at a node in a decision tree. What criterion does the tree use to choose the best feature and value to split on?
Think about how the tree tries to make groups that are more pure or similar.
A decision tree chooses splits that maximize information gain or reduce impurity (like Gini impurity or entropy), making child nodes more homogeneous.
What is the accuracy printed by this code after training a decision tree on the given data?
from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score X = [[0], [1], [2], [3]] y = [0, 0, 1, 1] model = DecisionTreeClassifier(random_state=42) model.fit(X, y) pred = model.predict(X) acc = accuracy_score(y, pred) print(acc)
The model is trained and tested on the same data.
The decision tree perfectly fits the training data, so accuracy is 1.0.
What happens if you set the max_depth parameter of a decision tree classifier to a very small number like 1?
Think about how limiting depth affects the tree's ability to separate classes.
Setting max_depth to 1 limits the tree to only one split, which may not capture complex patterns, causing underfitting.
You trained a decision tree classifier on a dataset where 95% of samples belong to class A and 5% to class B. Which metric is best to evaluate your model's performance?
Think about why accuracy might be misleading with imbalanced data.
Accuracy can be high by just predicting the majority class. Precision and recall help measure how well the model detects the minority class.
What error does this code raise and why?
from sklearn.tree import DecisionTreeClassifier X = [[1, 2], [3, 4]] y = [0, 1, 0] model = DecisionTreeClassifier() model.fit(X, y)
Check if the number of samples in X matches the number of labels in y.
The feature matrix X has 2 samples but the label vector y has 3 labels, causing a mismatch error.
