Cross Validation Explained!

CodeEmporium · Beginner ·🔢 Mathematical Foundations ·3y ago

Key Takeaways

Cross validation techniques for evaluating machine learning models, including k-fold cross validation and leave one out cross validation, are explained by CodeEmporium, with resources provided for further learning, such as simulations and textbooks.

Full Transcript

hello everyone and welcome to another episode of Code Emporium where we're going to talk about validation techniques and their importance so in typical regression what we want to do is when we have a cloud of data points we want to draw a curve through them it kind of looks like this now mathematically this would involve creating a linear regression perhaps with parameters Theta 1 and Theta 0 as the slope and The Intercept of the line and we would evaluate this or train this model using the mean squared error and the mean squared error is the difference between the actual value and the predicted value which is going to be essentially just the equation of this line now why you mean squared error is actually so good is because it can be decomposed into a bias term as well as a variance term now I've explained a lot about bias and variance in another video but essentially good machine learning models have both a low bias for the estimate of thetas and also a low variance for the estimations of the thetas and because mean squared error is proportional to both of these a lower mean squared error would involve creating a better model mean squared error is used during training as well as evaluation time of your model now with the with model evaluation why are validation techniques themselves required well the goal of validation and in general to evaluate your model you want to find the true model performance on unseen data and in the most Ideal World we could basically just keep getting data from The Real World and and just validate it probably with like mean squared error However unfortunately we in the real world don't really have access to just unlimited amounts of data and instead we are working with a data set or just a fixed amount of data that we use for both training and evaluating our models and so validation techniques become important working with this limited block of data now one type of validation technique is the hold out validation technique where here's our block of data which we split into a train set and a test set the train set is used to train the model and the test set is used to evaluate it and in this case we compute the holdout validation by taking the mean squared error of all of these test examples one of the fallacies of this method that you might hear is that it doesn't work that great if your data set is very small this is true for some part because this instance of the data set is actually just one instance from the broader real world data this could be for example just randomly a bad sampler or randomly a really good sample that might not be representative of how data is in the real world and what we want is the true model performance of unseen data so hold out validation may not be up to the par in that case however holdout validation is pretty reasonable with lots of data available and this is because of the law of large numbers this is a statistical principle that essentially states that as this test set goes on increasing the average or rather the mean squared error of this sample is going to be representative of the true unseen mean squared error of the population and based on our true goal that's exactly what we want and so hold out validation can actually work pretty well when you have a lot of data now another technique that's typically used in validation is cross validation where we essentially just break down our data into equal splits and we use one split for testing data whereas the remaining splits we use for training data and when we compute the mean squared error for our test data we can store that in a variable error one then we repeat this process with a different chunk where we train on the remaining chunks test on this chunk and get an error too and we repeat this process depending on the number of chunks that we decide in this case it's five so we have five errors and in the end the cross validation error is just going to be the mean of all of these errors now a neat thing that this provides over the holdout validation set is that in the case that the data set is small we now can use every single example for both training and evaluation however remember in the holdout validation set we have only some amount of data that's for training but this remaining data which we could have used for training is now used for evaluation and this training data when it's very small could have been very crucial to the model if we had more of it and so cross-validation tends to be a very preferred technique with lower limited amounts of data aside from this cross-validation also takes an average so if there are some like crazy error examples that you get here just off the fly all of those errors when you average them up will eventually come down to their truer value and so they're not affected by these outliers as much because of an average and so it's for this reason that cross-validation actually becomes very useful and versatile now when you want to use holdout versus cross validation is not a very specific rule there are General guidelines though like for example based on what we've discussed is just the amount of data where if you have a less amount of data you can probably use cross-validation however with more amounts of data the holdout group and cross-validation both perform pretty well but there are also other considerations to think about for example time dependence this means that the data that you've collected is actually determined at very specific points of time and if you have a time dependence in your data you can't really use cross-validation in the form that we discussed because of the concerns of data leakage to illustrate data leakage let's look at an example so let's say that this data block is arranged in ascending order of time so this is the earliest data this is the latest data in this particular iteration of cross-validation where we are testing on every single example in this set each of these examples are later than any of the samples that were used to train the data so this has no data leakage because this is exactly how we would use the model in production however in this case we are now testing on data where the training set could have included data from the future this is not representative of how we would actually use this model in the real world we can't use training data that we haven't seen yet and this is a good example of data leakage and it's because of this that you might see the error become low during evaluation but in the real world the model will actually not perform that well and will actually see a higher error and so with the time dependence cross validation in its current form is not usable that said there are also some time series Renditions of cross-validation that you may be able to use but for this case I've now just indicated them with hold out to use holdout validation now if there is no time dependence however you can use cross-validation and if you have lots of data you can use either cross validation or hold out as a recommendation like I mentioned before these are just recommendations but there are not strict hard and fast rules now another question that you might be thinking is how many partitions do we really need to use in cross-validation in fact we can just have two partitions one that's used for training and one that's used for evaluation or we can split this entire data set into partitions such that every sample is its own partition and this is the idea behind a specific case of cross-validation called leave one out cross validation now in order to determine the number of partitions you might want to consider how well does this actually perform and also what is the amount of compute that is required and one of the best ways to get an idea of performance is through simulation let's say that we simulated some data and we have now 40 data points for which we want to perform some form of cross validation now with 40 data points we can have the number number of partitions go as high as 40. this y-axis here is going to be the performance in terms of the complement of the mean squared error from this graph we kind of see that as long as we set the number of folds to be like around 10 or more the performance of our model actually doesn't change with even increase in the number of folds in Cross validation we also see something even similar with a larger number of data points with like 200. in this case we can now increase the number of folds all the way up to 200 but there isn't really still any kind of performance difference even if we had a very low number of partitions so here the performance itself is dependent on the amount of data because for smaller amounts of data you might have to increase the number of partitions but beyond a certain amount it really doesn't affect performance itself now for computation if you increase the number of partitions chances are it's going to take a lot more time to train but whether this is actually a huge concern depends on the kind of model you're using in the amount of data that you have because if the model itself only takes like maybe 20 to 30 Seconds To Train it's okay to increase the number of partitions without being too worried about computation power and resources and so how many partitions and cross validation really becomes less of an important question as you have more and more data and that's all I have for today I hope this gives you an intuition of what cross-validation is and how to use it very practically as well as its advantages and disadvantages of different types of techniques if you do like this video please do give it a like comment down below if there's anything that you agreed or disagreed with or if there's anything that you just learned today I'm trying to grow a community here and I want it to be about learning thank you all so much for watching and for your support and until next time

