English (United States) subtitles

← Gaussian NB Example - Intro to Machine Learning

Get Embed Code
6 Languages

Showing Revision 1 created 09/17/2016 by Udacity Robot.

  1. Okay, so now what I've done is I've gone to the Gaussian Naive Bayes
  2. documentation page.
  3. sklearn.naive_bayes.GaussianNB.
  4. This was that algorithm that I set out to find and
  5. now that I've, now I've found the sklearn documentation page.
  6. So the first thing that I see right here, actually this is one of the things I
  7. love about the SK Learn documentation, is it's full of examples.
  8. When I was actually developing the code for this class, this was one of
  9. the first things that I would always do is I would come find the example code
  10. and I would try to just run in my Python interpreter,
  11. see if I could get it working.
  12. And almost invariably it works right out of the box.
  13. So here's something that's just very simple.
  14. There's only a few lines here that are really important.
  15. So let me point them out to you and
  16. then I'll show you the code I've actually written for the example we just saw,
  17. and you'll start to recognize some of these lines.
  18. But first let's introduce them.
  19. So the first one that's really important is this one right here.
  20. Above this it's just creating some, some training points that we can use,
  21. it's not that important.
  22. This is where the real meat starts, is with this import statement and if you've
  23. programmed in Python before, you're well acquainted with import statements.
  24. This is the way that you bring in external modules into the code that you're
  25. writing so that you don't have to completely re-implement everything every time,
  26. you can use code that someone else has already written.
  27. So we say from sklearn.naive_bayes going to import GaussianNB.
  28. Very good.
  29. The next thing that we're going to do is we're going to use that to
  30. create a classifier.
  31. So classifier equals GaussianNB.
  32. If you miss your import statement.
  33. If you forget this line for
  34. some reason, then this line is going to throw an error.
  35. So if you end up seeing some kind of error that says that it doesn't recognize
  36. this function.
  37. It's probably a problem with your import statement.
  38. So, okay, we've created our classifier.
  39. So now the code is all sort of ready to go.
  40. The next thing that we need to do is we need to fit it.
  41. And we've been using the word train interchangeably with fit.
  42. So this is where we actually give it the training data,
  43. and it learns the patterns.
  44. So we have the classifier that we just created.
  45. We're calling the fit function on it, and then the two arguments that we pass to
  46. fit are x, which in this case are the features and y which are the labels.
  47. This is always going to be true in supervised classification.
  48. Is that it's going to call this fit function and
  49. then it's going to have the features.
  50. And then the labels.
  51. And then the last thing that we do is we ask the classifier that
  52. we've just trained for some predictions.
  53. So we give it a new point.
  54. In this case the point is negative 0.8, negative 1.
  55. And we ask for this what do you think the label is for this particular point?
  56. What's the, what class does it belong to?
  57. So in this particular case it says it belongs to class number one.
  58. Or you could imagine for some other point it might say class number two.
  59. So of course you have to have already fit the classifier before you
  60. can call predict on it.
  61. Because when it's fitting the data that's where it's
  62. actually learning the patterns.
  63. Then here is where it's using those patterns to make a prediction.
  64. So, that's it.
  65. That's kind of, now you know most all there is to
  66. know to get this working in the first example that I've done.