{"id":4779,"date":"2026-10-15T09:59:50","date_gmt":"2026-10-15T09:59:50","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=4779"},"modified":"2026-10-16T16:03:41","modified_gmt":"2026-10-16T16:03:41","slug":"random-forest-tutorial-for-beginners","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/","title":{"rendered":"Random Forest Tutorial for Beginners"},"content":{"rendered":"<h1><strong><b>Random Forest Tutorial for Beginners<\/b><\/strong><\/h1>\n<p>Suppose you want a second opinion on a medical report. You would not trust one doctor if you could ask a hundred, each trained a little differently and each looking at different details. If most of them reach the same conclusion, you can be fairly confident. A random forest works on this principle. It is a crowd of decision trees that vote.<\/p>\n<p>Random forest is one of the most widely used algorithms in machine learning, and for good reason. It handles both classification and regression, works well with little tuning, copes with many features and gives you a measure of which features matter. It is often the first strong model people try on a table of data, and the one other models are compared against.<\/p>\n<p>In this tutorial, we begin with the decision tree, the building block of the forest, and see why a single tree is not enough. We then explain how a random forest is built, how its trees split data using the Gini index, entropy or mean squared error, and what its strengths and weaknesses are. A complete Python program follows, with output from a real run that includes accuracy, precision and recall, the effect of the number of trees and the most important features. We finish with common mistakes and a short FAQ.<\/p>\n<h2><strong><b>Random Forest in Machine Learning<\/b><\/strong><\/h2>\n<p>Classification is a supervised learning task. The model learns from examples that already have labels, then predicts the label of new data, such as spam or not spam, or benign or malignant. Many algorithms do this, including logistic regression, KNN, SVM and Naive Bayes. Random forest is one of the strongest general purpose choices among them, and it is also used for regression, where the answer is a number and not a class.<\/p>\n<h2><strong><b>Decision Tree: The Building Block of a Random Forest<\/b><\/strong><\/h2>\n<p>A decision tree classifies data by asking a series of yes or no questions. Each question splits the data into smaller groups, and the final group gives the answer. For example, a tree for loan approval might ask whether income is above a limit, then whether there is a past default, and then end at approve or reject.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4781\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF2.png\" alt=\"Random Forest Tutorial for Beginners\" width=\"850\" height=\"579\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF2.png 950w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF2-300x204.png 300w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF2-768x523.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>A single tree is easy to read, but it has a weakness. If left to grow deep, it memorises its training data, including the noise. Change a few rows and you can get a very different tree. That sensitivity is called high variance, and it is what the forest fixes.<\/p>\n<h2><strong><b>What is a Random Forest?<\/b><\/strong><\/h2>\n<p>A random forest builds many decision trees and combines their answers. Two kinds of randomness make the trees different from each other:<\/p>\n<ul>\n<li><b><\/b><strong><b>Random rows.<\/b><\/strong>Each tree trains on a bootstrap sample, which is a random draw of rows with replacement. This is the bagging idea.<\/li>\n<li><b><\/b><strong><b>Random features.<\/b><\/strong>At each split, a tree considers only a random subset of the features, not all of them. This stops every tree from relying on the same strong feature.<\/li>\n<\/ul>\n<p>To predict, every tree gives its answer. For classification, the class with the most votes wins. In scikit learn, the forest averages the trees&#8217; predicted probabilities, which usually gives the same answer. For regression, it averages the trees&#8217; numbers.<\/p>\n<p>Say a forest has 6 trees, and 4 of them predict class A while 2 predict class B. The forest answers class A. Even if two of the trees are wrong, the group is still right.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4782\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/rf3.png\" alt=\"Introduction of Random Forest \" width=\"850\" height=\"494\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/rf3.png 713w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/rf3-300x174.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<ol>\n<li><b><\/b><strong><b>Draw a bootstrap sample <\/b><\/strong>of the training rows for each tree.<\/li>\n<li><b><\/b><strong><b>Grow a tree <\/b><\/strong>on that sample. At every split, look at only a random subset of features and pick the best split among them.<\/li>\n<li><b><\/b><strong><b>Repeat <\/b><\/strong>until you have the number of trees you want, 100 or more is typical.<\/li>\n<li><b><\/b><strong><b>Combine the trees. <\/b><\/strong>Vote for classification, average for regression.<\/li>\n<\/ol>\n<h2><strong><b>Why is Random Forest Better Than a Single Tree?<\/b><\/strong><\/h2>\n<p>Each tree makes its own mistakes, but because the trees are different, the mistakes point in different directions and largely cancel when averaged. The pattern that every tree shares is kept. The result is a model with lower variance than one tree, and usually higher accuracy on new data, without much extra effort.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Single decision tree vs random forest<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 220px;\" scope=\"col\">Point<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Single decision tree<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Random forest<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Accuracy on new data<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Lower and unstable<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Higher and steadier<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Overfitting<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">High if grown deep<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Reduced, not removed<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Easy to explain<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes, you can draw it<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No, it is a black box<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Speed<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Fast to train and predict<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Slower, many trees<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Tuning needed<\/th>\n<td style=\"padding: 12px 16px;\">Depth and splits matter a lot<\/td>\n<td style=\"padding: 12px 16px;\">Works well with defaults<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><strong><b>How a Tree Chooses Its Splits?<\/b><\/strong><\/h2>\n<p>At every node, a tree looks for the question that separates the data best. How it measures best depends on the task.<\/p>\n<h3><strong><b>Regression: Mean Squared Error<\/b><\/strong><\/h3>\n<p>For regression, a common choice is mean squared error, or MSE. It is the average of the squared gaps between the predicted values and the actual values. In the formula, N is the number of data points, fi is the value the model returns, and yi is the actual value for that data point. A split is chosen if it makes this error smaller. MSE is a common choice, not the only one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4783\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF4.png\" alt=\"Regression problems of Random Forest in Machine Learning\" width=\"850\" height=\"166\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF4.png 457w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF4-300x58.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong><b>Classification: Gini Index and Entropy<\/b><\/strong><\/h3>\n<p>For classification, the tree uses a measure of how mixed the classes are in a node. A node with only one class is pure, and a node with an even mix is as impure as it gets. A good split makes the groups after the split purer than before.<\/p>\n<p>The <strong><b>Gini index<\/b><\/strong>\u00a0is one minus the sum of the squared class proportions. In the formula, pi is the relative frequency of each class and c is the number of classes. It measures impurity. It does not measure which branch is more likely.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4784\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF5.png\" alt=\"Classification Problems of Random Forest in Machine Learning\" width=\"850\" height=\"298\" \/><\/p>\n<p><strong><b>Entropy<\/b><\/strong>\u00a0is another measure of impurity. It is the negative sum of each class proportion times the base 2 logarithm of that proportion.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4785\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF6.png\" alt=\"Classification problem in Random Forest_Entropy\" width=\"850\" height=\"199\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF6.png 414w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF6-300x70.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>Here is a worked example. A node holds 10 records, 6 of one class and 4 of the other, so the proportions are 0.6 and 0.4.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 720px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Gini and entropy for a node with 6 of one class and 4 of the other<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 180px;\" scope=\"col\">Measure<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Calculation<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 200px;\" scope=\"col\">Result<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Gini index<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1 minus (0.6 squared plus 0.4 squared)<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">0.48<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Entropy<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Minus (0.6 times log2 of 0.6, plus 0.4 times log2 of 0.4)<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">0.971<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">A pure node<\/th>\n<td style=\"padding: 12px 16px;\">Only one class<\/td>\n<td style=\"padding: 12px 16px;\">Gini 0, entropy 0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>A perfectly even node would score 0.5 on Gini and 1 on entropy, so this node is quite mixed. In practice, Gini and entropy usually pick very similar splits. Gini is the default in scikit learn and is a little faster.<\/p>\n<p>&nbsp;<\/p>\n<h2><strong><b>Random Forest in Python With scikit learn<\/b><\/strong><\/h2>\n<p>This program uses the breast cancer dataset that comes with scikit learn, with 569 patients and 30 measurements each, labelled malignant or benign. It compares one tree with a forest, shows precision and recall, tests how the number of trees matters, lists the most important features and checks the Gini and entropy example above.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">import numpy as np\r\nfrom sklearn.datasets import load_breast_cancer\r\nfrom sklearn.ensemble import RandomForestClassifier\r\nfrom sklearn.metrics import accuracy_score, classification_report\r\nfrom sklearn.model_selection import train_test_split\r\nfrom sklearn.tree import DecisionTreeClassifier\r\n\r\ndata = load_breast_cancer()\r\nX, y = data.data, data.target\r\nX_train, X_test, y_train, y_test = train_test_split(\r\n    X, y, test_size=0.3, random_state=42, stratify=y\r\n)\r\n\r\n# One tree against a forest\r\ntree = DecisionTreeClassifier(random_state=42).fit(X_train, y_train)\r\nforest = RandomForestClassifier(n_estimators=200, oob_score=True, random_state=42)\r\nforest.fit(X_train, y_train)\r\nprint(\"Single tree accuracy:\", round(tree.score(X_test, y_test), 3))\r\nprint(\"Forest accuracy:\", round(accuracy_score(y_test, forest.predict(X_test)), 3))\r\nprint(\"Forest out of bag score:\", round(forest.oob_score_, 3))\r\n\r\n# Precision and recall, not accuracy alone\r\nprint(classification_report(y_test, forest.predict(X_test), target_names=data.target_names))\r\n\r\n# How the number of trees changes the score\r\nfor n in [1, 10, 50, 200]:\r\n    m = RandomForestClassifier(n_estimators=n, random_state=42).fit(X_train, y_train)\r\n    print(n, \"trees:\", round(m.score(X_test, y_test), 3))\r\n\r\n# The five most important features\r\norder = np.argsort(forest.feature_importances_)[::-1][:5]\r\nfor i in order:\r\n    print(data.feature_names[i], round(forest.feature_importances_[i], 3))\r\n\r\n# Gini and entropy for a node with 6 of one class and 4 of the other\r\np = np.array([0.6, 0.4])\r\nprint(\"Gini:\", round(1 - (p ** 2).sum(), 3))\r\nprint(\"Entropy:\", round(-(p * np.log2(p)).sum(), 3))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">Single tree accuracy: 0.918\r\nForest accuracy: 0.942\r\nForest out of bag score: 0.967\r\n              precision    recall  f1-score   support\r\n\r\n   malignant       0.94      0.91      0.92        64\r\n      benign       0.94      0.96      0.95       107\r\n\r\n    accuracy                           0.94       171\r\n   macro avg       0.94      0.93      0.94       171\r\nweighted avg       0.94      0.94      0.94       171\r\n\r\n1 trees: 0.912\r\n10 trees: 0.93\r\n50 trees: 0.924\r\n200 trees: 0.942\r\nworst perimeter 0.141\r\nworst area 0.132\r\nworst concave points 0.122\r\nworst radius 0.085\r\nmean concave points 0.077\r\nGini: 0.48\r\nEntropy: 0.971<\/pre>\n<\/div>\n<h3><strong><b>Part 1: One Tree Against a Forest<\/b><\/strong><\/h3>\n<p>When I ran the program, it printed:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">Single tree accuracy: 0.918\r\nForest accuracy: 0.942\r\nForest out of bag score: 0.967<\/pre>\n<\/div>\n<p>The test set has 171 patients, so the forest got about 4 more patients right than the single tree. The out of bag score is a free validation estimate, computed from the rows each tree did not see. It came to 0.967 without touching the test set.<\/p>\n<h3><strong><b>Part 2: Accuracy Is Not Enough<\/b><\/strong><\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">              precision    recall  f1-score   support\r\n\r\n   malignant       0.94      0.91      0.92        64\r\n      benign       0.94      0.96      0.95       107\r\n\r\n    accuracy                           0.94       171<\/pre>\n<\/div>\n<p>Overall accuracy looks fine at 0.94, but look at the malignant row. Its recall is 0.91, which means the forest caught 58 of 64 malignant cases and missed 6. In a medical setting, a missed malignant case is the costly error, so recall for that class matters more than the headline number. This is the check the original page left out.<\/p>\n<h3><strong><b>Part 3: How Many Trees?<\/b><\/strong><\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">1 trees: 0.912\r\n10 trees: 0.93\r\n50 trees: 0.924\r\n200 trees: 0.942<\/pre>\n<\/div>\n<p>One tree scores 0.912, and more trees help overall. But 50 trees scored a little below 10 trees, which is normal noise on a small test set, since one patient is worth about 0.6 points. The honest lesson is that more trees give steadier results, with diminishing returns. A forest does not get worse from adding trees. It only gets slower.<\/p>\n<h3><strong><b>Part 4: Feature Importance<\/b><\/strong><\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">worst perimeter         0.141\r\nworst area              0.132\r\nworst concave points    0.122\r\nworst radius            0.085\r\nmean concave points     0.077<\/pre>\n<\/div>\n<p>A forest can rank which features it relied on. Here the largest tumour measurements matter most. Treat this ranking with care. Perimeter, area and radius are strongly related to each other, so the forest splits the credit between them. The importance scores show what the model used, not what causes the result.<\/p>\n<h2><strong><b>Important Settings of RandomForestClassifier<\/b><\/strong><\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Key RandomForestClassifier settings in scikit learn<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 190px;\" scope=\"col\">Setting<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 130px;\" scope=\"col\">Default<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">What it does<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">n_estimators<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">100<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Number of trees. More is steadier but slower<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">max_depth<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">How deep trees can grow. Limit it to reduce overfitting<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">max_features<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">sqrt<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">How many features are considered at each split<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">min_samples_leaf<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Minimum rows in a leaf. Raise it to smooth the model<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">oob_score<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">False<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Switch on for a free validation score<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">class_weight<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Use balanced when one class is rare<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">n_jobs<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Parallel jobs. Use minus one for all cores<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">random_state<\/th>\n<td style=\"padding: 12px 16px;\">None<\/td>\n<td style=\"padding: 12px 16px;\">Set a number so results can be repeated<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<h2><strong><b>Advantages and Disadvantages of Random Forest<\/b><\/strong><\/h2>\n<h3><strong><b>Advantages<\/b><\/strong><\/h3>\n<ul>\n<li>Works for both classification and regression.<\/li>\n<li>Reduces the overfitting of a single tree.<\/li>\n<li>Works well with default settings and little tuning.<\/li>\n<li>Handles many features and does not need feature scaling.<\/li>\n<li>Trees train independently, so training can run in parallel.<\/li>\n<li>Gives feature importance and an out of bag score.<\/li>\n<\/ul>\n<h3><strong><b>Disadvantages<\/b><\/strong><\/h3>\n<ul>\n<li>Harder to interpret than one tree.<\/li>\n<li>Uses more memory and takes longer to predict with many deep trees.<\/li>\n<li>Cannot predict values outside the range it saw in training, which matters for regression on trends.<\/li>\n<li>Can still overfit noisy data, especially with unlimited depth.<\/li>\n<li>Feature importance can mislead when features are related to each other.<\/li>\n<\/ul>\n<h2><strong><b>Where Random Forest is Used?<\/b><\/strong><\/h2>\n<ul>\n<li><b><\/b><b><\/b>Credit scoring and fraud detection.<\/li>\n<li><b><\/b><b><\/b>Disease risk from patient measurements.<\/li>\n<li><b><\/b><strong><b>E commerce. <\/b><\/strong>Customer churn and product recommendation.<\/li>\n<li><b><\/b><b><\/b>Predicting equipment failure from sensor readings.<\/li>\n<li><b><\/b><b><\/b>Predicting prices and default risk.<\/li>\n<\/ul>\n<h2><strong><b>Common Mistakes to Avoid<\/b><\/strong><\/h2>\n<ul>\n<li><b><\/b><strong><b>Trusting accuracy alone. <\/b><\/strong>Check precision and recall, especially with imbalanced classes.<\/li>\n<li><b><\/b><strong><b>Judging by training accuracy. <\/b><\/strong>A forest of deep trees will score near perfect on its own training data.<\/li>\n<li><b><\/b><strong><b>Reading feature importance as cause. <\/b><\/strong>It shows what the model used.<\/li>\n<li><b><\/b><strong><b>Using too few trees. <\/b><\/strong>Start with 100 or more.<\/li>\n<li><b><\/b><strong><b>Leaving trees unlimited on noisy data. <\/b><\/strong>Set max_depth or min_samples_leaf.<\/li>\n<li><b><\/b><strong><b>Forgetting random_state. <\/b><\/strong>Without it, results change each run.<\/li>\n<\/ul>\n<p>A random forest turns many unstable trees into one dependable model. Run the program above on your own data, look at recall as well as accuracy, and use the out of bag score as a quick check. When you want to push accuracy higher, look at boosting next.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Random Forest Tutorial for Beginners Suppose you want a second opinion on a medical report. You would not trust one doctor if you could ask a hundred, each trained a little differently and each looking at different details. If most of them reach the same conclusion, you can be fairly confident. A random forest works [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4780,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,1696],"tags":[1752,1750,1751,421],"class_list":["post-4779","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","category-machine-learning-modules","tag-mastering-random-forests-in-python","tag-random-forest-algorithm-for-machine-learning-a-beginners-guide","tag-random-forest-classification-with-scikit-learn","tag-random-forest-tutorial-for-beginners"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Random Forest Tutorial for Beginners With Python | Prwatech<\/title>\n<meta name=\"description\" content=\"Learn how a random forest works, from decision trees and Gini to voting, with advantages, limits and a tested Python example using scikit learn.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Random Forest Tutorial for Beginners With Python | Prwatech\" \/>\n<meta property=\"og:description\" content=\"Learn how a random forest works, from decision trees and Gini to voting, with advantages, limits and a tested Python example using scikit learn.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-15T09:59:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-16T16:03:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"975\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/\",\"url\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/\",\"name\":\"Random Forest Tutorial for Beginners With Python | Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png\",\"datePublished\":\"2026-10-15T09:59:50+00:00\",\"dateModified\":\"2026-10-16T16:03:41+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Learn how a random forest works, from decision trees and Gini to voting, with advantages, limits and a tested Python example using scikit learn.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png\",\"width\":975,\"height\":650,\"caption\":\"Random Forest Tutorial for Beginners\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Random Forest Tutorial for Beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Random Forest Tutorial for Beginners With Python | Prwatech","description":"Learn how a random forest works, from decision trees and Gini to voting, with advantages, limits and a tested Python example using scikit learn.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/","og_locale":"en_US","og_type":"article","og_title":"Random Forest Tutorial for Beginners With Python | Prwatech","og_description":"Learn how a random forest works, from decision trees and Gini to voting, with advantages, limits and a tested Python example using scikit learn.","og_url":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-15T09:59:50+00:00","article_modified_time":"2026-10-16T16:03:41+00:00","og_image":[{"width":975,"height":650,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png","type":"image\/png"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/","url":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/","name":"Random Forest Tutorial for Beginners With Python | Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png","datePublished":"2026-10-15T09:59:50+00:00","dateModified":"2026-10-16T16:03:41+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Learn how a random forest works, from decision trees and Gini to voting, with advantages, limits and a tested Python example using scikit learn.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/RF1.png","width":975,"height":650,"caption":"Random Forest Tutorial for Beginners"},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/random-forest-tutorial-for-beginners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Random Forest Tutorial for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=4779"}],"version-history":[{"count":13,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4779\/revisions"}],"predecessor-version":[{"id":12016,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4779\/revisions\/12016"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media\/4780"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=4779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=4779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=4779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}