Original Description

Let's talk about an important machine learning topic used to evaluate models: cross validation ABOUT ME ⭕ Subscribe: https://www.youtube.com/c/CodeEmporium?sub_confirmation=1 📚 Medium Blog: https://medium.com/@dataemporium 💻 Github: https://github.com/ajhalthor 👔 LinkedIn: https://www.linkedin.com/in/ajay-halthor-477974bb/ RESOURCES [1] Simulation of how increasing K affects cross validation: https://stats.stackexchange.com/questions/61783/bias-and-variance-in-leave-one-out-vs-k-fold-cross-validation [2] Simulation code: https://github.com/xavierbourretsicotte/Data-Portfolio/blob/master/Cross_Validation.ipynb [3] Law of Large Numbers: https://en.wikipedia.org/wiki/Law_of_large_numbers [4] Walk forward cross validation for time series: https://otexts.com/fpp3/tscv.html [5] Introduction to Statistical Learning Textbook can provide a great basis: https://hastie.su.domains/ISLR2/ISLRv2_website.pdf [6] More info on Leave One Out cross validation: https://www.quora.com/Why-does-leave-one-out-cross-validation-have-less-bias-than-k-fold-approach MATH COURSES (7 day free trial) 📕 Mathematics for Machine Learning: https://imp.i384100.net/MathML 📕 Calculus: https://imp.i384100.net/Calculus 📕 Statistics for Data Science: https://imp.i384100.net/AdvancedStatistics 📕 Bayesian Statistics: https://imp.i384100.net/BayesianStatistics 📕 Linear Algebra: https://imp.i384100.net/LinearAlgebra 📕 Probability: https://imp.i384100.net/Probability OTHER RELATED COURSES (7 day free trial) 📕 ⭐ Deep Learning Specialization: https://imp.i384100.net/Deep-Learning 📕 Python for Everybody: https://imp.i384100.net/python 📕 MLOps Course: https://imp.i384100.net/MLOps 📕 Natural Language Processing (NLP): https://imp.i384100.net/NLP 📕 Machine Learning in Production: https://imp.i384100.net/MLProduction 📕 Data Science Specialization: https://imp.i384100.net/DataScience 📕 Tensorflow: https://imp.i384100.net/Tensorflow
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from CodeEmporium · CodeEmporium · 0 of 60

