Introduction to Random Forest | Intuition behind the Algorithm — Transcript
Full transcript
- 0:00Hello guys, welcome to my YouTube
- 0:02channel. In this video, we're going to
- 0:04start with a machine learning algorithm
- 0:06that you'll use in every project. It's
- 0:09so powerful and so flexible that any
- 0:11project you're doing in the machine
- 0:13learning domain will definitely use
- 0:14this algorithm. Right? This algorithm's
- 0:18name is Random Forest. I hope you've
- 0:20heard of it before. Right? Now, in this
- 0:22video and the next two or three videos,
- 0:25we'll learn all about Random Forest in
- 0:27detail. So let's start the video. So,
- 0:31the best thing about Random Forest is
- 0:33that it's an algorithm that
- 0:34consistently delivers excellent results
- 0:36on every type of machine learning
- 0:38problem. Right? This, along with
- 0:43Gradient Boosting and XG Boost, is one
- 0:46of two algorithms that performs well on
- 0:48every machine learning problem. Right?
- 0:52That's the first good part about Random
- 0:54Forest. The second good thing is that
- 0:57random forests can be applied to both
- 0:59regression and classification problems.
- 1:05The third, which is especially great
- 1:07for beginners, is that the computer
- 1:09scientist who created random forests (I
- 1:11don't know their name), but he wrote in
- 1:14his paper that random forests are an
- 1:16algorithm that doesn't require much
- 1:17tuning out of the box. Meaning, even if
- 1:24you don't tinker with its settings too
- 1:26much, its performance remains excellent
- 1:28. In fact, you'll notice in the future
- 1:33that if you're using random forests in
- 1:36any project, along with other
- 1:37algorithms, without touching any
- 1:39parameters, random forests always give
- 1:41you the best results most of the time.
- 1:46Okay? Now let's talk about what you
- 1:48need to know to study random forests.
- 1:51So, random forests are an equivalent
- 1:53technique. Okay? So, to study this, you
- 1:56should first understand decision trees.
- 1:58If you don't have an idea of a decision
- 2:00tree, you won't understand anything
- 2:02about random forests because a random
- 2:04forest is basically a collection of
- 2:06trees. A collection of trees is why
- 2:08it's called a forest. Okay? Second,
- 2:10it's actually a bagging technique. The
- 2:14algorithm we just covered was bagging
- 2:16operations. Okay? Now, I've covered
- 2:19bagging operations in great detail. But
- 2:22if you don't know that, you might have
- 2:24a little trouble with this video. I'll
- 2:30try to teach you from scratch, but it's
- 2:32still recommended that you watch the
- 2:33bagging video first. Okay? So, let's
- 2:38start with random forests. So, the
- 2:42first thing you should know clearly
- 2:44about random forests is why it's called
- 2:46random forests. Okay? Let's talk about
- 2:50why it's called random forests. So,
- 2:53there are two words here: random and
- 2:56forest. You can easily justify the word
- 2:58forest. Because here you train multiple
- 3:02decision trees together. That's why we
- 3:06call it a forest. So, it's basically a
- 3:08group of trees. And forest is a logical
- 3:12name for a group of trees. Second,
- 3:15where does the word "random" come from?
- 3:19The word "random" comes from the fact
- 3:21that random forest is a bagging-based
- 3:23technique. It's a bagging technique.
- 3:27And I told you in the bagging video
- 3:29that the full form of bagging is "boot
- 3:31strap." Aggregation. Right. Now, this "
- 3:43boot strap" comes from the fact that
- 3:45you're sampling here. What is sampling?
- 3:50Randomly selecting data. Right? So,
- 3:53this randomly selected section of data
- 3:55is where the word "random" comes from.
- 3:58Right? So, I hope you've first
- 4:00understood why it's called "random
- 4:02forest." Right? Now, it's a bagging
- 4:05technique. Essentially, it's a bagging
- 4:08technique. I taught you about the
- 4:10bagging technique, that you can use any
- 4:12base algorithm. Right? You can use KN.
- 4:15You can use SBM. You can use any base
- 4:18algorithm. But if you use a decision
- 4:23tree as the base algorithm in bagging,
- 4:25you call it a random forest. Right? So,
- 4:29the most important point is that this
- 4:31is the bagging technique. But the
- 4:34bagging technique has a specialization.
- 4:36And what is that specialization? If
- 4:39you're using all the models in the
- 4:41bagging technique as decision trees,
- 4:42then it becomes a random forest. There
- 4:46are some other minor differences, but
- 4:47we'll learn about them a little later.
- 4:50Okay? Now, for those who don't
- 4:53understand bagging or haven't seen a
- 4:55video on bagging, let me explain how it
- 4:57all works. Let's assume you don't
- 5:00understand bagging. So, I'll explain
- 5:02from scratch how a random forest works.
- 5:04Okay? So, it's very simple. Look, let's
- 5:08say you have a data set with 1,000 days
- 5:11. Okay? 1,000 days. Any number of
- 5:15columns. Let's say you have a data set.
- 5:18Okay? Now, what do you do here? You
- 5:20work in two steps. So, what do you do
- 5:22in the first step? You pick a bunch of
- 5:25base models. And all of these base
- 5:28models are decision trees. It's always
- 5:30a random forest, so a decision tree
- 5:32will be the base model. Okay? So, this
- 5:34is decision tree one. This is decision
- 5:38tree two. This is decision tree three.
- 5:40Dot dot dot, let's say we have 100
- 5:43decision trees. So the last one is
- 5:45decision tree 100. Okay? Now, what you
- 5:52do is you give this data to each
- 5:54decision tree to train, but you don't
- 5:56give it all the data; what you do is
- 5:58you create samples out of your data.
- 6:04Okay? So, you do two ways of sampling.
- 6:06The first is sampling. Actually, you do
- 6:09sampling in three ways. The first is
- 6:11row sampling. Let me give you an
- 6:14example of row sampling. Let's say you
- 6:18decided to give 500 rows to each base
- 6:20model and each decision tree. How many
- 6:23rows did you have in total? 1000. But
- 6:26you decided to give only 500 rows to
- 6:28each base model. Right? And how will
- 6:32those 500 rows be decided? Because our
- 6:34data has different rows. Like 1, 2, 3,
- 6:384, 5, and so on, there are 1000 rows.
- 6:43But only 500 of these rows will go into
- 6:45this first decision tree. Which ones?
- 6:48We don't decide this. They are selected
- 6:50randomly. Right? So, randomly, we
- 6:53selected one, we selected 18, we
- 6:56selected 32, we selected 43, we
- 6:59selected 103. So on and on. In this way
- 7:04, we selected 500 different rows. We
- 7:09selected 500 different rows. And you
- 7:13can call this row, that is, this data
- 7:15form you created, D1. You give this D1
- 7:19to the first decision tree to train.
- 7:22Okay? Now, the selection that's
- 7:25happening here also has two types. One
- 7:29is with sampling and without sampling.
- 7:44So, I guess you know, sorry, I'm sorry,
- 7:46I completely mispronounced "with
- 7:48replacement" and "without replacement."
- 7:52Replacement is the word. I'm sorry,
- 7:55with replacement and without
- 7:57replacement. So, let me give you an
- 8:01example of "with replacement." Suppose
- 8:05you want to give your first decision
- 8:07tree data of 500 rows and you're doing
- 8:09row sampling with replacement. Row
- 8:12sampling with replacement. So, suppose
- 8:14the first row you randomly selected was
- 8:17one. Now you have one in your new data
- 8:20set. But you returned this one row.
- 8:23This means it's available for selection
- 8:25again. When you selected it a second
- 8:28time, it returned 42. Then, when you
- 8:31selected it a third time, it returned
- 8:331. Why? Because you're using with
- 8:35replacement. You're picking one row and
- 8:37putting it back in. So, it could be
- 8:39selected again. This means that the
- 8:43data set you get here has a chance of
- 8:45having duplicate rows. If you're
- 8:49talking about using with replacement,
- 8:51but if you're talking about using
- 8:52without replacement, then there won't
- 8:54be any duplicate rows. Okay? So, that's
- 8:58what I was telling you: what you do? In
- 9:00the case of random forests, you take
- 9:02multiple base models, which are
- 9:04decision trees. Then, you feed data to
- 9:07each decision tree. That data is
- 9:10actually a subset of your actual data
- 9:12set. Okay? Now, there are three ways to
- 9:15generate those subsets. The first is
- 9:17row sampling. The second is column
- 9:19sampling. What you do in column
- 9:21sampling is you do the same thing on
- 9:23columns. So, suppose you have 10
- 9:26columns, and for the first decision
- 9:29tree, you decided that each decision
- 9:31tree would have only five columns. Each
- 9:34decision tree would have only five
- 9:36columns. So, when you're deciding for
- 9:38the first decision tree, you'll
- 9:40randomly choose five columns from the
- 9:4210. Okay? Then, for the second decision
- 9:46tree, you'll randomly choose five
- 9:48columns. And you'll do this for every
- 9:50decision tree. So, this sampling is
- 9:52done on columns. That's why it's called
- 9:55feature sampling. Row sampling is done
- 9:57on rows, feature sampling is done on
- 9:59features, edge sampling is done on
- 10:01columns. And there are two types of it:
- 10:03one with replacement and the other
- 10:05without replacement. With replacement,
- 10:07there may be duplicates. Without
- 10:09replacement, there won't be duplicates.
- 10:11Okay? And the third type, I guess you
- 10:14can guess, is a combination of these.
- 10:17So, you're sampling based on both.
- 10:20You're extracting subsets of the days,
- 10:22and subsets of the columns. Okay? So,
- 10:25what are you doing in short? Let me
- 10:27summarize it all. When you're training
- 10:30your model, what is your model made of?
- 10:32Multiple smaller models. And those
- 10:34smaller models are decision trees. Okay
- 10:37? So, you give each decision tree a
- 10:39subset of data. How that subset of data
- 10:41is created depends on three things:
- 10:43either you do row sampling, or you do
- 10:45column sampling, or you do combined
- 10:47sampling. Okay? And then you're giving
- 10:49each one its own respective data. Here
- 10:52you're giving D2. Here you're giving D3
- 10:54. Here you're giving D00. And you train
- 10:57all these decision trees. Now, since
- 10:59each decision tree is getting different
- 11:02data, each decision tree will see
- 11:03different data. For example, this
- 11:06decision tree might look like this. It
- 11:11might look like this. This decision
- 11:13tree might look like this. This
- 11:18decision tree might look like this.
- 11:25Right? Since you're feeding different
- 11:27data to each decision tree, each
- 11:29decision tree will train differently.
- 11:31Okay? What do you do next? Now that all
- 11:34your decision trees have been trained,
- 11:36you come to the second step. The first
- 11:38step was boot-stepping, which we just
- 11:40saw. This is bootstaping, which we just
- 11:42did. The second is aggregation. What
- 11:46you do in aggregation is that when you
- 11:49need to make a prediction, you use the
- 11:51aggregation step. So, you have all your
- 11:53decision trees trained. You have 100
- 11:56decision trees trained. And let's say
- 11:58this is a classification problem. Now
- 12:01you have a new query point whose class
- 12:03you need to determine. So you give that
- 12:06decision point to each decision tree.
- 12:09Given to every decision tree. Now,
- 12:12since every decision tree can make a
- 12:14prediction, it predicted whether it
- 12:16should be one or zero. Like this. Okay?
- 12:22Right? Now, in the aggregation step,
- 12:25what you do is you ask how many
- 12:27decision trees said zero? Let's say 43
- 12:31trees out of 100 said its label is zero
- 12:34, and 157 said it. Now, since 57 is
- 12:38greater than 43, most decision trees
- 12:40are saying that the label of this
- 12:42incoming query point should be one. Now
- 12:45you say its label will be one. And that
- 12:48is what aggregation is. Aggregation
- 12:50means the most frequent or most common
- 12:53thing, you assume, like democracy. Okay
- 12:56? If this were a regression problem,
- 12:58what would you do? You would again give
- 13:01the query point to everyone. Okay? What
- 13:04would happen this time? Instead of zero
- 13:06, one would come back. A number would
- 13:09come back. What would you do with that
- 13:12number? Calculate the mean, and you'd
- 13:15get your regression output. Okay? So,
- 13:18in a nutshell, that's what a random
- 13:20forest is. What I just taught you is
- 13:22the same thing I taught you in the
- 13:24previous video when I taught bagging.
- 13:27The only difference here is that the
- 13:29base models are decision trees. And
- 13:32there's a very important difference,
- 13:34which I'll cover a little later when I
- 13:36teach you about bagging versus random
- 13:38forest. But let's summarize at this
- 13:41point. Since we've covered a lot, let's
- 13:44summarize that random forest is a
- 13:46bagging-based technique. Bagging means
- 13:50boot-step aggregation. Bootstepping
- 13:53means you have multiple models. You
- 13:56give each model a subset of data. Now,
- 13:59how is that subset of data created? It
- 14:01depends on three things. First, are you
- 14:03using row sampling? Second, are you
- 14:06using column sampling? Third, are you
- 14:08using a combined approach? Okay? And
- 14:11then you train all the models on a
- 14:13subset of the data and then use the
- 14:15aggregation step to make predictions.
- 14:18So that's the whole thing. Random
- 14:20Forest is exactly similar to bagging.
- 14:23Nothing different. There's a small
- 14:25difference that we'll cover later.
- 14:28Before that, I want to show you a
- 14:30little demo of the benefits of using
- 14:32Random Forest. Okay? So, we'll see that
- 14:35next. So, guys, to explain Random
- 14:37Forest to you, I've created a
- 14:39step-by-step demo. Now, this isn't a
- 14:42perfect demo. Meaning, not everything
- 14:46I've shown here is exactly what happens
- 14:48inside a Random Forest. It's a loose
- 14:51demo to explain the concept. But
- 14:54everything is not true. I'll explain
- 14:56what's wrong. But this whole thing will
- 14:59give you a basic understanding of
- 15:00Random Forest. Okay? So, let's start
- 15:03with the demo. First, I've generated a
- 15:06data set. Okay? Using the make
- 15:09classification function. And this is
- 15:11the data set. This data set has five
- 15:14input columns and one output column.
- 15:16And this is a classification problem.
- 15:18Okay? Now, here I've created three
- 15:21different functions. One function is
- 15:24called sample rows. If you give it a
- 15:28data frame and tell it a percentage, it
- 15:30will randomly sample from it and give
- 15:31it to you. So, if you have a data frame
- 15:38with 100 rows and you call this
- 15:40function and say, "Give me 50%of the
- 15:42data randomly." What will this function
- 15:46do? It will randomly generate 50%of the
- 15:48rows from that data frame and give it
- 15:51to you. Okay? Then I created a similar
- 15:53function called "sample features,"
- 15:55which will do the same thing, but on
- 15:57columns. And then I created another
- 16:00function called "combined sampling,"
- 16:02which will do the same thing on both.
- 16:06So, you can extract rows, meaning
- 16:08subsets of rows and columns. Let me
- 16:12first show you using sample rows. Okay?
- 16:15So what am I going to do? I explained
- 16:17earlier that a random forest is nothing
- 16:19but a collection of decision trees. So,
- 16:22the random forest we're going to create
- 16:24will consist of three decision trees.
- 16:26So, I need to feed all three of them
- 16:28data. The data I have now is this,
- 16:30which has 100 rows and a total of six
- 16:32columns. Right? What am I going to do
- 16:34now? I'm going to create three separate
- 16:36data sets. How am I going to create all
- 16:38three? Using row sampling. Okay? Now,
- 16:41to do the row sampling, I'm going to
- 16:43call the sampleRows function. Here, I'm
- 16:47going to send my original data frame
- 16:49and specify a percentage of the
- 16:51original data that the new data I want
- 16:53should contain. So, I'm saying I only
- 16:58want 10%of the data. So, if my original
- 17:01data set had 100 rows, then the new
- 17:04data set I want will only have 10 rows.
- 17:07Okay? So as I run this, you can see
- 17:11that here, these 10 rows have been
- 17:13randomly selected. Okay? Now, this is
- 17:17with replacement. Meaning, there could
- 17:19be duplicates here. I don't see any
- 17:22duplicates here, but there could be.
- 17:24Meaning, it's not necessary that there
- 17:26won't be any. For example, look here,
- 17:2952 and 52 appear twice. In fact, three
- 17:32times. Okay? There could be other
- 17:34duplicates here as well. So, I've done
- 17:37row sampling with replacement. Okay? So
- 17:40, for now, I'm just doing point one. So
- 17:44this is df1. This is the first data set
- 17:48that I'll give to my first decision
- 17:50tree. I've run it like this. Okay? Then
- 17:57I'm calling the same function again,
- 17:59giving my original data set and sending
- 18:01point1 again, and you can see that this
- 18:03time a different data set has come in.
- 18:08Okay? I'm calling this df2. df2, okay?
- 18:14And similarly, I'll generate one more
- 18:16data set for the third decision tree.
- 18:20I'll call it df3. Everything else is
- 18:22the same. Okay? So far, there's no
- 18:26problem. Now, if you extract the shape
- 18:32of any of these: df1: Dot Shape 10 6
- 18:35df2: Dot Shape 10 6 df3: Shape 10 6 The
- 18:39shape will be the same. Okay? So what
- 18:46did I do? I called the decision tree
- 18:48classifier and created three objects.
- 18:51So far, it's exactly the same. Now what
- 18:55do I do? I need to train the model. So,
- 18:57what do I do to train the model? I'll
- 18:59give all the rows of df1. And where do
- 19:02I put the columns? I'll give the first
- 19:04five columns. So, I'll give them from
- 19:06zero to four. 4 will be the last column
- 19:12. And as soon as I run this, all three
- 19:16decision trees are trained. Then I call
- 19:19the plot tree function. And here is my
- 19:23first decision tree. This is my first
- 19:26decision tree. This is my second
- 19:28decision tree. And this is my third
- 19:31decision tree. And if you look, you'll
- 19:36start to see the differences. For
- 19:38example, there's a zero and a five here
- 19:40. Meaning, five samples came in the
- 19:43left node, and five samples came in the
- 19:46right node. Where as in this one, there
- 19:49are seven samples in the left node, and
- 19:51three samples in this one. Okay? And if
- 19:54we go to the top, there are seven and
- 19:56three here too. In fact, let's do one
- 19:58thing. Let's rerun the entire code
- 20:00again. Let's hope the tree becomes a
- 20:04little different. So I'll rerun the
- 20:08entire code and see what difference it
- 20:13makes. Because every time the selection
- 20:15is random, the decision tree will also
- 20:16be different. This time a combination
- 20:19of 64 is coming. This time a
- 20:23combination of 55 is coming. And look,
- 20:27this is a completely different decision
- 20:29tree. In fact, let's do one thing. For
- 20:32a little more visible change, what we
- 20:34can do is rather than having 10%a day.
- 20:37Let's have 20%a day. 20%20%and I'm
- 20:47training everything again. Here's my
- 20:53first decision tree. 13 plus a total of
- 20:5820. 8 plus a total of 20. And 1 plus 8
- 21:10plus 11. Okay? So we have three
- 21:13different decision trees. Okay? Now
- 21:15tomorrow a new point comes. Let's say a
- 21:18new point comes. I'll pick a point from
- 21:20here. Let's say this point comes. This
- 21:24is this point. Okay? Now its target,
- 21:26its prediction, should be zero. So what
- 21:34I'm going to do is I'm going to pass
- 21:36this inside the np.dot array and I'm
- 21:38going to put commas everywhere like
- 21:40this. Okay? And I'm going to reshape it
- 21:44to 1 5 and I'm going to put it in the
- 21:46CLF one. So I'm asking the first
- 21:49decision tree, "What should the output
- 21:51of this point be?" And the number of
- 21:54features of one matches the input. Why
- 21:59is the number of features four? We just
- 22:13wrote one second. So what did we do
- 22:19wrong? We wrote 0 to 4 here. So 0, 1, 2
- 22:22, 3, so the train is in four columns.
- 22:25There should be five here. My bat, we
- 22:27were wrong from the beginning. There
- 22:30should be five here. So I'll run it
- 22:32again. And here's my first decision
- 22:45tree. It's getting a little deeper now.
- 22:49Here's my second decision tree. It's a
- 22:52little different. And here's my third
- 22:54decision tree. It's also quite
- 22:56different. Okay? And I'm making it 1,
- 22:595. So, according to the first, it
- 23:02should be zero. According to the second
- 23:07, I'll copy this entire code and I'll
- 23:09paste it over here. And I'll run it. It
- 23:16should be zero. And let's see what the
- 23:19third one should be. It should also be
- 23:23one according to the third one. So
- 23:27since two decision trees are returning
- 23:29zero and one decision tree is returning
- 23:31one, then going by majority count, you
- 23:33would say the output prediction for
- 23:36this query point should be one, I'm
- 23:38sorry, zero. Okay? So that's how row
- 23:41sampling works. Similarly, you can do
- 23:43column sampling as well. Let me show
- 23:46you quickly. What am I doing this time?
- 23:49I'm going to call it "Rather Than
- 23:51Sample Row." I'm going to call "
- 23:54SampleFeatures" and I'm going to pass
- 23:56it "df." There are five input columns
- 23:58in df. And what am I doing? I'm doing
- 24:03it by point. 70%, so total five total.
- 24:12Okay, okay, okay total. There's a
- 24:17problem. The problem with my code is
- 24:21that the percentage I've calculated
- 24:23should be of df, dot shape -1; because
- 24:25you don't count the target column.
- 24:31That's why. Once I run this code again,
- 24:36it's gone. Or, so now I'm saying, let's
- 24:40say give me 50%columns, give me 50%
- 24:42columns, so I have five columns in the
- 24:45total input. You can see that there are
- 24:48five columns in the total input. So 50%
- 24:50is 2.5, and if you round it to an
- 24:52integer, it becomes two. This means
- 24:56that if I run this code, I'll get a new
- 24:58data frame with how many rows? As many
- 25:01as 100 in the original data frame. But
- 25:03how many columns? Only two. Let's see
- 25:06if that works. And you can see 100 rows
- 25:09with two columns. Okay? If I add 8 here
- 25:14, then 8 of five is four. So now after
- 25:25doing this, the data frame we get will
- 25:27have 100 rows but four columns. Let's
- 25:31see. Look, these four columns were
- 25:34randomly generated. Okay? Every time I
- 25:37run this, different things will appear.
- 25:39Look, column two, column one, column
- 25:42five, column four, c columns 1, 2, 5,
- 25:473. Okay, and now what you can do with
- 25:50this again is put it in df1 (). This is
- 25:54df1. What I'll do next is copy and
- 25:57paste the same code. So, copy and paste
- 26:02, this is my df2, and this is my df3.
- 26:07So now I have three different data sets
- 26:09. Let's repeat the whole thing by
- 26:11sampling the columns. Okay, one more
- 26:17thing, we can't put a five here because
- 26:20the shape of the data set we have now
- 26:22is a total of four columns. Only four
- 26:27columns are coming. So we'll enter zero
- 26:30to three here. We'll give three. So 0,
- 26:341, 2. Copy and paste. Paste. Model
- 26:42trained. Unknown. Label. Continuous. Oh
- 26:47, okay, there's a slight logical issue
- 26:49here. The problem is that the target
- 26:53column isn't there. What we're
- 26:56returning doesn't even include the
- 26:58target column. We need to return the
- 27:00target column as well. So, I need to
- 27:02make a few changes to this code. Ah,
- 27:04I'll do it, and then I'll explain it to
- 27:06you. So, guys, I made the changes. Just
- 27:09a few changes. You can see this code if
- 27:12you want. Now you'll always get a
- 27:14target column back. Let me show you. So
- 27:18, if I run this code again, this
- 27:20warning is coming up because you're
- 27:22making changes to the data frame. And
- 27:27now, look, we only have four columns in
- 27:29the input, but we're getting a target
- 27:31column as well. Okay? So, what do we do
- 27:36now? The next code is very simple.
- 27:38We'll write here: zero to four. zero to
- 27:42four. zero to four. These are our
- 27:46decision trees trained. Here is our
- 27:48first decision tree. Since there are
- 27:50more rows here, it will be a very deep
- 27:52decision tree. Look, here is our second
- 27:54decision tree. It's a little different
- 27:57from the one above. Yes, it's quite
- 28:00different, and the third one will be
- 28:02different as well. Because you're
- 28:03giving different data. You're giving
- 28:05different columns. Okay? If you want to
- 28:09check, you can check who got which
- 28:10column. For example, df3 has these
- 28:14columns. I'll write "infact, df3 dot
- 28:19columns." I'm printing this out. Copy
- 28:31and paste to one, and you can see that
- 28:40all three data sets are different
- 28:42because they all have different columns
- 28:44. Okay? And that's why the decision
- 28:49tree trained is also very different.
- 28:51And now what will we do? We'll take a
- 28:54new query point again. For example,
- 28:56what I'll do is I'll take df3. This is
- 29:05DF3 and grab this query point or these
- 29:11query points. And the output should be
- 29:15one. So now I'll go to the first
- 29:18decision tree. This is the first
- 29:20decision tree. I'll pass the columns.
- 29:28And this will become 1/4. Look, it
- 29:33should be one. The first one is saying
- 29:35it should be one. Let's see what the
- 29:37second one is saying. So again, we're
- 29:39giving the same input to the second
- 29:40person. He's saying zero. Let's see
- 29:49what the third person is saying. The
- 29:55third person is also saying one. Since
- 29:57two people are saying one and one
- 29:59person is saying zero, the majority
- 30:01count would give you one, which is
- 30:02correct. Okay? So that's how it works.
- 30:05So first, I showed you row sampling.
- 30:07Then I showed you column sampling.
- 30:09There's another function here. Let me
- 30:11run that and show you. And this
- 30:13function is called combined sampling.
- 30:15Okay? So what do I do? I'll show you
- 30:18combined sampling. In combined sampling
- 30:20, you have to provide three elements of
- 30:22your data frame. Then, what percentage
- 30:24of rows do you want? I want five. So,
- 30:27I'll get 50 rows. 0.5 means 50 rows.
- 30:31Since the total number of rows was 100,
- 30:33it would be 50%. And I want the number
- 30:37of columns. 25, or five. Let's take
- 30:44both fives. So five and five columns
- 30:47will become two. Okay? So the resulting
- 30:50data frame I get will have 50 rows and
- 30:53two columns. Let's see if that happens.
- 30:59So you can see. If I run this and
- 31:05calculate its shape, it's coming out to
- 31:07be 50 50 3, of course, because the
- 31:09target column is different. The input
- 31:13will have two columns. The output will
- 31:14have one column. So the total is 50 3.
- 31:17So what do I do now? I'll rewrite it as
- 31:19df1. I'll copy this code. Paste it.
- 31:31This will be df2 and over here. Also,
- 31:37I'll write df3. So now I have three
- 31:41data sets. The rows are different, the
- 31:43columns are different, and there's more
- 31:45randomness. Okay? And let me show you
- 31:48the columns again. Each one has
- 31:50different columns. This one has column
- 31:52one, column five. This one has column
- 31:54five, column one. This one has column
- 31:56three, column two. And what do we do
- 31:57now? Everything will remain the same.
- 32:00This time, the input will be "pe2" here
- 32:03. Run it. The model is trained. Here's
- 32:10my first decision tree. It looks like
- 32:14this. Here's my second decision tree.
- 32:19It looks like this. And here's my third
- 32:21decision tree. It looks like this. So,
- 32:25these two decision trees are somewhat
- 32:27similar. Kind of mirror images. Now
- 32:29let's do one thing. Let's find out for
- 32:31this point. Okay, let's take a point
- 32:35for which we know the answer. So let's
- 32:37do one thing. Print DF3. So we're
- 32:44taking this point. Its answer should be
- 32:47one. Okay? So we'll go down and paste
- 32:51it here. The answer should be one. We
- 32:56know. So we asked the first decision
- 32:59tree. It said we'd have to add a pe2
- 33:03here while reshaping. It's one. Let's
- 33:07ask the second decision tree. Copy it,
- 33:15it's saying it's zero. And this one is
- 33:23saying it's one. Two people are again
- 33:25saying one, and one is saying zero. So,
- 33:28the output will be one. So, loosely,
- 33:30random forests work this way. I'll
- 33:33share this notebook with you. Please
- 33:36give it a try. You'll get a great
- 33:38intuition about how bagging, or random
- 33:40forests, works internally. Okay? So,
- 33:45next, we'll go through another piece of
- 33:48code to see what benefits a random
- 33:50forest provides. Okay? We'll look at
- 33:53that next.
About this transcript
This page contains the full transcript of Introduction to Random Forest | Intuition behind the Algorithm by CampusX, generated from the public captions YouTube serves with the video. The transcript has 4,634 words across 669 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.