Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

Overfitting And Underfitting Machine Learning | Machine Learning Tutorial For Beginners |Simplilearn

Simplilearn16:10 46.178 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 114 Zeilen
Herunterladen
  1. hello everyone welcome to this video tutorial by simply learn in this video we will learn about overfitting and underfitting in machine learning we have
  2. our experience instructor richard who will take us through this video and help us understand the basics of overfitting and underfitting the reasons why it
  3. occurs and finally we look at a demo in python over to richard now what is overfitting
  4. what is underfitting and those are like the biggest things right now in data science is overfitting and under fitting what does that mean
  5. so let's go ahead and talk about overfitting when we talk about overfitting it's a scenario where the machine learning
  6. model tries to learn from the details along with the noise and the data tries to fit each data point on the curve you can see that
  7. um if you plug in your coordinates you're just going to get the whatever it's fitted every point on the data stream there's no average there's no
  8. two points that might have the you know y might have two different answers because if the wind blows a certain way um in
  9. the efficiency of your car maybe you have a headwind so your car might alter how efficient it is as it goes and so there's going to be this variance on
  10. here and this says no you can't have any variance with you know the this is it's going to be exactly this it can't be any you can't be the same speed or the same
  11. car and have a slightly different efficiency so as the model has very less flexibility it fails to predict new
  12. data points and thus the model rejects every new data point during the prediction so you'll get like a really high error
  13. on here and so reasons for overfitting data used for training is not cleaned
  14. and contains noise garbage values in it you can spend so much time cleaning your data and it's so important it's so important that if you have if you have
  15. some kind of something wrong with the data coming in it needs to be addressed whether it's a source of the data maybe they use in
  16. medical different measuring tools uh so you now have to adjust for data that came in from hospital a versus hospital b or even off of machine a and
  17. machine b that's testing something and those those numbers are coming in wrong the model has a high variance again wind is a good example i was
  18. talking about that with the car you may have a hundred tests but because the wind's blowing it's all over the place
  19. size of training data used is not enough so a small amount of data is going to also cause this problem you only have a few points and you try to plot
  20. everything the model is too complex this comes up a lot we put too many pieces together and how
  21. they interact can't even be tracked and so you have to go back back break it up and find out actually what correlates and what doesn't
  22. so what is underfitting a scenario where machine learning models can either learn
  23. the relationship between the data points nor predict or classify a new data point and you can see here we have our efficiency of our
  24. car and our line drawn and it's just going to be way off for both the training and the predicting data as the model doesn't fully learn the
  25. patterns it accepts every new data point during the prediction so instead of looking for a general pattern we just kind of accept
  26. everything data used for training is not cleaned and contains noise garbage and values again under fitting and overfitting same
  27. issue you've got to clean your data the model has a high bias we've seen this in all kinds of things from
  28. [Music] the mod the most common is the driving cars to facial identification or whatever it is the model itself when
  29. they build it might have a bias towards one thing and this would be an underfitted model would have that bias because it's averaged it out so if you
  30. have five people from india and 10 people from africa and 20 people from the u.s you
  31. created a bias because it's looking at the 20 people and you only have a small amount of data to work with
  32. size of training data used is not enough that goes with the size i was just talking about so we have a model with a high bias we
  33. have size of training data used it's not enough the model is too simple again this is one straight line through all the data when it needs a slight
  34. shift to it for other reasons so what is a good fit uh a linear curve that best fits the data is neither overfitting or under
  35. fitting models but is just right and of course we have the nice examples here where we have overfitting lines going up and down every point is trying
  36. to be include gluted underfitting the line really is off from where the data is and then a good fit is got to
  37. get rid of that minimize that error coming through so this is all exciting but what does this look like so we really need to jump
  38. in and put a code together and see what this looks like when we're programming for this demo we'll bring up our trusty anaconda and go into jupiter notebook
  39. for python move myself out of the way here uh and so we're going to start off this
  40. is going to be a demo on overfitting and underfitting using python and let's start with our imports now if you've been through enough of
  41. these tutorials we don't want to spend a huge amount of time on what we're bringing in and what we're doing so you should be up on doing this with
  42. python and how to bring in your different modules we're going to bring in the sklearn or the scikit processing sk
  43. learn dot neural network import in a mlp regressor so there's our regressor model right there
  44. that's going to be our linear regression model and we have our metrics mean absolute error if you remember we had our that's
  45. how we figure out how well it fits is how far off that error is based on the um mean square error value mse
  46. and then of course numpy because we just like to work with numpy it's a great data array we always import it as mp that's the most common way of doing it
  47. and then we have sklearn model selection import validation curve so we're going to look at a validation curve to see how good our models are
  48. and then we have the data set we'll use the very famous iris data set and that's embedded in the scikit so the s key
  49. learn data sets have a load iris in there and then we have the matplot library because if you're doing any kind of demo
  50. or showing this off to your shareholders we want to have something nice to display it on and then we have sklearn model selection
  51. we're going to import import k-fold and we'll talk about that when we get to it and then we're going to go ahead and do just for our numpy we're going to do
  52. like a random seed for random numbers and then for our plot style we'll use the gg plot that's just some back end setup you could even probably leave the
  53. plot style out depending on what version of you're using of depending on what version you're using a
  54. map plot library and then we'll go ahead and run this it's not going to do anything that we can visibly see because it's just
  55. loading those modules and then we also want to load our iris data in here and the iris data has an iris data and iris target
  56. we're going to load that as x and y and just so you can have an idea what we're talking about we're going to go ahead and print
  57. x and just the first bit of x we'll just do the top of x and we'll also print y so you can see
  58. what the top of y looks like print y and since we're a numpy we're going to go ahead and do our own thing if this
  59. was of course pandas we could just do the head of it and see what it looks like and you can see here we've loaded this
  60. up and in x we have these different measurements that they take of the flower the iris flowers from this particular data set
  61. and what kind of flower it is it's going to be a zero one or a two is actually what the target comes out of even though that doesn't show in here
  62. and so we're going to come in here and we're going to use the k folds cross validation with 20 folds and a good catch that this was a model
  63. selection we're we're going through and we're selecting different parts of the data in here here
  64. we use k-fold cross validation with 20 folds k equals 20 to evaluate the generalization efficiency of the model within each fold we will then estimate
  65. the training and test error using the training and test sets respectfully so here we have our kf equals kf k fold here's our splits on the top
  66. and then we need to go ahead and have our list training error we're going to create an array for that we're going to list our testing error
  67. and for train index and test index in k f dot split x x train y uh x train and x test we're going to go ahead and split up our
  68. our data our x values and the same thing with the y values so now we have an x train and x test a y train and a y test and then here's our model our impel
  69. mlp regressor and that's your linear regression model in there and we have used a multi-layer perceptron mlp
  70. so this is a neural network multi-layer perceptron that's what the mlp is for regressor means that it is dealing with
  71. numbers we're not categorizing things um and then let's go ahead uh i kind of went off the screen here we'll just go
  72. ahead and bring that down it's a class of feed forward artificial neural networks and they kind of loosely call it a and n
  73. don't get caught up in the a n n n and n m n n there's nnn is neural network and then everybody
  74. puts their own flavor on it depending on what they're doing uh so if you see the nnn you know you're dealing with a neural network
  75. so we go ahead and fit our data here's our model dot fit we have x train and y train and the y train data we're going to
  76. predict equals model dot predict x train so here's our prediction of what it's going to be so we've trained it and we've predicted it we've trained our the
  77. train data and then we have our y train and then we have our y test and the y test equals a model predict x test
  78. now notice what we did here is we're going to use our model to predict what we think y should be but this is the training set
  79. uh so we've trained it with this data and now we want to see how good our model fits our training data and then we want to see how well it fits
  80. our testing data so we take our fold training error mean absolute error y train y train data predict and we're gonna do
  81. our full testing error the mean absolute error of y test and y test data predict and we do this and here's our mean
  82. absolute error there's our a little bit different connotation but that's that's taking the square value and finding the in this case it's using
  83. the absolute value so instead of the square value we get rid of the minus and pluses by using an absolute value and we find the average of that and that works
  84. the same way as doing the squared value and then we take our list training error and we're going to just append it for each
  85. each one of these runs we go through so every time we fold the data think of it like this we want to go ahead and take a piece of
  86. data that's going to be one piece of the data and we're going to look at each section and we want to go through each section to see how well it does and
  87. splits it up this way we have a nice picture uh when we're looking at it from a distance i do this a lot when i do x and tr when
  88. i split my x train and my y train i'll take two thirds of the data and then one third of the data and then i'll switch it and i'll do three different models so
  89. i can really see how well it tests out and how that averages out this is the same thing but with the with the k fold we're doing and we're
  90. doing it across 20 sections we'll go ahead and run this and we run this it's not too exciting because we're just loading up the data
  91. and appending it into our list and so we want to take with this is we're going to go ahead and plot it
  92. and this is where we can really see what's going on this is where it gets exciting uh so we take it we're going to create a couple subplots uh that way we
  93. have a nice setup down here we're splitting it up into a couple different graphs
  94. and let's go ahead and run this and then we'll walk through it a little bit so our subplot comes in
  95. there's our subplot and then our plt plot we're going to do in there range one we're going ahead and do the splits plus one npra list training error uh
  96. ravel this is of course just a code to how we properly set it up on there so that it sees it correctly
  97. and then we have our x label which is our number fold uh plot the y label training error plot the title training area across folds
  98. plot the tight layout plot the subplot so we're going to move on to this is one two one one two two there's our one it's just one and two
  99. uh it has to do with how it how it layers it on there for doing multiple plots because you can do all kinds of cool things with our plot our pi plot
  100. library uh and again we're going to go ahead and do the same thing for the error and we end up with our training error across
  101. folds and our testing area across folds and so you can see these different folds how they kind of spike and how they look and so we're talking about overfitting
  102. or under fitting we're comparing these two graphs and if one of them is more off than the other one
  103. if you're looking at these two graphs you're going to say hey is this one overfit or underfit and this is always a good question to ask i mean what do we
  104. got going here is that over fit or is that underfit and i would say based on these two graphs and the
  105. training data uh the training data is more sporadic than the testing data so i would look at this and say hey
  106. this might need to be fit a little bit better maybe we don't have enough data with the iris we probably don't
  107. something else is going on here so it's a little underfit maybe a different model would fit better i would not use a neural network model for this i would
  108. actually use just a basic linear linear model on this a lot of different choices but this gives you an idea what we're
  109. looking at is how chaotic are these two is it getting better or is it getting worse if at some point the training data gets
  110. so much better than the testing data you know you've over fit it and that's where you start running into the overfitting this to me looks like it's under fit
  111. so that concludes underfitting and overfitting thank you for watching this video i hope it was useful and informative if you
  112. liked this video then please go ahead and subscribe to simply learn channel to stay updated with all the latest trending technologies stay tuned for
  113. more from simply learn [Music] hi there if you like this video
  114. subscribe to the simply learn youtube channel and click here to watch similar videos turn it up and get certified click here

Zum Nachlesen