Help Understanding Cross Validation and Decision Trees
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Understanding Cross Validation and Decision Trees
In the domain of machine learning, both cross-validation and decision trees are fundamental concepts that enhance model robustness and interpretability. Understanding these concepts is crucial for building effective predictive models.
Cross Validation
Cross-validation is a resampling procedure used to assess the performance of a machine learning model. It is primarily used to estimate the skill of a model on unseen data. The central idea behind cross-validation is to divide the dataset into subsets, train the model on a subset, and validate it on another.
Key Types of Cross Validation:
- K-Fold Cross Validation: • The dataset is divided into
kequally (or nearly equally) sized folds. • The model is trainedktimes, each time using a different fold as the test set and the remainingk-1folds as the training set. • The performance measure is averaged over thektrials. - Leave-One-Out Cross Validation (LOOCV): • A specific case of k-fold where
kis equal to the number of data points. • Each sample is used once as a test set while the rest are used as the training set. - Stratified K-Fold Cross Validation: • Similar to k-fold cross-validation, but ensures each fold is a good representative of the whole by preserving the percentage of samples for each class.
Example of K-Fold Cross Validation:
Suppose we have a dataset with 100 observations and opt for a 5-fold cross-validation. The dataset will be divided into 5 folds, where each fold contains 20 observations. The model will train and test as shown below:
• Iteration 1: Train on folds 2, 3, 4, 5; Test on fold 1 • Iteration 2: Train on folds 1, 3, 4, 5; Test on fold 2 • ... • Iteration 5: Train on folds 1, 2, 3, 4; Test on fold 5
Mathematical Insight:
For a given model and k-fold: • Total error,
Where represents the error on fold i
.
Decision Trees
Decision trees are a non-parametric supervised learning method used for classification and regression. They are intuitive and model outcomes based on decisions rules derived from the features.
Key Characteristics of Decision Trees:
• Nodes: Represents features tested. • Edges: Outcome of a test and leads to the next node. • Leaves: Terminal nodes that denote a class label or regression output.
Example of a Decision Tree:
Consider a binary classification task to predict whether a loan application is approved:
• Select the Best Feature: Select the feature that splits the data best based on the chosen criterion (e.g., Gini index, information gain). • Split the Node: Divide the dataset into subsets, one for each branch extending from the node. • Repeat: Recursively build each subtree using the remaining attributes. • Gini Index: Measures impurity based on class probabilities. A Gini index of 0 denotes purity. • Entropy: Quantifies information and measures the uncertainty in data. • Addresses model overfitting by truncating branches that have little importance. • Easy interpretability. • Requires little data preprocessing. • Handles both numerical and categorical data. • Prone to overfitting. • Can be unstable; small changes in data might lead to different trees.
Related reading
- Heroku deploying Deep Learning model
- Heroku tensorflow 2.2.1 too large for deployment
- Hidden Markov Model for multiple observed variables
- hidden markov model thresholding
- Heuristic for finding elements that appears often together in a big data set
- Hidden Markov models package in R
- Hopcroft–Karp algorithm in Python
- How can I remove a specific item from an array in JavaScript?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.