# Random Forest Algorithm in Machine Learning

2 mins read
14::7

### Decision Tree

The Decision Tree Algorithm is a popular algorithm and falls in the category of supervised learning. They are used to resolve both regression and classification problems. The Decision Tree makes use of the tree representation to resolve the problem in which every leaf node corresponds to a class label & characteristics are represented on the internal node of this tree. Using the decision tree we can represent any Boolean function on discrete attributes.

So, I assume you’re acquainted using the Decision Tree Algorithm to know more you can Click Here…

### Random Forest Algorithm

Random Forest is a popular ML algorithm that belongs to the supervised mastering algorithms. It may be used for both Regression and Classification problems in ML. It’s based on the idea of ensemble learning, which happens to be a procedure of pairing several classifiers in order to solve a complicated problem and to boost the overall performance of the algorithm.

Random Forest as the name suggests Random Forest is a classifier that has a number of decision trees on numerous subsets of the specified dataset. It takes the average to enhance the predictive accuracy of the given dataset. Rather than depending on a single decision tree, the random forest takes the prediction from every tree and depending on the majority of votes of predictions, plus it predicts the final output.

The larger the number of trees in the forest, the higher the accuracy of the algorithm and it also prevents the problem of over-fitting.

The reason behind this wonderful outcome is the fact that the trees protect one another from their individual mistakes (as long as they do not constantly all errors in the same direction). Although some trees might be wrong, several other trees will be right, so as a team the trees can move in the correct direction. So the prerequisites for a random forest to function well are:

• There must be some actual signals in our features so that models built based on these features will perform better than random guessing.
• The predictions made by the individual trees or the errors need to have low correlations with each other.

The image below explains the Random Forest Algorithm:

### Working of Random Forest Algorithm:

1. The first step is the selection of random samples or training sets from a given input data set.
2. A decision tree is developed by the algorithm for every sample. Then from every decision tree predictions are done.
3. Voting is done by each decision tree and it will be counted for every predicted result.
4. In the end, the prediction that gets the maximum votes is the prediction of the Random Forest Algorithm.

### Application of Random Forest in Real-Life:

• Banking

The banking industry consists of nearly all users. There are lots of loyal customers and fraud customers. To determine whether the customer is a faithful or fraud, Random forest analysis helps bankers. With the assistance of a random forest algorithm in machine learning, we can simply decide whether the customer is a fraud or not. A system use of a set of a random algorithm that identifies the fraud transactions by a series of the pattern.

• Stock Market

Machine learning also plays a role in stock market analysis. When you would like to learn the actions of the stock market, with the help of the Random Forest Algorithm, the actions of the stock market could be examined. In addition, it is able to show the expected loss or profit which may be created while buying a particular stock.

• Medicines

Medicines need a complex blend of specific chemical substances. As a result, to determine the great combination in the medications, the Random Forest Algorithm is used. With the assistance of the machine learning algorithm, it’s become easier to identify and anticipate the drug sensitivity of a medicine. In addition, it helps you to determine the patient’s illness by analyzing the person’s medical record.

• E-Commerce

When you are going to find it hard to suggest or even recommend what kind of things your customer must-see. This’s exactly where you can use a random forest algorithm. Using a machine learning process, you can recommend the solutions that will be a little more apt for a client. Using a specific pattern and sticking to the product’s curiosity of a buyer, you are able to recommend products that are similar to your customers.

### Advantaged of Random Forest Algorithm

• It combines the result of different decision trees that helps in overcoming the problem of overfitting.
• It works well when we have a large range of data as compared to a single decision tree.
• It has less variance than a single decision tree.
• It is very flexible.
• It possesses very high accuracy.
• It doesn’t require scaling of data.
• Even if the data is not scaled properly it still maintains good accuracy.
• Even if a large portion is missing in the given input data It still maintains good accuracy.

### Disadvantaged of Random Forest Algorithm

• The main disadvantage of the Random Forest Algorithm is its complexity.
• Construction of this algorithm is harder and time-consuming as compared to a single decision tree.
• To implement this more computational resources are required.
• When we have a large collection of decision trees it is less intuitive.
• The prediction done using this algorithm is time-consuming when compared to other algorithms.

### Prediction pseudocode of Random Forest

A trained random forest algorithm performs prediction based on the pseudocode given below:

1. It takes the test features and uses the rules of each randomly created decision tree to predict the output and stores the predicted output (Target).
2. It calculates the votes for every predicted target.
3. It considers the predicted target with the highest votes as the final prediction output of the random forest algorithm.

### Implementation of Random Forest Algorithm in Python:

Now we will implement the Random Forest Algorithm using python:

1. Data pre-processing steps
2. Fitting the given training set in the Random Forest Algorithm
3. Predicting the test results
4. Creation on confusion matrix (test accuracy of the result)
5. Visualizing the test set results