← Previous Next →
1 Linear Regression and Multiple Regression
Linear Regression and Multiple Regression
CodeEmporium
2 Logistic Regression - THE MATH YOU SHOULD KNOW!
Logistic Regression - THE MATH YOU SHOULD KNOW!
CodeEmporium
3 Generative Adversarial Networks - FUTURISTIC & FUN AI !
Generative Adversarial Networks - FUTURISTIC & FUN AI !
CodeEmporium
4 Deep Learning on the Cloud - GPU TO LEARN FASTER
Deep Learning on the Cloud - GPU TO LEARN FASTER
CodeEmporium
5 Deep Mind's AlphaGo Zero - EXPLAINED
Deep Mind's AlphaGo Zero - EXPLAINED
CodeEmporium
6 Mask Region based Convolution Neural Networks - EXPLAINED!
Mask Region based Convolution Neural Networks - EXPLAINED!
CodeEmporium
7 Attention in Neural Networks
Attention in Neural Networks
CodeEmporium
8 Depthwise Separable Convolution - A FASTER CONVOLUTION!
Depthwise Separable Convolution - A FASTER CONVOLUTION!
CodeEmporium
9 One Neural network learns EVERYTHING ?!
One Neural network learns EVERYTHING ?!
CodeEmporium
10 Neural Voice Cloning
Neural Voice Cloning
CodeEmporium
11 AI creates Image Classifiers…by DRAWING?
AI creates Image Classifiers…by DRAWING?
CodeEmporium
12 Unpaired Image-Image Translation using CycleGANs
Unpaired Image-Image Translation using CycleGANs
CodeEmporium
13 K-Means Clustering - EXPLAINED!
K-Means Clustering - EXPLAINED!
CodeEmporium
14 Random Forest Classification
Random Forest Classification
CodeEmporium
15 Data Science in Finance
Data Science in Finance
CodeEmporium
16 Hypothesis testing with Applications in Data Science
Hypothesis testing with Applications in Data Science
CodeEmporium
17 A/B Testing - Simply Explained
A/B Testing - Simply Explained
CodeEmporium
18 The Kernel Trick - THE MATH YOU SHOULD KNOW!
The Kernel Trick - THE MATH YOU SHOULD KNOW!
CodeEmporium
19 Support Vector Machines - THE MATH YOU  SHOULD KNOW
Support Vector Machines - THE MATH YOU SHOULD KNOW
CodeEmporium
20 Principal Component Analysis (PCA) - THE MATH YOU SHOULD KNOW!
Principal Component Analysis (PCA) - THE MATH YOU SHOULD KNOW!
CodeEmporium
21 History of Calculus - Animated
History of Calculus - Animated
CodeEmporium
22 Curiosity in AI
Curiosity in AI
CodeEmporium
23 DropBlock - A BETTER DROPOUT for Neural Networks
DropBlock - A BETTER DROPOUT for Neural Networks
CodeEmporium
24 Autoencoders - EXPLAINED
Autoencoders - EXPLAINED
CodeEmporium
25 Recurrent Neural Networks - EXPLAINED!
Recurrent Neural Networks - EXPLAINED!
CodeEmporium
26 LSTM Networks - EXPLAINED!
LSTM Networks - EXPLAINED!
CodeEmporium
27 Building an Image Captioner with Neural Networks
Building an Image Captioner with Neural Networks
CodeEmporium
28 10 Machine Learning Questions - ANSWERED!
10 Machine Learning Questions - ANSWERED!
CodeEmporium
29 How do neural networks work?
How do neural networks work?
CodeEmporium
30 Evolution of Face Generation |  Evolution of GANs
Evolution of Face Generation | Evolution of GANs
CodeEmporium
31 How does Google Translate's AI work?
How does Google Translate's AI work?
CodeEmporium
32 How to keep up with AI research?
How to keep up with AI research?
CodeEmporium
33 How does YouTube recommend videos? - AI EXPLAINED!
How does YouTube recommend videos? - AI EXPLAINED!
CodeEmporium
34 Variational Autoencoders - EXPLAINED!
Variational Autoencoders - EXPLAINED!
CodeEmporium
35 Logistic Regression - VISUALIZED!
Logistic Regression - VISUALIZED!
CodeEmporium
36 Gradient Descent - THE MATH YOU SHOULD KNOW
Gradient Descent - THE MATH YOU SHOULD KNOW
CodeEmporium
37 Boosting - EXPLAINED!
Boosting - EXPLAINED!
CodeEmporium
38 Transformer Neural Networks - EXPLAINED! (Attention is all you need)
Transformer Neural Networks - EXPLAINED! (Attention is all you need)
CodeEmporium
39 Loss Functions - EXPLAINED!
Loss Functions - EXPLAINED!
CodeEmporium
40 Optimizers - EXPLAINED!
Optimizers - EXPLAINED!
CodeEmporium
41 NLP with Neural Networks & Transformers
NLP with Neural Networks & Transformers
CodeEmporium
42 Batch Normalization - EXPLAINED!
Batch Normalization - EXPLAINED!
CodeEmporium
43 Activation Functions - EXPLAINED!
Activation Functions - EXPLAINED!
CodeEmporium
44 Data Scientist Answers Interview Questions
Data Scientist Answers Interview Questions
CodeEmporium
45 Why use GPU with Neural Networks?
Why use GPU with Neural Networks?
CodeEmporium
46 How do GPUs speed up Neural Network training?
How do GPUs speed up Neural Network training?
CodeEmporium
47 BERT Neural Network - EXPLAINED!
BERT Neural Network - EXPLAINED!
CodeEmporium
48 ConvNets Scaled Efficiently
ConvNets Scaled Efficiently
CodeEmporium
49 Transformer Neural Net makes music! (JukeboxAI)
Transformer Neural Net makes music! (JukeboxAI)
CodeEmporium
50 What do filters of Convolution Neural Network learn?
What do filters of Convolution Neural Network learn?
CodeEmporium
51 We're hosting a Machine Learning Conference!
We're hosting a Machine Learning Conference!
CodeEmporium
52 MLconfEU 2020: Machine Learning Conference for Software Engineers
MLconfEU 2020: Machine Learning Conference for Software Engineers
CodeEmporium
53 Are Neural Networks Intelligent?
Are Neural Networks Intelligent?
CodeEmporium
54 Time Series Forecasting with Machine Learning
Time Series Forecasting with Machine Learning
CodeEmporium
55 Few Shot Learning - EXPLAINED!
Few Shot Learning - EXPLAINED!
CodeEmporium
56 How does a Data Scientist Fight FRAUD?
How does a Data Scientist Fight FRAUD?
CodeEmporium
57 How would a Data Scientist analyze Customer Churn?
How would a Data Scientist analyze Customer Churn?
CodeEmporium
58 Expectations with Machine Learning
Expectations with Machine Learning
CodeEmporium
59 Why Logistic Regression DOESN'T return probabilities?!
Why Logistic Regression DOESN'T return probabilities?!
CodeEmporium
60 How you SHOULD code Machine Learning
How you SHOULD code Machine Learning
CodeEmporium

This video explains cross validation techniques for evaluating machine learning models, providing resources for further learning, including simulations and textbooks. Viewers will learn how to implement cross validation to improve model performance and reduce overfitting. By the end of this video, viewers will understand the importance of cross validation in machine learning and be able to apply it to their own projects.

Key Takeaways
  1. Understand the concept of cross validation
  2. Learn about k-fold cross validation
  3. Learn about leave one out cross validation
  4. Implement cross validation in a machine learning project
  5. Evaluate model performance using cross validation
💡 Cross validation is a crucial step in evaluating machine learning models, as it helps to reduce overfitting and improve model performance by simulating real-world scenarios.

Related Reads

Up next
Marks Weightage | Quantitative Aptitude CA Foundation September 2026 | ABC Analysis | Nithin
ArivuPro Academy
Watch →