Bird
Raised Fist0
ML Pythonml~20 mins

Decision tree classifier in ML Python - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Decision tree classifier
Problem:Classify iris flowers into species using a decision tree classifier.
Current Metrics:Training accuracy: 100%, Validation accuracy: 70%
Issue:The model is overfitting: it performs perfectly on training data but poorly on validation data.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85%, while keeping training accuracy below 95%.
You can only change the decision tree hyperparameters such as max_depth, min_samples_split, and min_samples_leaf.
Do not change the dataset or use other models.
Hint 1
Hint 2
Hint 3
Solution
ML Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load data
iris = load_iris()
X, y = iris.data, iris.target

# Split data
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=42)

# Create decision tree with limited depth and minimum samples
model = DecisionTreeClassifier(max_depth=3, min_samples_split=5, min_samples_leaf=3, random_state=42)

# Train model
model.fit(X_train, y_train)

# Predict
train_preds = model.predict(X_train)
val_preds = model.predict(X_val)

# Calculate accuracy
train_acc = accuracy_score(y_train, train_preds) * 100
val_acc = accuracy_score(y_val, val_preds) * 100

print(f"Training accuracy: {train_acc:.2f}%")
print(f"Validation accuracy: {val_acc:.2f}%")
Added max_depth=3 to limit tree depth and reduce overfitting.
Set min_samples_split=5 to require more samples before splitting nodes.
Set min_samples_leaf=3 to ensure leaves have enough samples, simplifying the tree.
Results Interpretation

Before: Training accuracy was 100%, validation accuracy was 70%. The model memorized training data but failed to generalize.

After: Training accuracy dropped to 93.33%, validation accuracy improved to 90%. The model is simpler and generalizes better.

Limiting the complexity of a decision tree by controlling depth and minimum samples helps reduce overfitting and improves validation accuracy.
Bonus Experiment
Try using a random forest classifier instead of a single decision tree to see if ensemble methods improve validation accuracy further.
💡 Hint
Random forests combine many trees and reduce overfitting by averaging their predictions.

Practice

(1/5)
1. What is the main purpose of a Decision Tree Classifier in machine learning?
easy
A. To store data in a database
B. To perform mathematical calculations on numbers
C. To generate random numbers for simulations
D. To split data into groups based on simple questions for classification

Solution

  1. Step 1: Understand the role of decision trees

    Decision tree classifiers split data by asking simple yes/no questions to separate data into classes.
  2. Step 2: Compare options with the purpose

    Only To split data into groups based on simple questions for classification describes splitting data for classification, which is the main use of decision trees.
  3. Final Answer:

    To split data into groups based on simple questions for classification -> Option D
  4. Quick Check:

    Decision tree purpose = classification by splitting [OK]
Hint: Decision trees split data step-by-step for classification [OK]
Common Mistakes:
  • Confusing decision trees with random number generators
  • Thinking decision trees store data like databases
  • Assuming decision trees perform complex math calculations
2. Which of the following is the correct way to create a Decision Tree Classifier in Python using scikit-learn?
easy
A. from sklearn.tree import DecisionTreeClassifier model = DecisionTreeClassifier()
B. from sklearn.tree import DecisionTree model = DecisionTree()
C. import DecisionTreeClassifier from sklearn model = DecisionTreeClassifier()
D. from sklearn.classifier import DecisionTreeClassifier model = DecisionTreeClassifier()

Solution

  1. Step 1: Recall correct import syntax

    The correct import for decision tree classifier is from sklearn.tree import DecisionTreeClassifier.
  2. Step 2: Check object creation

    Creating the model with DecisionTreeClassifier() is correct. Other options have wrong imports or class names.
  3. Final Answer:

    from sklearn.tree import DecisionTreeClassifier model = DecisionTreeClassifier() -> Option A
  4. Quick Check:

    Correct import and class name = from sklearn.tree import DecisionTreeClassifier model = DecisionTreeClassifier() [OK]
Hint: Use sklearn.tree and DecisionTreeClassifier() to create model [OK]
Common Mistakes:
  • Using wrong import paths like sklearn.classifier
  • Misspelling class name as DecisionTree
  • Incorrect import syntax like import DecisionTreeClassifier from sklearn
3. Given the following code, what will be the predicted class for the input X_test = [[5, 3]]?
from sklearn.tree import DecisionTreeClassifier
X_train = [[2, 3], [10, 15], [6, 8], [1, 1]]
y_train = [0, 1, 1, 0]
model = DecisionTreeClassifier(max_depth=2)
model.fit(X_train, y_train)
print(model.predict(X_test))
medium
A. [0]
B. [1]
C. [2]
D. Error: max_depth must be greater than 2

Solution

  1. Step 1: Understand training data and labels

    Training data has points labeled 0 or 1. Points near [5,3] are closer to class 1 points like [6,8].
  2. Step 2: Predict with trained model

    The decision tree with max_depth=2 will classify [5,3] as class 1 based on training data proximity.
  3. Final Answer:

    [1] -> Option B
  4. Quick Check:

    Prediction for [5,3] = [1] [OK]
Hint: Check nearest training points to predict class [OK]
Common Mistakes:
  • Assuming max_depth=2 causes error
  • Predicting class 2 which does not exist
  • Ignoring training labels when predicting
4. What is wrong with this code snippet for training a Decision Tree Classifier?
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=-1)
model.fit(X_train, y_train)
medium
A. X_train and y_train are not defined
B. DecisionTreeClassifier does not have max_depth parameter
C. max_depth cannot be negative; it should be null or positive
D. fit() method requires extra parameters

Solution

  1. Step 1: Check max_depth parameter validity

    max_depth must be null or a positive integer. Negative values are invalid and cause errors.
  2. Step 2: Verify other code parts

    Assuming X_train and y_train are defined, and fit() requires only these two parameters, the main error is max_depth.
  3. Final Answer:

    max_depth cannot be negative; it should be null or positive -> Option C
  4. Quick Check:

    max_depth negative = error [OK]
Hint: max_depth must be positive or null, never negative [OK]
Common Mistakes:
  • Thinking max_depth can be negative
  • Assuming fit() needs more parameters
  • Confusing parameter names
5. You want to avoid overfitting in a Decision Tree Classifier trained on a noisy dataset. Which approach is best to control this?
hard
A. Limit the tree depth using the max_depth parameter
B. Increase the number of features used for splitting
C. Train the tree without any depth limit
D. Use a decision tree without pruning

Solution

  1. Step 1: Understand overfitting in decision trees

    Overfitting happens when the tree is too deep and fits noise instead of general patterns.
  2. Step 2: Choose method to reduce overfitting

    Limiting max_depth controls tree size and prevents overfitting. Increasing features or no pruning can worsen it.
  3. Final Answer:

    Limit the tree depth using the max_depth parameter -> Option A
  4. Quick Check:

    Control overfitting = limit max_depth [OK]
Hint: Limit tree depth to prevent overfitting [OK]
Common Mistakes:
  • Thinking more features reduce overfitting
  • Believing no depth limit is better
  • Ignoring pruning or depth control
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard