1. Home
  2. Explore
  3. Computer Science
  4. Neural Networks Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Neural Networks Overview

Transcript

Okay it's about that time, let's go ahead and get  started. So in the past many lectures now, not   even just the last lecture, we've been building  up a lot of neural nets. So first, I think around   lecture six, we introduced neural nets: we'd built  on linear regression and logistic regression,   we built up to neural nets where we transformed  the features and then we developed convolutional neural nets not too long after that as a way to  deal with vision style problems. Then we developed   recurrent neural nets not too long after that as  a way to deal with things with an inherent, maybe,

time structure or recursion structure, something  that you might see in language, for instance,   where you're building up words and so I think  at this point, you could really reasonably think   based on the structure of the course that the  whole course is building up to neural nets and   neural nets are like the epitome of machine  learning and that's all you should ever use   for every machine learning problem and part of  that sentence is correct. So there's a very real sense in which it's natural to build neural  nets from things like logistic regression, they add extra elements like learning features.  It's natural to build convolutional neural nets   and recurrent neural nets from when we've learned  about neural nets and these are certainly super   important methods, they've really revolutionized  a lot of applications especially in things like   vision. We might use a lot of these ideas in maybe  natural language processing but what's incorrect   about my previous statement is that you should  use these on every machine learning problem.

It's more realistic to think that what  we're doing is we're building a toolbox   and these are part of that toolbox.  They're very useful and an important part   but there are other aspects of that toolbox. So  just because we did linear regression and logistic   regression first doesn't mean that only silly  people use them. Absolutely people use them all   the time to this day and to great effect and we're  going to see today some more elements, some more machine learning tools that should probably be  in your toolbox as you go forward. In particular,   if you care about interpretability, decision trees  are a really important part of your toolbox and if   you care purely about predictive performance, then  ensembles and random forests are really important   parts of your toolbox, so we'll spend some  time talking about each of these methods today.

Okay so first let me just spend a little  time hopefully convincing you that predictive   performance isn't everything, that there are  things that we really do care about beyond pure   predictive performance or perhaps we just need to  rethink what we mean by predictive performance.   But an example of this I think is very timely  it's election audits. So if you looked in the   news very recently, but here's actually an article  from back in October even before the election, election audits are a very important item  that people are talking about. 46 states,   at least according to this article in October,  have some auditing regime in place to just double   check if there is some kind of user error or  some kind of technical glitch or whatever,   but this isn't a new idea of auditing  elections. So here's a paper from 2008   that's been very influential actually. The  article, the news article on the left, was

talking to Philip Stark who wrote this article in  2008 and has been doing a lot of work in election   auditing. And I'll also mention Ron Rivest in  our department, he’s at MIT, and has also been   doing a lot of work in election auditing.  It's a pretty cool area and gets into some   math. But here's a quote that I really want to  excerpt from this 2008 article which is “the   choices that this researcher made, he made them  to simplify the exposition and implementation; his methods needed to be transparent to be  adopted as part of the election process and to   inspire public confidence. He thought about using  methods that are more familiar to data analysts   and they might be more efficient (in fact, they  probably are), but because of their complexity,   they'd likely meet resistance from elections  officials and voting rights groups.” And so   I think this is a nice explanation of why we  sometimes need methods that maybe don't reach

the absolute peak of something like predictive  performance or some other notion of goodness. Well   it might be that we just need to explain them to  people and I think it's not just that everybody's   being silly and we should all accept neural nets  as our new overlords. There are plenty of reasons   that we should want these good explanations. I  mean one is that there are reasons to be skeptical of methods you don't understand. So lots of folks  right now seem to say they have machine learning   solutions and maybe they're selling snake oil  sometimes and so if you're not a machine learning   expert being able to distinguish between those  items might require some kind of interpretability.   But it's not just about whether you have expertise  or not. Even if you're a machine learning expert,

you probably actually care about interpretability.  So here, here's an example you can read in this   paper of a case where machine learning experts  ran neural nets and also more interpretable models   on this problem where they were interested  in what is the risk of death from pneumonia   from patients. So this was in a medical context  and the interpretable model had a very clear step:   if the patient… Once they got all of their methods  out, they have the neural net and it's like “okay, here I have a neural net. What do I do  with this?” But the interpretable model   very clearly and precisely stated “hey, if  somebody is a patient with asthma and pneumonia at   the same time, then they have lower risk of death  from pneumonia.” And so that's the thing that,   even if you don't have a lot of medical expertise  but certainly if you do have medical expertise,   you might think that seems pretty silly that  somebody who has asthma would probably be at

higher risk for pneumonia. And because they have  this interpretable model, they could say “hey,   something is going on here, that's a little bit  weird, let's dig into it more” and, in fact, it   turned out the reason that was this was happening  was that the patients with asthma and pneumonia   were being admitted to the ICU because they were  deemed to be such high risk. They got aggressive   care that decreased their risk of dying and so  this is an example of something that you've seen earlier in the semester: Simpson’s paradox. So you  might want to check when that thing is going on   and now you are aware already from the semester  of Simpson's paradox, but now you have an ability,   maybe if you have an interpretable model, to see  that that might be happening. Another nice thing   about interpretable models is that there's less  room for human error. So if you look at Malcolm

Gladwell's “Blink” book, there's an example of Dr.  Brendan Reilly, the chairman of the Department of   Medicine at Cook County Hospital in Chicago. He  was facing resource shortfalls (they have lots   of poor patients that they're treating) and  they needed a way to diagnose heart attacks   in patients that were presenting with chest pains,  who came in and had chest pains. It was quick   and accurate and so they end up using exactly the  method we're about to talk about, a decision tree, but here you could say they care about predictive  performance, like they care about accurately   diagnosing each person who comes in, but the  problem is that the machine learning method isn't   exactly the method that they're using. They're  using a person using the machine learning method   and so that's not the kind of thing that you're  testing when you're testing cross validation,   when you're looking at test set error. That's  actually an extra element of the system

beyond the pure method and so that's a reason,  at least on the face of it, if you can't do   more that you might care about something  that's very simple to use so humans don't   mess it up when they're using it. Okay, so a lot  of reasons to care about interpretability, about   things beyond predictive performance. It doesn't  even just have to be interpretability although   that's what I've been focusing on here. But even  if you only care about predictive performance, you should care about today's lecture, you should care  about trees, because trees are part of the things   that have the best predictive performance in  many cases. So if you look at this Kaggle (Kaggle   is a subsidiary of Google LLC but it also runs  machine learning competitions among other things)   and there was this magazine that recently in March  did an interview with a Kaggle master ranked 19th

in the global Kaggle competitions leaderboard and  what's interesting is they say what's his toolkit.   And if you look at these algorithms in his  toolkit, lightgbm, xgboost, catboost, these are   all ensembles of trees. And so while neural nets  can be really useful again for things like vision   and NLP and so on, the reality is for a lot of the  data that you're going to encounter, ensembles of   trees are basically the thing that really does  the best in terms of predictive performance. Okay so at this point, hopefully, you're  motivated to hear about what is a tree   and what is an ensemble of trees and  so that's what we're gonna do today.

Okay, so let's start with what is the decision  tree. Well decision tree is basically just a   presentation of a series of decisions in  a tree. So let's let's start with that   and then we'll nail down exactly what it is.  So let's say that I have someone coming in, maybe they came in with a heart attack. And we  know after the first 24 hours whether they've had   a heart attack, they've been in the hospital over  24 hours, and now we want to assess their risk in   order to decide on their follow-up treatment.  And so here is a tree that people actually in   the early days of decision trees presented, which  is the following series of decisions. So first we   asked, let's look at a particular type of blood  pressure, systolic blood pressure over the last

24 hours, and asked: did it really dip down very  low? Was it always greater than or equal to 91?   So we have two choices: either yes or no. So  if no, if it was, if it did dip down below 91,   then this patient is considered high risk. If  yes, then we're going to ask another question.   The question is: is their patient age greater than  or equal to 65? And a patient who is a bit older   might be riskier, so if this  patient has an age less than 65, then we might just consider them low risk at  this point, they're probably going to be okay.

But if they're greater than 65, we have  yet another question. We're going to ask:   is sinus tachycardia present? For the purposes  of this lecture, let's just say that this is   a medical thing that you can measure and you can  check and you can check if it's present. It either   is or it isn't. If it is not, then this patient is  low risk. If it is, this patient is high risk. And   so here is just a series of decisions, a series of  yes or no questions that, even if you don't have medical expertise, if you're provided with the  appropriate data, you can just go through these   and answer them. Okay so this is a nice little  flowchart, but it's maybe not clear in this form   how it relates to everything we've talked about in  the class. So let's turn this into something that   we can actually talk about in the words and the  phrasing and the terminology of this class. Okay,

so let's imagine in real life we have a bunch of  data. You can imagine each individual who walks in   and is around for 24 hours after a heart attack  is a data point and we have various features   associated with those individuals like the date  that they were admitted, their age, their height,   their weight, was sinus tachycardia present , and  the minimum systolic blood pressure over 24 hours, and maybe their latest diastolic blood pressure,  and maybe even there were some other features. Okay. So actually, we can see  each of these internal nodes   in this tree as being some kind of cut or split  on one of these features. So in particular,   minimum systolic blood pressure over 24 hours,  that's actually our sixth feature in this case.   So we can write this as x_6 >= 91. Is this true?  We'll go to the right-hand child if it's false,   we'll go to the left-hand child. Okay, we can do  the same thing with age. Age is our second feature

so we can actually rewrite this as x_2 >= 65.  If it's true, then we'll go to the right hand   child. If it's false we'll go to the left-hand  child. Is sinus tachycardia present? Well this   depends on how we encode this. A typical  way that we might encode, as we've seen,   a binary variable, binary classes is either  0 or 1 or -1 or 1. Either case, 1 is probably   going to be the yes and so we'll ask: is x_5 >=  1? And so here, now, we have our decision tree

in terms of our features. Now typically, what we  do here is we do some kind of classification or   regression. Hopefully it's clear that this looks  like classification, that we're trying to classify   into two classes: high or low risk and so we  might give them labels. A typical thing we've   done in this class is give them labels like 0  or 1 or -1 or 1 and so here we're doing that:   we're having 1 be high risk and -1 be low risk  and so here we are, so we can change this into our labels. And so at this point, basically,  we have created a classifier or, in general,   a predictor. And so let's just double check that  that's true, that in fact this is a classifier.   Oh and before we do that, let's finish up  saying what exactly is the decision tree,   what counts as a decision tree? Well it's going to  be a binary tree with internal nodes and leaves.   The internal nodes, each one of them, is going  to be defined by a dimension index j and a

split value s. So for instance, if I look at this  node, so this is one of the internal nodes (I've   color coded the internal nodes here as being white  and the leaves as being green), so this is an   internal node. It has a dimension index that we're  splitting on, that's j. In this case, j is 6.   It has a split value s that we're splitting on. In  this case, that split value is 91. And then it has two child notes and each one of them can either be  internal or leaf nodes. And then the leaf nodes, so this one's a leaf node, this one's an internal  node. Now the leaf nodes are defined just by   their label. So we get to a leaf node, that's  the end. It's not going to have any children   but it does have some kind of label and that's  what we're going to predict at that label.

Okay so as we said, this is a classification  tree. This is doing classification as its form of   decision. And now, we can say that we have defined  a classifier just like we've done in the past   for logistic regression, for neural nets, for  any of the things that we've been looking at. And let's just check that that's true. So here,  let's suppose we have a data point that comes in.   So a person comes into the hospital. That person  has a bunch of features associated with them,   again: the date, the age, the height,  the weight, is sinus tachycardia present,   systolic blood pressure, latest diastolic blood  pressure. And now we're going to ask ourselves:   how would we classify this data point? So in order  to check “what does this predictor look like” on our particular data point, we're basically going  to run through the decision tree. So first,   we're going to ask: is x6, is the sixth feature  for this data point, greater than or equal to 91.

So here's the sixth feature. In this case,  it's 115, so it's greater than or equal to 91,   so we say yes. So now we go to the next decision: is x_2 >= 65? Is the person older than 65?  And we check this person's age, it's 49,   they're not older than 65, so we go to no.  And now we're to a leaf and so as soon as we   get to a leaf, that is our prediction. In this  case, it's -1: we say this person is low risk.

Okay so this is a classification tree.  We can use it to make classifications.   You'll notice that it does not have  to use absolutely every feature.   We're going to see later that, actually, features  can repeat too, we can have things like that,   but it is a binary tree of decisions, a finite  binary tree of decisions, that eventually gives   us some kind of classifier. And of course, we  can do the same thing for regression, it doesn't have to just be classification, so let's take  a look at a regression tree. So here, instead   of a medical example, let's have an example  that we actually visited way back when we were   talking about neural nets. So here our example  could be: when am I going to run? Where and how   much am I going to go for a run? And maybe my two  features that I use to decide this are temperature   and precipitation. So how cold is it and is it  raining, how much is it precipitating out there?

And my label could be kilometers run. Okay so  what are we gonna do here?Sso we're gonna again   start with the decision, say in this case, x_2 >=  0.3? Basically, is there a lot of precipitation?   In which case, we'll go to yes. Or is there not so  much precipitation? In which case we'll go to no. Okay well if there's a lot of precipitation,  maybe it turns out that I just go inside and I   go on the treadmill and I don't want to run as  much and so I run, let's say three kilometers.

If there's not so much precipitation, I'm gonna  ask: is it cool? So we'll have x_1 >= -5 and maybe   I say if it's cold, so if there's a no here, then  again I'm gonna go on the treadmill. I'm not gonna   run maybe as long, maybe it'll be two kilometers.  If it's yes, maybe I'll ask is it warm, is it   way too warm? Is x_1 >= 33? So if it's very warm  or if, first, if it's in intermediate temperature, so it's not too cold and it's not too hot, maybe  I'll go outside and I'll run and I'll run a good   five kilometers. But if it's very warm, I don't  feel like doing anything and so maybe I'll just   run 0. So in this case, what's different between a  regression and a classification tree, we're seeing   that in the regression, all the decisions and the  intermediate part are totally the same as before,   the only difference is the labels are real  valued now. So this is like the difference

between regression and classification, in general,  that you have a predictor that's real valued   instead of a predictor that's just one class  or the other or multiple classes is also fine. Okay, so something to note here and  this is not specific to regression   or classification, this is generally true  for these decision trees, that the tree   itself defines an axis-aligned partition of  the feature space. So let's dig into what all   of those words mean by first seeing  what happens when we use this tree.

Okay so first, when we get to the root node, first  we have all of the feature space. So in this case,   that's x_1 > 0. Sorry, x_1 is any value because  we can have any value in degrees Celsius. I mean   technically speaking there's a lower limit to that  but basically all the allowable degrees Celsius.   Then x_2 has to be greater than zero because  it's precipitation. Can't usually have   negative precipitation. And so we have some  whole feature space that's here in orange.

Okay so now we're going to look at the first  split here. So the first split is saying:   let's look at x_2 >= 0.3. And so what we're  doing with this feature space is we're saying,   “hey, we're going to split it into two  parts with a line.” So this is x_2 = 0.3,   the line. There's a part where x_2 is  greater >= 0.3, in which case we go to the right child and there's a part where x_2  < 0.3, in which case we go to the left child.

Okay, now in this case, when we go to the  left child, there's actually another decision,   another split. And so when we're splitting, you  can really think of the split as being applied   to this feature space. We're actually making a  split in this feature space. So in particular   here, now what we're going to do is we're  going to take only the space where x_2 < 0.3 and we're going to split it with x_1  = -5. So you'll notice this line only   applies when x_2 < 0.3. It's like we've already  restricted ourselves to the x_2 < 0.3 space.   And on one side we have x_1 < -5 and  on the other side we have x_1 >= -5.

Okay so now, again, we say: where are we in the  space right now, moving down this regression tree? And then I'm going to finish this up and then I'll  do the question. So then we have our x_1 >= 33   split. So again, we're going to split up our space  so we have x_1 = 33 on one side, we'll have x_1 <   33 but it also conforms to all of the other  constraints we've imposed so far. So this is   the set right here where x_2 < 0.3, x_1 > -5 and  x_1 < 33 and on this side, we have the same two   first set of constraints and then x_1 > 33. Okay  so the question is: how could regression trees

generate labels with a continuous number when we  have a binary split node? And so what's happening   is that the binary split node is just partitioning  the space as we're seeing here but the continuous   value that you actually say is your regressor,  that you actually say is my prediction at a   particular point, is the value in the leaf node  and so here I've got kind of lazy and I put those   values as 3, 2, 5 and 0. If I had been slightly  less lazy, I could make them more obviously real valued, like maybe sometimes I run 3.2  kilometers and sometimes I run 5.1 kilometers,   but the idea of regression is just that you are  predicting a continuous value and so as long as   you are allowing a continuous value in your leaf  nodes, you are doing regression. Another way to   think about that is what's happening in regression  is that, for every value in your feature space,   you are predicting a continuous value and so what  we're actually seeing here in this little plot

that I put in the lower right hand corner, is our  feature space and if you think of this as being a   three-dimensional plot where the y label is coming  out of the slides at you, then this would actually   be, you could have that y label and then you would  have your regression values which, in the case   of orange is 3, in the case of red is 2, in the  case of blue is 5, and in the case of purple is 0 and so if we could imagine rotating that and  then you would actually see this step function   that we're predicting and so the difference with  classification is just that you're only allowing,

in the case of two value classification, those  two values, but if you were doing three value of   classification, multi-class classification,  you wouldn't be putting that on a y-axis,   you would be saying it's like a, b and c is what  you're predicting and you wouldn't just plot those   on the same thing because as we saw before having  this is back in lecture three with our features,   we saw that having multi-classes isn't  the same as having an ordering like 1, 2, 3 because there's no ordering on those  classes and see that's where I think you   can really see that there's a difference in what  you do with the classification and regression.

Now that being said, I think this observation  about the binary split node is key because it   tells you that we're really doing a simple type of  regression: we're not getting a really different prediction from one point to the next,  we're really doing a step function as our   regression. Now I said in the beginning  of this slide or earlier in the slide,   I would say, what is an axis aligned  partition. So let me just say that now.   So a partition is when you have a bunch of  mutually exclusive and exhaustive sets. That is to   say the sets are all separate, they don't overlap,  and when you take their union, they all become the whole space. And so that's what we're seeing here  is that we've taken up the space of x_1 and x_2,   our feature space, and we divided it up.  That's all you're doing, the partition:   you're dividing it up into little subspaces and  so we have basically four here is what we've done   and each one of those we're going to predict  something different. Now two things that are   worth noting about this one, this is why we end  up getting this step function because we're just,

in this case, predicting a constant value. Now  you don't have to do that in regression trees,   you could do something else at the nodes, but  here we're just going to do a constant value. The other thing to notice is that these are axis  aligned by our choice of splits. Our splits are   we take one dimension and we split it a particular  value and so we're always going to get these lines   as our dividers that are parallel to one of  our axes like x_1 or x_2 in this case. Again,   that's something that you could try to go beyond,  but that one's a lot harder to go beyond in a   reasonable way that actually gives you useful  predictions but it is something that you can do.

Great okay. So now, we've talked about two  different types of decision trees: we've talked   about classification trees, we've talked about  regression trees and, in a way, you can think of   what we've done so far as describing the forward  mode of these. We talked, when we were talking   about neural nets, about the forward run of the  model and then going backwards through it and so   here what we're doing is we're saying, “hey, if  I have a decision tree and I have a data point, I can tell you the prediction, I know how to  do that and we've talked through that now.”

Okay and that's step one of our familiar pattern.  So at this point, hopefully this is becoming so   old as to be boring (you totally know the familiar  pattern and you're really familiar with it),   so step one: we have to choose how to predict a  label. So if we're given a set of features and a   predictor, which is defined by parameters, and  we'll say in a moment what is a natural way to   think about parameters here, but given features  and parameters, we know how to predict a label. So we just saw that with decision trees: we looked  through an example and the medical example of   “suppose I have a data point, what'll I do  to predict whether this person is high risk   or low risk?” We could do the same thing here:  if I have a day that is 20 degrees Celsius and   there's zero precipitation, I can go to the top.  I say there's zero precipitation, so I go to no.   It's 20 degrees celsius so I'll go all the way  down to the bottom and I'll say I'm gonna run

five kilometers. Okay so we know how to predict  a label given a set of features and parameters.   The next thing that we do is we choose a loss, a  loss between our guess and our actual label. So   we'll just do that in a moment of course.  That will depend on whether we're doing   regression or classification, that will  depend on what type of thing we're doing   with regression and classification, but  it's something that we'll choose and then finally we somehow choose parameters  by trying to minimize the training loss. Okay so what are the parameters here? Well the  parameters are typically the thing that describes what is our predictor and so, in this  case, we have a bunch of unknowns.   We want to say, for each internal  node, what is the split dimension.

We want to say for each internal node what is  the split value. So again, for x_2 >= 0.3, the   split dimension is 2, the split value is 0.3, so  those together will define what's going on at that   internal node, what am I going to do, how am I  going to make my decision. But I also need to say:   what are the two children notes? Is there one leaf  and one internal node? Is there one internal node and one leaf in the other direction? Are there two  leafs? Are there two internal nodes? These are all   possibilities and we need to say which it is and  then for each leaf node we have to say the label.   So what we're gonna do in a moment is we're going  to come up with a waym once we've chosen a loss,   to choose all of these parameters by  trying to minimize the training loss.

Now something that's worth noting here that's a  bit different from things we've done in the past,   hopefully immediately different if you think  back to linear regression or logistic regression,   but actually really different from  most of the things we've done,   is that the parameters here don't have a fixed  dimension. So as soon as I make an internal node,   if that has two children that are internal nodes,  now I suddenly have a bunch more parameters. And if they have internal nodes as children, now  I have a bunch more parameters. In fact, you can   get more and more parameters this way so that's  a bit different than what we've done in the past.

Okay so let's suppose that I'm going to do  regression, I'm going to choose to do regression and I'm going to choose squared error loss because  that's a really natural thing that I might do   for regression. Well hey, squared error loss,  we've talked before about how if I were doing something with squared error loss, I might  try to set up everything in my problem so   that's all differentiable and run gradient  descent or stochastic gradient descent.   And that's just not going to happen  here. So a couple of things to note.   One, we certainly haven't talked about how you  would do gradient descent or stochastic gradient   descent if you don't have a fixed dimension  parameter. That sounds like it might be hard.

Two, this is definitely a super not  differentiable structure. So if I'm   trying to decide whether to add an internal  node or not, that's at least, on the face of it,   not differentiable, that's just a very discreet  decision and so it's really not clear that I   could apply something like gradient descent or  stochastic gradient descent which really relies on   there being differentiability in all of my  parameters, that my loss, my overall objective is differentiable in all my parameters. And so we're  going to have to think about what could we do. Well, we could go back to our roots. Way  back at the very beginning of this class,   we used a heuristic, the perceptron. We don't  always have to use gradient descent and stochastic   gradient descent, I mean even when they're  available, we don't always have to use them.   And so in this case, we're  going to look at a heuristic.   It's going to have two parts: we're going to  build up a tree, we're going to think of it

as growing the tree, and then we're going to prune  it back, so take away parts of the tree. This is a   particular way, it's a very classical way to build  the decision tree. I kind of want to emphasize   that just like many things we've talked about in  this class, this is not the only way to make one,   there are definitely other ways you can make a  decision tree and I think, in some ways, what's   really important to understand is the simplicity  of the ultimate decision tree that you get is very useful, it's very understandable,  it's the kind of thing that you could present   to somebody who's an expert in another area who  isn't an expert in machine learning. You could   have an extremely smart medical practitioner and  that doesn't mean that they know about neural   nets so this is something you can easily talk  about with them. If you're serving on a jury   and you're participating in your civic duty on a  jury, a tree is something that you could explain

to your fellow jury members. And so that  simplicity is separate from what we choose   to learn it and now we're going to talk about  here's an algorithm that you might use to learn it   but again not the only thing you could do. Okay so  again we're going to talk about these two steps:   building it up and putting it back. Okay so let's  first start by talking about building a decision   tree and as just a motivating example, let's  imagine that we have some toy data over here and it is running times based on the,  again, temperature and precipitation. So x_1   is the temperature. It's very cold, maybe  I'm not running. When it's very warm, I'm   not running. And then, x_2 is the precipitation.  When it's precipitating a lot, I'm not running,   but when you get that nice temperature  and there's not a lot of precipitation,   then I'm running and maybe it's something like  five kilometers. And we're gonna think of this

as a regression problem. Again, if I put a little  more time into this, these could be numbers like,   realistically, I might run like 5.2 kilometers  or 4.9 kilometers. So you can think of these as   being values that really do vary continuously  and we're trying to learn a regression tree. Okay so if we're trying to learn a regression,  again a really natural loss is squared error   loss. So let's just go ahead with squared  error loss for the moment. It depends on   what you're trying to do in general and it's  not always the right choice for regression but   for here it's certainly convenient and we might  just go ahead with it. So let's suppose we're   building a regression tree with squared error  loss. Okay so we're going to build our tree

and it's going to take two inputs: one, it's  going to take a collection of data indices.   What do I mean by that? Well the first thing that  we're always going to do when we run BuildTree   is we're going to run it on our training  data. And so here “I”, the collection of   indices that we put in, will be the indices of our  training data: training data point 1, 2 up to n,   assuming we have n training data points. Now why  would we bother making that an input to BuildTree? Well the problem is we're going to be doing it  recursively later with just a subset of the data   and so that's why we want to make that an input.  We'll see that that's important as we go along.

Okay next we have k. So k is going to  be a hyper parameter of this algorithm.   It's going to be the minimum number of  points that we allow in a tree branch   or in a leaf. So basically, as soon as we get  down to two points, we're gonna stop, we're   gonna say we're done. We're not gonna go down to  allowing a leaf with just, say, one point in it. According to the algorithm we're about to build  up, I think it's actually the maximum number   that we're gonna allow. I take that back yeah.  Okay the point is we're not going beyond two.   Okay so our first question: because we're  not going beyond two points, we're not   letting ourselves have more than two points  in a branch, we're going to ask ourselves,   “okay, is the number of data points that we're  looking at right now greater than or equal to 2?”

Okay, wellm let's start asking that. This is  a real question for the chat. I'm looking at   the number of data points when I run this.  I'm gonna run exactly BuildTree 1 to n on   the dataset that I have right here. So this is a  dataset that I'm illustrating with the 0s and 5s.   I'm gonna run this BuildTree. What is  the number of data indices? So that   absolute value “I” means the number of things  in the set. So I'm asking, for the chat, what is the absolute value of “I” here? What  are the number of things in the set of indices?

Okay I'm getting great answers from both  directions. So some people are answering   in full generality, which is great. In that  case, the answer is n: there are clearly n   things in this index set. And if you did answer in  full generality, I'll ask you to also think about   what is the number n for this particular  dataset that we're illustrating here? Great, so just to recap: in this dataset, just  to be really clear about how I'm labeling things,   so I'm at a particular x_1 and x_2 point.  If there's a point, I'll put a number   and that number is the regression number. So it's  like, oh, at this data point I ran it's the label:   I ran zero kilometers at this data point, I  ran five kilometers and so the answer which   many of you are getting correctly is 11 because  there are 11 data points that I'm plotting here.

And so I'm going to check, in this case, is  11 <= 2 and it is not and so I would skip   this if statement and the first time I run  this BuildTree for this dataset but let's ask   what would happen if I did happen to have  actually only 2 points and we'll fill it in. Okay so if I only had two points, if I were  making myself a leaf, well what happens at a   leaf? At a leaf, we just make a label. We just say  what's the label that I'm going to have? What's   the predictor that I'm going to return for this  little area? It'll turn out to be like that little   partition element that we saw before, this  little block of space in our feature set.   Okay so we're going to have to set y hat somehow.  How are we going to set y hat, where y hat is our

label, it is what we predict in this little  area? Well remember, our goal here—I mean this   is going to be a heuristic method—but our goal is  to minimize the loss and, in particular, the loss   or the error, the thing that we want to minimize  here, is going to be the sum of the losses over   the data points in this little set. So we have a  sum over data indices in the tiny little set that we're looking at right now. I mean by construction  it's tiny because it had fewer data points than k and then we're going to have  the loss for that data point.   And so something that you should do is either  immediately think that you have already solved   this problem earlier in the course or,  even better, solve it again right now   and by right now I mean if you don't immediately  think of the solution, do this later on your own   time. But you should convince yourself that the y  hat that minimizes this loss is the average of the

y's of the training data here. So this is a great  exercise if maybe you're getting a little rusty on   this from earlier in the course, but make sure you  convince yourself that I'm trying to minimize the   squared error loss on this subset of data, then  I'm going to take the average of that subset and   that'll be if I have to do a constant prediction  that that'll be the best thing I can do. Okay so now, I know what I'm going  to use for my label in this leaf:   it's going to be the average of the  data points that fall into this leaf.

Okay so now let's think about what  happens if I didn't get down to,   say, two data points or if I  didn't get down to, in general,   k data points which is certainly what happens  the first time I start building this tree. Okay in that case, I'm gonna have to make some  kind of split, that's what happens in our internal   nodes. I've decided I'm not making a leap so  I'm gonna have to make an internal node and   that internal node does a split and so that split,  remember, has a dimension and it has a value and   so, for instance, the dimension in this case, in  this toy example we have here, could either be 1   or it could be 2, there's the only dimensions  that I have. I'm basically go through all my feature dimensions and consider each one of them  in turn. Okay well let's suppose that I choose x_1

and I choose a split value that's maybe slightly  negative. This is what my split would look like.   Or maybe I could choose a different split  value or maybe I could choose one over here.   Any of these would be totally fine. Okay  so here's an issue: if I have a for loop   over every data dimension, that's fine because  there are usually d data dimensions, that's a   finite number, we're all good. If I have a for  loop over every possible split value and these are continuous dimensions, I have an uncountable  for loop and that's bad, that's not something   that I could do on my computer and so that seems  like a problem that we're going to have to resolve   and basically I'm just going to come back to in  a moment but hopefully you can think to yourself   why is this bad. We couldn't have a for loop over  an uncountable infinity or any infinity really.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent