YouTube2Text

Introduction to Random Forest | Intuition behind the Algorithm — Transcript

by CampusX · 4,634 words · 669 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hello guys, welcome to my YouTube
  2. 0:02channel. In this video, we're going to
  3. 0:04start with a machine learning algorithm
  4. 0:06that you'll use in every project. It's
  5. 0:09so powerful and so flexible that any
  6. 0:11project you're doing in the machine
  7. 0:13learning domain will definitely use
  8. 0:14this algorithm. Right? This algorithm's
  9. 0:18name is Random Forest. I hope you've
  10. 0:20heard of it before. Right? Now, in this
  11. 0:22video and the next two or three videos,
  12. 0:25we'll learn all about Random Forest in
  13. 0:27detail. So let's start the video. So,
  14. 0:31the best thing about Random Forest is
  15. 0:33that it's an algorithm that
  16. 0:34consistently delivers excellent results
  17. 0:36on every type of machine learning
  18. 0:38problem. Right? This, along with
  19. 0:43Gradient Boosting and XG Boost, is one
  20. 0:46of two algorithms that performs well on
  21. 0:48every machine learning problem. Right?
  22. 0:52That's the first good part about Random
  23. 0:54Forest. The second good thing is that
  24. 0:57random forests can be applied to both
  25. 0:59regression and classification problems.
  26. 1:05The third, which is especially great
  27. 1:07for beginners, is that the computer
  28. 1:09scientist who created random forests (I
  29. 1:11don't know their name), but he wrote in
  30. 1:14his paper that random forests are an
  31. 1:16algorithm that doesn't require much
  32. 1:17tuning out of the box. Meaning, even if
  33. 1:24you don't tinker with its settings too
  34. 1:26much, its performance remains excellent
  35. 1:28. In fact, you'll notice in the future
  36. 1:33that if you're using random forests in
  37. 1:36any project, along with other
  38. 1:37algorithms, without touching any
  39. 1:39parameters, random forests always give
  40. 1:41you the best results most of the time.
  41. 1:46Okay? Now let's talk about what you
  42. 1:48need to know to study random forests.
  43. 1:51So, random forests are an equivalent
  44. 1:53technique. Okay? So, to study this, you
  45. 1:56should first understand decision trees.
  46. 1:58If you don't have an idea of a decision
  47. 2:00tree, you won't understand anything
  48. 2:02about random forests because a random
  49. 2:04forest is basically a collection of
  50. 2:06trees. A collection of trees is why
  51. 2:08it's called a forest. Okay? Second,
  52. 2:10it's actually a bagging technique. The
  53. 2:14algorithm we just covered was bagging
  54. 2:16operations. Okay? Now, I've covered
  55. 2:19bagging operations in great detail. But
  56. 2:22if you don't know that, you might have
  57. 2:24a little trouble with this video. I'll
  58. 2:30try to teach you from scratch, but it's
  59. 2:32still recommended that you watch the
  60. 2:33bagging video first. Okay? So, let's
  61. 2:38start with random forests. So, the
  62. 2:42first thing you should know clearly
  63. 2:44about random forests is why it's called
  64. 2:46random forests. Okay? Let's talk about
  65. 2:50why it's called random forests. So,
  66. 2:53there are two words here: random and
  67. 2:56forest. You can easily justify the word
  68. 2:58forest. Because here you train multiple
  69. 3:02decision trees together. That's why we
  70. 3:06call it a forest. So, it's basically a
  71. 3:08group of trees. And forest is a logical
  72. 3:12name for a group of trees. Second,
  73. 3:15where does the word "random" come from?
  74. 3:19The word "random" comes from the fact
  75. 3:21that random forest is a bagging-based
  76. 3:23technique. It's a bagging technique.
  77. 3:27And I told you in the bagging video
  78. 3:29that the full form of bagging is "boot
  79. 3:31strap." Aggregation. Right. Now, this "
  80. 3:43boot strap" comes from the fact that
  81. 3:45you're sampling here. What is sampling?
  82. 3:50Randomly selecting data. Right? So,
  83. 3:53this randomly selected section of data
  84. 3:55is where the word "random" comes from.
  85. 3:58Right? So, I hope you've first
  86. 4:00understood why it's called "random
  87. 4:02forest." Right? Now, it's a bagging
  88. 4:05technique. Essentially, it's a bagging
  89. 4:08technique. I taught you about the
  90. 4:10bagging technique, that you can use any
  91. 4:12base algorithm. Right? You can use KN.
  92. 4:15You can use SBM. You can use any base
  93. 4:18algorithm. But if you use a decision
  94. 4:23tree as the base algorithm in bagging,
  95. 4:25you call it a random forest. Right? So,
  96. 4:29the most important point is that this
  97. 4:31is the bagging technique. But the
  98. 4:34bagging technique has a specialization.
  99. 4:36And what is that specialization? If
  100. 4:39you're using all the models in the
  101. 4:41bagging technique as decision trees,
  102. 4:42then it becomes a random forest. There
  103. 4:46are some other minor differences, but
  104. 4:47we'll learn about them a little later.
  105. 4:50Okay? Now, for those who don't
  106. 4:53understand bagging or haven't seen a
  107. 4:55video on bagging, let me explain how it
  108. 4:57all works. Let's assume you don't
  109. 5:00understand bagging. So, I'll explain
  110. 5:02from scratch how a random forest works.
  111. 5:04Okay? So, it's very simple. Look, let's
  112. 5:08say you have a data set with 1,000 days
  113. 5:11. Okay? 1,000 days. Any number of
  114. 5:15columns. Let's say you have a data set.
  115. 5:18Okay? Now, what do you do here? You
  116. 5:20work in two steps. So, what do you do
  117. 5:22in the first step? You pick a bunch of
  118. 5:25base models. And all of these base
  119. 5:28models are decision trees. It's always
  120. 5:30a random forest, so a decision tree
  121. 5:32will be the base model. Okay? So, this
  122. 5:34is decision tree one. This is decision
  123. 5:38tree two. This is decision tree three.
  124. 5:40Dot dot dot, let's say we have 100
  125. 5:43decision trees. So the last one is
  126. 5:45decision tree 100. Okay? Now, what you
  127. 5:52do is you give this data to each
  128. 5:54decision tree to train, but you don't
  129. 5:56give it all the data; what you do is
  130. 5:58you create samples out of your data.
  131. 6:04Okay? So, you do two ways of sampling.
  132. 6:06The first is sampling. Actually, you do
  133. 6:09sampling in three ways. The first is
  134. 6:11row sampling. Let me give you an
  135. 6:14example of row sampling. Let's say you
  136. 6:18decided to give 500 rows to each base
  137. 6:20model and each decision tree. How many
  138. 6:23rows did you have in total? 1000. But
  139. 6:26you decided to give only 500 rows to
  140. 6:28each base model. Right? And how will
  141. 6:32those 500 rows be decided? Because our
  142. 6:34data has different rows. Like 1, 2, 3,
  143. 6:384, 5, and so on, there are 1000 rows.
  144. 6:43But only 500 of these rows will go into
  145. 6:45this first decision tree. Which ones?
  146. 6:48We don't decide this. They are selected
  147. 6:50randomly. Right? So, randomly, we
  148. 6:53selected one, we selected 18, we
  149. 6:56selected 32, we selected 43, we
  150. 6:59selected 103. So on and on. In this way
  151. 7:04, we selected 500 different rows. We
  152. 7:09selected 500 different rows. And you
  153. 7:13can call this row, that is, this data
  154. 7:15form you created, D1. You give this D1
  155. 7:19to the first decision tree to train.
  156. 7:22Okay? Now, the selection that's
  157. 7:25happening here also has two types. One
  158. 7:29is with sampling and without sampling.
  159. 7:44So, I guess you know, sorry, I'm sorry,
  160. 7:46I completely mispronounced "with
  161. 7:48replacement" and "without replacement."
  162. 7:52Replacement is the word. I'm sorry,
  163. 7:55with replacement and without
  164. 7:57replacement. So, let me give you an
  165. 8:01example of "with replacement." Suppose
  166. 8:05you want to give your first decision
  167. 8:07tree data of 500 rows and you're doing
  168. 8:09row sampling with replacement. Row
  169. 8:12sampling with replacement. So, suppose
  170. 8:14the first row you randomly selected was
  171. 8:17one. Now you have one in your new data
  172. 8:20set. But you returned this one row.
  173. 8:23This means it's available for selection
  174. 8:25again. When you selected it a second
  175. 8:28time, it returned 42. Then, when you
  176. 8:31selected it a third time, it returned
  177. 8:331. Why? Because you're using with
  178. 8:35replacement. You're picking one row and
  179. 8:37putting it back in. So, it could be
  180. 8:39selected again. This means that the
  181. 8:43data set you get here has a chance of
  182. 8:45having duplicate rows. If you're
  183. 8:49talking about using with replacement,
  184. 8:51but if you're talking about using
  185. 8:52without replacement, then there won't
  186. 8:54be any duplicate rows. Okay? So, that's
  187. 8:58what I was telling you: what you do? In
  188. 9:00the case of random forests, you take
  189. 9:02multiple base models, which are
  190. 9:04decision trees. Then, you feed data to
  191. 9:07each decision tree. That data is
  192. 9:10actually a subset of your actual data
  193. 9:12set. Okay? Now, there are three ways to
  194. 9:15generate those subsets. The first is
  195. 9:17row sampling. The second is column
  196. 9:19sampling. What you do in column
  197. 9:21sampling is you do the same thing on
  198. 9:23columns. So, suppose you have 10
  199. 9:26columns, and for the first decision
  200. 9:29tree, you decided that each decision
  201. 9:31tree would have only five columns. Each
  202. 9:34decision tree would have only five
  203. 9:36columns. So, when you're deciding for
  204. 9:38the first decision tree, you'll
  205. 9:40randomly choose five columns from the
  206. 9:4210. Okay? Then, for the second decision
  207. 9:46tree, you'll randomly choose five
  208. 9:48columns. And you'll do this for every
  209. 9:50decision tree. So, this sampling is
  210. 9:52done on columns. That's why it's called
  211. 9:55feature sampling. Row sampling is done
  212. 9:57on rows, feature sampling is done on
  213. 9:59features, edge sampling is done on
  214. 10:01columns. And there are two types of it:
  215. 10:03one with replacement and the other
  216. 10:05without replacement. With replacement,
  217. 10:07there may be duplicates. Without
  218. 10:09replacement, there won't be duplicates.
  219. 10:11Okay? And the third type, I guess you
  220. 10:14can guess, is a combination of these.
  221. 10:17So, you're sampling based on both.
  222. 10:20You're extracting subsets of the days,
  223. 10:22and subsets of the columns. Okay? So,
  224. 10:25what are you doing in short? Let me
  225. 10:27summarize it all. When you're training
  226. 10:30your model, what is your model made of?
  227. 10:32Multiple smaller models. And those
  228. 10:34smaller models are decision trees. Okay
  229. 10:37? So, you give each decision tree a
  230. 10:39subset of data. How that subset of data
  231. 10:41is created depends on three things:
  232. 10:43either you do row sampling, or you do
  233. 10:45column sampling, or you do combined
  234. 10:47sampling. Okay? And then you're giving
  235. 10:49each one its own respective data. Here
  236. 10:52you're giving D2. Here you're giving D3
  237. 10:54. Here you're giving D00. And you train
  238. 10:57all these decision trees. Now, since
  239. 10:59each decision tree is getting different
  240. 11:02data, each decision tree will see
  241. 11:03different data. For example, this
  242. 11:06decision tree might look like this. It
  243. 11:11might look like this. This decision
  244. 11:13tree might look like this. This
  245. 11:18decision tree might look like this.
  246. 11:25Right? Since you're feeding different
  247. 11:27data to each decision tree, each
  248. 11:29decision tree will train differently.
  249. 11:31Okay? What do you do next? Now that all
  250. 11:34your decision trees have been trained,
  251. 11:36you come to the second step. The first
  252. 11:38step was boot-stepping, which we just
  253. 11:40saw. This is bootstaping, which we just
  254. 11:42did. The second is aggregation. What
  255. 11:46you do in aggregation is that when you
  256. 11:49need to make a prediction, you use the
  257. 11:51aggregation step. So, you have all your
  258. 11:53decision trees trained. You have 100
  259. 11:56decision trees trained. And let's say
  260. 11:58this is a classification problem. Now
  261. 12:01you have a new query point whose class
  262. 12:03you need to determine. So you give that
  263. 12:06decision point to each decision tree.
  264. 12:09Given to every decision tree. Now,
  265. 12:12since every decision tree can make a
  266. 12:14prediction, it predicted whether it
  267. 12:16should be one or zero. Like this. Okay?
  268. 12:22Right? Now, in the aggregation step,
  269. 12:25what you do is you ask how many
  270. 12:27decision trees said zero? Let's say 43
  271. 12:31trees out of 100 said its label is zero
  272. 12:34, and 157 said it. Now, since 57 is
  273. 12:38greater than 43, most decision trees
  274. 12:40are saying that the label of this
  275. 12:42incoming query point should be one. Now
  276. 12:45you say its label will be one. And that
  277. 12:48is what aggregation is. Aggregation
  278. 12:50means the most frequent or most common
  279. 12:53thing, you assume, like democracy. Okay
  280. 12:56? If this were a regression problem,
  281. 12:58what would you do? You would again give
  282. 13:01the query point to everyone. Okay? What
  283. 13:04would happen this time? Instead of zero
  284. 13:06, one would come back. A number would
  285. 13:09come back. What would you do with that
  286. 13:12number? Calculate the mean, and you'd
  287. 13:15get your regression output. Okay? So,
  288. 13:18in a nutshell, that's what a random
  289. 13:20forest is. What I just taught you is
  290. 13:22the same thing I taught you in the
  291. 13:24previous video when I taught bagging.
  292. 13:27The only difference here is that the
  293. 13:29base models are decision trees. And
  294. 13:32there's a very important difference,
  295. 13:34which I'll cover a little later when I
  296. 13:36teach you about bagging versus random
  297. 13:38forest. But let's summarize at this
  298. 13:41point. Since we've covered a lot, let's
  299. 13:44summarize that random forest is a
  300. 13:46bagging-based technique. Bagging means
  301. 13:50boot-step aggregation. Bootstepping
  302. 13:53means you have multiple models. You
  303. 13:56give each model a subset of data. Now,
  304. 13:59how is that subset of data created? It
  305. 14:01depends on three things. First, are you
  306. 14:03using row sampling? Second, are you
  307. 14:06using column sampling? Third, are you
  308. 14:08using a combined approach? Okay? And
  309. 14:11then you train all the models on a
  310. 14:13subset of the data and then use the
  311. 14:15aggregation step to make predictions.
  312. 14:18So that's the whole thing. Random
  313. 14:20Forest is exactly similar to bagging.
  314. 14:23Nothing different. There's a small
  315. 14:25difference that we'll cover later.
  316. 14:28Before that, I want to show you a
  317. 14:30little demo of the benefits of using
  318. 14:32Random Forest. Okay? So, we'll see that
  319. 14:35next. So, guys, to explain Random
  320. 14:37Forest to you, I've created a
  321. 14:39step-by-step demo. Now, this isn't a
  322. 14:42perfect demo. Meaning, not everything
  323. 14:46I've shown here is exactly what happens
  324. 14:48inside a Random Forest. It's a loose
  325. 14:51demo to explain the concept. But
  326. 14:54everything is not true. I'll explain
  327. 14:56what's wrong. But this whole thing will
  328. 14:59give you a basic understanding of
  329. 15:00Random Forest. Okay? So, let's start
  330. 15:03with the demo. First, I've generated a
  331. 15:06data set. Okay? Using the make
  332. 15:09classification function. And this is
  333. 15:11the data set. This data set has five
  334. 15:14input columns and one output column.
  335. 15:16And this is a classification problem.
  336. 15:18Okay? Now, here I've created three
  337. 15:21different functions. One function is
  338. 15:24called sample rows. If you give it a
  339. 15:28data frame and tell it a percentage, it
  340. 15:30will randomly sample from it and give
  341. 15:31it to you. So, if you have a data frame
  342. 15:38with 100 rows and you call this
  343. 15:40function and say, "Give me 50%of the
  344. 15:42data randomly." What will this function
  345. 15:46do? It will randomly generate 50%of the
  346. 15:48rows from that data frame and give it
  347. 15:51to you. Okay? Then I created a similar
  348. 15:53function called "sample features,"
  349. 15:55which will do the same thing, but on
  350. 15:57columns. And then I created another
  351. 16:00function called "combined sampling,"
  352. 16:02which will do the same thing on both.
  353. 16:06So, you can extract rows, meaning
  354. 16:08subsets of rows and columns. Let me
  355. 16:12first show you using sample rows. Okay?
  356. 16:15So what am I going to do? I explained
  357. 16:17earlier that a random forest is nothing
  358. 16:19but a collection of decision trees. So,
  359. 16:22the random forest we're going to create
  360. 16:24will consist of three decision trees.
  361. 16:26So, I need to feed all three of them
  362. 16:28data. The data I have now is this,
  363. 16:30which has 100 rows and a total of six
  364. 16:32columns. Right? What am I going to do
  365. 16:34now? I'm going to create three separate
  366. 16:36data sets. How am I going to create all
  367. 16:38three? Using row sampling. Okay? Now,
  368. 16:41to do the row sampling, I'm going to
  369. 16:43call the sampleRows function. Here, I'm
  370. 16:47going to send my original data frame
  371. 16:49and specify a percentage of the
  372. 16:51original data that the new data I want
  373. 16:53should contain. So, I'm saying I only
  374. 16:58want 10%of the data. So, if my original
  375. 17:01data set had 100 rows, then the new
  376. 17:04data set I want will only have 10 rows.
  377. 17:07Okay? So as I run this, you can see
  378. 17:11that here, these 10 rows have been
  379. 17:13randomly selected. Okay? Now, this is
  380. 17:17with replacement. Meaning, there could
  381. 17:19be duplicates here. I don't see any
  382. 17:22duplicates here, but there could be.
  383. 17:24Meaning, it's not necessary that there
  384. 17:26won't be any. For example, look here,
  385. 17:2952 and 52 appear twice. In fact, three
  386. 17:32times. Okay? There could be other
  387. 17:34duplicates here as well. So, I've done
  388. 17:37row sampling with replacement. Okay? So
  389. 17:40, for now, I'm just doing point one. So
  390. 17:44this is df1. This is the first data set
  391. 17:48that I'll give to my first decision
  392. 17:50tree. I've run it like this. Okay? Then
  393. 17:57I'm calling the same function again,
  394. 17:59giving my original data set and sending
  395. 18:01point1 again, and you can see that this
  396. 18:03time a different data set has come in.
  397. 18:08Okay? I'm calling this df2. df2, okay?
  398. 18:14And similarly, I'll generate one more
  399. 18:16data set for the third decision tree.
  400. 18:20I'll call it df3. Everything else is
  401. 18:22the same. Okay? So far, there's no
  402. 18:26problem. Now, if you extract the shape
  403. 18:32of any of these: df1: Dot Shape 10 6
  404. 18:35df2: Dot Shape 10 6 df3: Shape 10 6 The
  405. 18:39shape will be the same. Okay? So what
  406. 18:46did I do? I called the decision tree
  407. 18:48classifier and created three objects.
  408. 18:51So far, it's exactly the same. Now what
  409. 18:55do I do? I need to train the model. So,
  410. 18:57what do I do to train the model? I'll
  411. 18:59give all the rows of df1. And where do
  412. 19:02I put the columns? I'll give the first
  413. 19:04five columns. So, I'll give them from
  414. 19:06zero to four. 4 will be the last column
  415. 19:12. And as soon as I run this, all three
  416. 19:16decision trees are trained. Then I call
  417. 19:19the plot tree function. And here is my
  418. 19:23first decision tree. This is my first
  419. 19:26decision tree. This is my second
  420. 19:28decision tree. And this is my third
  421. 19:31decision tree. And if you look, you'll
  422. 19:36start to see the differences. For
  423. 19:38example, there's a zero and a five here
  424. 19:40. Meaning, five samples came in the
  425. 19:43left node, and five samples came in the
  426. 19:46right node. Where as in this one, there
  427. 19:49are seven samples in the left node, and
  428. 19:51three samples in this one. Okay? And if
  429. 19:54we go to the top, there are seven and
  430. 19:56three here too. In fact, let's do one
  431. 19:58thing. Let's rerun the entire code
  432. 20:00again. Let's hope the tree becomes a
  433. 20:04little different. So I'll rerun the
  434. 20:08entire code and see what difference it
  435. 20:13makes. Because every time the selection
  436. 20:15is random, the decision tree will also
  437. 20:16be different. This time a combination
  438. 20:19of 64 is coming. This time a
  439. 20:23combination of 55 is coming. And look,
  440. 20:27this is a completely different decision
  441. 20:29tree. In fact, let's do one thing. For
  442. 20:32a little more visible change, what we
  443. 20:34can do is rather than having 10%a day.
  444. 20:37Let's have 20%a day. 20%20%and I'm
  445. 20:47training everything again. Here's my
  446. 20:53first decision tree. 13 plus a total of
  447. 20:5820. 8 plus a total of 20. And 1 plus 8
  448. 21:10plus 11. Okay? So we have three
  449. 21:13different decision trees. Okay? Now
  450. 21:15tomorrow a new point comes. Let's say a
  451. 21:18new point comes. I'll pick a point from
  452. 21:20here. Let's say this point comes. This
  453. 21:24is this point. Okay? Now its target,
  454. 21:26its prediction, should be zero. So what
  455. 21:34I'm going to do is I'm going to pass
  456. 21:36this inside the np.dot array and I'm
  457. 21:38going to put commas everywhere like
  458. 21:40this. Okay? And I'm going to reshape it
  459. 21:44to 1 5 and I'm going to put it in the
  460. 21:46CLF one. So I'm asking the first
  461. 21:49decision tree, "What should the output
  462. 21:51of this point be?" And the number of
  463. 21:54features of one matches the input. Why
  464. 21:59is the number of features four? We just
  465. 22:13wrote one second. So what did we do
  466. 22:19wrong? We wrote 0 to 4 here. So 0, 1, 2
  467. 22:22, 3, so the train is in four columns.
  468. 22:25There should be five here. My bat, we
  469. 22:27were wrong from the beginning. There
  470. 22:30should be five here. So I'll run it
  471. 22:32again. And here's my first decision
  472. 22:45tree. It's getting a little deeper now.
  473. 22:49Here's my second decision tree. It's a
  474. 22:52little different. And here's my third
  475. 22:54decision tree. It's also quite
  476. 22:56different. Okay? And I'm making it 1,
  477. 22:595. So, according to the first, it
  478. 23:02should be zero. According to the second
  479. 23:07, I'll copy this entire code and I'll
  480. 23:09paste it over here. And I'll run it. It
  481. 23:16should be zero. And let's see what the
  482. 23:19third one should be. It should also be
  483. 23:23one according to the third one. So
  484. 23:27since two decision trees are returning
  485. 23:29zero and one decision tree is returning
  486. 23:31one, then going by majority count, you
  487. 23:33would say the output prediction for
  488. 23:36this query point should be one, I'm
  489. 23:38sorry, zero. Okay? So that's how row
  490. 23:41sampling works. Similarly, you can do
  491. 23:43column sampling as well. Let me show
  492. 23:46you quickly. What am I doing this time?
  493. 23:49I'm going to call it "Rather Than
  494. 23:51Sample Row." I'm going to call "
  495. 23:54SampleFeatures" and I'm going to pass
  496. 23:56it "df." There are five input columns
  497. 23:58in df. And what am I doing? I'm doing
  498. 24:03it by point. 70%, so total five total.
  499. 24:12Okay, okay, okay total. There's a
  500. 24:17problem. The problem with my code is
  501. 24:21that the percentage I've calculated
  502. 24:23should be of df, dot shape -1; because
  503. 24:25you don't count the target column.
  504. 24:31That's why. Once I run this code again,
  505. 24:36it's gone. Or, so now I'm saying, let's
  506. 24:40say give me 50%columns, give me 50%
  507. 24:42columns, so I have five columns in the
  508. 24:45total input. You can see that there are
  509. 24:48five columns in the total input. So 50%
  510. 24:50is 2.5, and if you round it to an
  511. 24:52integer, it becomes two. This means
  512. 24:56that if I run this code, I'll get a new
  513. 24:58data frame with how many rows? As many
  514. 25:01as 100 in the original data frame. But
  515. 25:03how many columns? Only two. Let's see
  516. 25:06if that works. And you can see 100 rows
  517. 25:09with two columns. Okay? If I add 8 here
  518. 25:14, then 8 of five is four. So now after
  519. 25:25doing this, the data frame we get will
  520. 25:27have 100 rows but four columns. Let's
  521. 25:31see. Look, these four columns were
  522. 25:34randomly generated. Okay? Every time I
  523. 25:37run this, different things will appear.
  524. 25:39Look, column two, column one, column
  525. 25:42five, column four, c columns 1, 2, 5,
  526. 25:473. Okay, and now what you can do with
  527. 25:50this again is put it in df1 (). This is
  528. 25:54df1. What I'll do next is copy and
  529. 25:57paste the same code. So, copy and paste
  530. 26:02, this is my df2, and this is my df3.
  531. 26:07So now I have three different data sets
  532. 26:09. Let's repeat the whole thing by
  533. 26:11sampling the columns. Okay, one more
  534. 26:17thing, we can't put a five here because
  535. 26:20the shape of the data set we have now
  536. 26:22is a total of four columns. Only four
  537. 26:27columns are coming. So we'll enter zero
  538. 26:30to three here. We'll give three. So 0,
  539. 26:341, 2. Copy and paste. Paste. Model
  540. 26:42trained. Unknown. Label. Continuous. Oh
  541. 26:47, okay, there's a slight logical issue
  542. 26:49here. The problem is that the target
  543. 26:53column isn't there. What we're
  544. 26:56returning doesn't even include the
  545. 26:58target column. We need to return the
  546. 27:00target column as well. So, I need to
  547. 27:02make a few changes to this code. Ah,
  548. 27:04I'll do it, and then I'll explain it to
  549. 27:06you. So, guys, I made the changes. Just
  550. 27:09a few changes. You can see this code if
  551. 27:12you want. Now you'll always get a
  552. 27:14target column back. Let me show you. So
  553. 27:18, if I run this code again, this
  554. 27:20warning is coming up because you're
  555. 27:22making changes to the data frame. And
  556. 27:27now, look, we only have four columns in
  557. 27:29the input, but we're getting a target
  558. 27:31column as well. Okay? So, what do we do
  559. 27:36now? The next code is very simple.
  560. 27:38We'll write here: zero to four. zero to
  561. 27:42four. zero to four. These are our
  562. 27:46decision trees trained. Here is our
  563. 27:48first decision tree. Since there are
  564. 27:50more rows here, it will be a very deep
  565. 27:52decision tree. Look, here is our second
  566. 27:54decision tree. It's a little different
  567. 27:57from the one above. Yes, it's quite
  568. 28:00different, and the third one will be
  569. 28:02different as well. Because you're
  570. 28:03giving different data. You're giving
  571. 28:05different columns. Okay? If you want to
  572. 28:09check, you can check who got which
  573. 28:10column. For example, df3 has these
  574. 28:14columns. I'll write "infact, df3 dot
  575. 28:19columns." I'm printing this out. Copy
  576. 28:31and paste to one, and you can see that
  577. 28:40all three data sets are different
  578. 28:42because they all have different columns
  579. 28:44. Okay? And that's why the decision
  580. 28:49tree trained is also very different.
  581. 28:51And now what will we do? We'll take a
  582. 28:54new query point again. For example,
  583. 28:56what I'll do is I'll take df3. This is
  584. 29:05DF3 and grab this query point or these
  585. 29:11query points. And the output should be
  586. 29:15one. So now I'll go to the first
  587. 29:18decision tree. This is the first
  588. 29:20decision tree. I'll pass the columns.
  589. 29:28And this will become 1/4. Look, it
  590. 29:33should be one. The first one is saying
  591. 29:35it should be one. Let's see what the
  592. 29:37second one is saying. So again, we're
  593. 29:39giving the same input to the second
  594. 29:40person. He's saying zero. Let's see
  595. 29:49what the third person is saying. The
  596. 29:55third person is also saying one. Since
  597. 29:57two people are saying one and one
  598. 29:59person is saying zero, the majority
  599. 30:01count would give you one, which is
  600. 30:02correct. Okay? So that's how it works.
  601. 30:05So first, I showed you row sampling.
  602. 30:07Then I showed you column sampling.
  603. 30:09There's another function here. Let me
  604. 30:11run that and show you. And this
  605. 30:13function is called combined sampling.
  606. 30:15Okay? So what do I do? I'll show you
  607. 30:18combined sampling. In combined sampling
  608. 30:20, you have to provide three elements of
  609. 30:22your data frame. Then, what percentage
  610. 30:24of rows do you want? I want five. So,
  611. 30:27I'll get 50 rows. 0.5 means 50 rows.
  612. 30:31Since the total number of rows was 100,
  613. 30:33it would be 50%. And I want the number
  614. 30:37of columns. 25, or five. Let's take
  615. 30:44both fives. So five and five columns
  616. 30:47will become two. Okay? So the resulting
  617. 30:50data frame I get will have 50 rows and
  618. 30:53two columns. Let's see if that happens.
  619. 30:59So you can see. If I run this and
  620. 31:05calculate its shape, it's coming out to
  621. 31:07be 50 50 3, of course, because the
  622. 31:09target column is different. The input
  623. 31:13will have two columns. The output will
  624. 31:14have one column. So the total is 50 3.
  625. 31:17So what do I do now? I'll rewrite it as
  626. 31:19df1. I'll copy this code. Paste it.
  627. 31:31This will be df2 and over here. Also,
  628. 31:37I'll write df3. So now I have three
  629. 31:41data sets. The rows are different, the
  630. 31:43columns are different, and there's more
  631. 31:45randomness. Okay? And let me show you
  632. 31:48the columns again. Each one has
  633. 31:50different columns. This one has column
  634. 31:52one, column five. This one has column
  635. 31:54five, column one. This one has column
  636. 31:56three, column two. And what do we do
  637. 31:57now? Everything will remain the same.
  638. 32:00This time, the input will be "pe2" here
  639. 32:03. Run it. The model is trained. Here's
  640. 32:10my first decision tree. It looks like
  641. 32:14this. Here's my second decision tree.
  642. 32:19It looks like this. And here's my third
  643. 32:21decision tree. It looks like this. So,
  644. 32:25these two decision trees are somewhat
  645. 32:27similar. Kind of mirror images. Now
  646. 32:29let's do one thing. Let's find out for
  647. 32:31this point. Okay, let's take a point
  648. 32:35for which we know the answer. So let's
  649. 32:37do one thing. Print DF3. So we're
  650. 32:44taking this point. Its answer should be
  651. 32:47one. Okay? So we'll go down and paste
  652. 32:51it here. The answer should be one. We
  653. 32:56know. So we asked the first decision
  654. 32:59tree. It said we'd have to add a pe2
  655. 33:03here while reshaping. It's one. Let's
  656. 33:07ask the second decision tree. Copy it,
  657. 33:15it's saying it's zero. And this one is
  658. 33:23saying it's one. Two people are again
  659. 33:25saying one, and one is saying zero. So,
  660. 33:28the output will be one. So, loosely,
  661. 33:30random forests work this way. I'll
  662. 33:33share this notebook with you. Please
  663. 33:36give it a try. You'll get a great
  664. 33:38intuition about how bagging, or random
  665. 33:40forests, works internally. Okay? So,
  666. 33:45next, we'll go through another piece of
  667. 33:48code to see what benefits a random
  668. 33:50forest provides. Okay? We'll look at
  669. 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.