Return to Video

Gaussian NB Example - Intro to Machine Learning

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

more » « less
Video Language:
English
Team:
Udacity
Project:
ud120 - Intro to Machine Learning
Duration:
03:03

English subtitles

Revisions Compare revisions