Last Updated on

Decision trees can suffer from high variance which makes their results fragile to the specific training data used.

Building multiple models from samples of your training data, called bagging, can reduce this variance, but the trees are highly correlated.

Random Forest is an extension of bagging that in addition to building trees based on multiple samples of your training data, it also constrains the features that can be used to build the trees, forcing trees to be different. This, in turn, can give a lift in performance.

In this tutorial, you will discover how to implement the Random Forest algorithm from scratch in Python.

After completing this tutorial, you will know:

- The difference between bagged decision trees and the random forest algorithm.
- How to construct bagged decision trees with more variance.
- How to apply the random forest algorithm to a predictive modeling problem.

Discover how to code ML algorithms from scratch including kNN, decision trees, neural nets, ensembles and much more in my new book, with full Python code and no fancy libraries.

Let’s get started.

**Update Jan/2017**: Changed the calculation of fold_size in cross_validation_split() to always be an integer. Fixes issues with Python 3.**Update Feb/2017**: Fixed a bug in build_tree.**Update Aug/2017**: Fixed a bug in Gini calculation, added the missing weighting of group Gini scores by group size (thanks Michael!).**Update Aug/2018**: Tested and updated to work with Python 3.6.

## Description

This section provides a brief introduction to the Random Forest algorithm and the Sonar dataset used in this tutorial.

### Random Forest Algorithm

Decision trees involve the greedy selection of the best split point from the dataset at each step.

This algorithm makes decision trees susceptible to high variance if they are not pruned. This high variance can be harnessed and reduced by creating multiple trees with different samples of the training dataset (different views of the problem) and combining their predictions. This approach is called bootstrap aggregation or bagging for short.

A limitation of bagging is that the same greedy algorithm is used to create each tree, meaning that it is likely that the same or very similar split points will be chosen in each tree making the different trees very similar (trees will be correlated). This, in turn, makes their predictions similar, mitigating the variance originally sought.

We can force the decision trees to be different by limiting the features (rows) that the greedy algorithm can evaluate at each split point when creating the tree. This is called the Random Forest algorithm.

Like bagging, multiple samples of the training dataset are taken and a different tree trained on each. The difference is that at each point a split is made in the data and added to the tree, only a fixed subset of attributes can be considered.

For classification problems, the type of problems we will look at in this tutorial, the number of attributes to be considered for the split is limited to the square root of the number of input features.

1 |
num_features_for_split = sqrt(total_input_features) |

The result of this one small change are trees that are more different from each other (uncorrelated) resulting predictions that are more diverse and a combined prediction that often has better performance that single tree or bagging alone.

### Sonar Dataset

The dataset we will use in this tutorial is the Sonar dataset.

This is a dataset that describes sonar chirp returns bouncing off different surfaces. The 60 input variables are the strength of the returns at different angles. It is a binary classification problem that requires a model to differentiate rocks from metal cylinders. There are 208 observations.

It is a well-understood dataset. All of the variables are continuous and generally in the range of 0 to 1. The output variable is a string “M” for mine and “R” for rock, which will need to be converted to integers 1 and 0.

By predicting the class with the most observations in the dataset (M or mines) the Zero Rule Algorithm can achieve an accuracy of 53%.

You can learn more about this dataset at the UCI Machine Learning repository.

Download the dataset for free and place it in your working directory with the filename **sonar.all-data.csv**.

## Tutorial

This tutorial is broken down into 2 steps.

- Calculating Splits.
- Sonar Dataset Case Study.

These steps provide the foundation that you need to implement and apply the Random Forest algorithm to your own predictive modeling problems.

### 1. Calculating Splits

In a decision tree, split points are chosen by finding the attribute and the value of that attribute that results in the lowest cost.

For classification problems, this cost function is often the Gini index, that calculates the purity of the groups of data created by the split point. A Gini index of 0 is perfect purity where class values are perfectly separated into two groups, in the case of a two-class classification problem.

Finding the best split point in a decision tree involves evaluating the cost of each value in the training dataset for each input variable.

For bagging and random forest, this procedure is executed upon a sample of the training dataset, made with replacement. Sampling with replacement means that the same row may be chosen and added to the sample more than once.

We can update this procedure for Random Forest. Instead of enumerating all values for input attributes in search if the split with the lowest cost, we can create a sample of the input attributes to consider.

This sample of input attributes can be chosen randomly and without replacement, meaning that each input attribute needs only be considered once when looking for the split point with the lowest cost.

Below is a function name **get_split()** that implements this procedure. It takes a dataset and a fixed number of input features from to evaluate as input arguments, where the dataset may be a sample of the actual training dataset.

The helper function **test_split()** is used to split the dataset by a candidate split point and **gini_index()** is used to evaluate the cost of a given split by the groups of rows created.

We can see that a list of features is created by randomly selecting feature indices and adding them to a list (called **features**), this list of features is then enumerated and specific values in the training dataset evaluated as split points.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Select the best split point for a dataset def get_split(dataset, n_features): class_values = list(set(row[-1] for row in dataset)) b_index, b_value, b_score, b_groups = 999, 999, 999, None features = list() while len(features) < n_features: index = randrange(len(dataset[0])-1) if index not in features: features.append(index) for index in features: for row in dataset: groups = test_split(index, row[index], dataset) gini = gini_index(groups, class_values) if gini < b_score: b_index, b_value, b_score, b_groups = index, row[index], gini, groups return {'index':b_index, 'value':b_value, 'groups':b_groups} |

Now that we know how a decision tree algorithm can be modified for use with the Random Forest algorithm, we can piece this together with an implementation of bagging and apply it to a real-world dataset.

### 2. Sonar Dataset Case Study

In this section, we will apply the Random Forest algorithm to the Sonar dataset.

The example assumes that a CSV copy of the dataset is in the current working directory with the file name **sonar.all-data.csv**.

The dataset is first loaded, the string values converted to numeric and the output column is converted from strings to the integer values of 0 and 1. This is achieved with helper functions **load_csv()**, **str_column_to_float()** and **str_column_to_int()** to load and prepare the dataset.

We will use k-fold cross validation to estimate the performance of the learned model on unseen data. This means that we will construct and evaluate k models and estimate the performance as the mean model error. Classification accuracy will be used to evaluate each model. These behaviors are provided in the **cross_validation_split()**, **accuracy_metric()** and **evaluate_algorithm()** helper functions.

We will also use an implementation of the Classification and Regression Trees (CART) algorithm adapted for bagging including the helper functions **test_split()** to split a dataset into groups, **gini_index()** to evaluate a split point, our modified **get_split()** function discussed in the previous step, **to_terminal()**, **split()** and **build_tree()** used to create a single decision tree, **predict()** to make a prediction with a decision tree, **subsample()** to make a subsample of the training dataset and **bagging_predict()** to make a prediction with a list of decision trees.

A new function name **random_forest()** is developed that first creates a list of decision trees from subsamples of the training dataset and then uses them to make predictions.

As we stated above, the key difference between Random Forest and bagged decision trees is the one small change to the way that trees are created, here in the **get_split()** function.

The complete example is listed below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# Random Forest Algorithm on Sonar Dataset from random import seed from random import randrange from csv import reader from math import sqrt # Load a CSV file def load_csv(filename): dataset = list() with open(filename, 'r') as file: csv_reader = reader(file) for row in csv_reader: if not row: continue dataset.append(row) return dataset # Convert string column to float def str_column_to_float(dataset, column): for row in dataset: row[column] = float(row[column].strip()) # Convert string column to integer def str_column_to_int(dataset, column): class_values = [row[column] for row in dataset] unique = set(class_values) lookup = dict() for i, value in enumerate(unique): lookup[value] = i for row in dataset: row[column] = lookup[row[column]] return lookup # Split a dataset into k folds def cross_validation_split(dataset, n_folds): dataset_split = list() dataset_copy = list(dataset) fold_size = int(len(dataset) / n_folds) for i in range(n_folds): fold = list() while len(fold) < fold_size: index = randrange(len(dataset_copy)) fold.append(dataset_copy.pop(index)) dataset_split.append(fold) return dataset_split # Calculate accuracy percentage def accuracy_metric(actual, predicted): correct = 0 for i in range(len(actual)): if actual[i] == predicted[i]: correct += 1 return correct / float(len(actual)) * 100.0 # Evaluate an algorithm using a cross validation split def evaluate_algorithm(dataset, algorithm, n_folds, *args): folds = cross_validation_split(dataset, n_folds) scores = list() for fold in folds: train_set = list(folds) train_set.remove(fold) train_set = sum(train_set, []) test_set = list() for row in fold: row_copy = list(row) test_set.append(row_copy) row_copy[-1] = None predicted = algorithm(train_set, test_set, *args) actual = [row[-1] for row in fold] accuracy = accuracy_metric(actual, predicted) scores.append(accuracy) return scores # Split a dataset based on an attribute and an attribute value def test_split(index, value, dataset): left, right = list(), list() for row in dataset: if row[index] < value: left.append(row) else: right.append(row) return left, right # Calculate the Gini index for a split dataset def gini_index(groups, classes): # count all samples at split point n_instances = float(sum([len(group) for group in groups])) # sum weighted Gini index for each group gini = 0.0 for group in groups: size = float(len(group)) # avoid divide by zero if size == 0: continue score = 0.0 # score the group based on the score for each class for class_val in classes: p = [row[-1] for row in group].count(class_val) / size score += p * p # weight the group score by its relative size gini += (1.0 - score) * (size / n_instances) return gini # Select the best split point for a dataset def get_split(dataset, n_features): class_values = list(set(row[-1] for row in dataset)) b_index, b_value, b_score, b_groups = 999, 999, 999, None features = list() while len(features) < n_features: index = randrange(len(dataset[0])-1) if index not in features: features.append(index) for index in features: for row in dataset: groups = test_split(index, row[index], dataset) gini = gini_index(groups, class_values) if gini < b_score: b_index, b_value, b_score, b_groups = index, row[index], gini, groups return {'index':b_index, 'value':b_value, 'groups':b_groups} # Create a terminal node value def to_terminal(group): outcomes = [row[-1] for row in group] return max(set(outcomes), key=outcomes.count) # Create child splits for a node or make terminal def split(node, max_depth, min_size, n_features, depth): left, right = node['groups'] del(node['groups']) # check for a no split if not left or not right: node['left'] = node['right'] = to_terminal(left + right) return # check for max depth if depth >= max_depth: node['left'], node['right'] = to_terminal(left), to_terminal(right) return # process left child if len(left) <= min_size: node['left'] = to_terminal(left) else: node['left'] = get_split(left, n_features) split(node['left'], max_depth, min_size, n_features, depth+1) # process right child if len(right) <= min_size: node['right'] = to_terminal(right) else: node['right'] = get_split(right, n_features) split(node['right'], max_depth, min_size, n_features, depth+1) # Build a decision tree def build_tree(train, max_depth, min_size, n_features): root = get_split(train, n_features) split(root, max_depth, min_size, n_features, 1) return root # Make a prediction with a decision tree def predict(node, row): if row[node['index']] < node['value']: if isinstance(node['left'], dict): return predict(node['left'], row) else: return node['left'] else: if isinstance(node['right'], dict): return predict(node['right'], row) else: return node['right'] # Create a random subsample from the dataset with replacement def subsample(dataset, ratio): sample = list() n_sample = round(len(dataset) * ratio) while len(sample) < n_sample: index = randrange(len(dataset)) sample.append(dataset[index]) return sample # Make a prediction with a list of bagged trees def bagging_predict(trees, row): predictions = [predict(tree, row) for tree in trees] return max(set(predictions), key=predictions.count) # Random Forest Algorithm def random_forest(train, test, max_depth, min_size, sample_size, n_trees, n_features): trees = list() for i in range(n_trees): sample = subsample(train, sample_size) tree = build_tree(sample, max_depth, min_size, n_features) trees.append(tree) predictions = [bagging_predict(trees, row) for row in test] return(predictions) # Test the random forest algorithm seed(2) # load and prepare data filename = 'sonar.all-data.csv' dataset = load_csv(filename) # convert string attributes to integers for i in range(0, len(dataset[0])-1): str_column_to_float(dataset, i) # convert class column to integers str_column_to_int(dataset, len(dataset[0])-1) # evaluate algorithm n_folds = 5 max_depth = 10 min_size = 1 sample_size = 1.0 n_features = int(sqrt(len(dataset[0])-1)) for n_trees in [1, 5, 10]: print('Trees: %d' % n_trees) print('Scores: %s' % scores) print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores)))) |

A k value of 5 was used for cross-validation, giving each fold 208/5 = 41.6 or just over 40 records to be evaluated upon each iteration.

Deep trees were constructed with a max depth of 10 and a minimum number of training rows at each node of 1. Samples of the training dataset were created with the same size as the original dataset, which is a default expectation for the Random Forest algorithm.

The number of features considered at each split point was set to sqrt(num_features) or sqrt(60)=7.74 rounded to 7 features.

A suite of 3 different numbers of trees were evaluated for comparison, showing the increasing skill as more trees are added.

Running the example prints the scores for each fold and mean score for each configuration.

1 2 3 4 5 6 7 8 9 10 11 |
Trees: 1 Scores: [56.09756097560976, 63.41463414634146, 60.97560975609756, 58.536585365853654, 73.17073170731707] Mean Accuracy: 62.439% Trees: 5 Scores: [70.73170731707317, 58.536585365853654, 85.36585365853658, 75.60975609756098, 63.41463414634146] Mean Accuracy: 70.732% Trees: 10 Scores: [75.60975609756098, 80.48780487804879, 92.6829268292683, 73.17073170731707, 70.73170731707317] Mean Accuracy: 78.537% |

## Extensions

This section lists extensions to this tutorial that you may be interested in exploring.

**Algorithm Tuning**. The configuration used in the tutorial was found with a little trial and error but was not optimized. Experiment with larger numbers of trees, different numbers of features and even different tree configurations to improve performance.**More Problems**. Apply the technique to other classification problems and even adapt it for regression with a new cost function and a new method for combining the predictions from trees.

**Did you try any of these extensions?**

Share your experiences in the comments below.

## Review

In this tutorial, you discovered how to implement the Random Forest algorithm from scratch.

Specifically, you learned:

- The difference between Random Forest and Bagged Decision Trees.
- How to update the creation of decision trees to accommodate the Random Forest procedure.
- How to apply the Random Forest algorithm to a real world predictive modeling problem.

**Do you have any questions?**

Ask your questions in the comments below and I will do my best to answer.

Hi Jason,

Firstly, thanks for your work on this site – I’m finding it to be a great resource to start my exploration in python machine learning!

Now, I’m working through your python machine learning mini course and I’m up to Lesson 09: spot checking algorithms. You suggest testing the random forest which has lead me to this blog post where I’m tyring to run the recipe but get thrown the following:

Traceback (most recent call last):

File “test.py”, line 203, in

scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)

File “test.py”, line 57, in evaluate_algorithm

folds = cross_validation_split(dataset, n_folds)

File “test.py”, line 42, in cross_validation_split

index = randrange(len(dataset_copy))

File “//anaconda/lib/python3.5/random.py”, line 186, in randrange

raise ValueError(“empty range for randrange()”)

ValueError: empty range for randrange()

I’ve spent the better part of the last hour trying to work out what I may be doing wrong.. unfortunately I’m really new to coding so I’m finding it very difficult. I think i’ve narrowed to the following possibilities:

1. possibly a problem with the evaluate_algorithm function that has been defined..?

2. possibly an issue using randrange in python 3.5.2?

3. possibly a problem with the definition of “dataset”?

I think it’s either #1 because I can run the code without issue up until line 202 or #3 because dataset is the common thread in each of the returned lines from the error..?

Your guidance would be greatly appreciated!

thanks again!

marco

Hi,

Is it simple to adapt this implementation in order to accommodate tuples of feature vectors?

Thanks,

D.

Hi Jason, I was able to get the code to run and got the results as posted on this page. My question what next? How do you use these results to make classification on new data?

Thanks Jeff

You can fit a final model on all training data and start making predictions.

See this post about developing a final model:

http://machinelearningmastery.com/train-final-machine-learning-model/

Can you send me a video indicates the algorithm of random forest from scratch in paython

Sorry, I don’t have any videos.

Figured it out! It was a problem with using Python 3.5.2. I switched to 2.7 and it worked!

thanks

marco

Glad to hear it Marco.

Traceback (most recent call last):

File “rf2.py”, line 203, in

scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)

File “rf2.py”, line 68, in evaluate_algorithm

predicted = algorithm(train_set, test_set, *args)

File “rf2.py”, line 181, in random_forest

tree = build_tree(sample, max_depth, min_size, n_features)

File “rf2.py”, line 146, in build_tree

split(root, max_depth, min_size, n_features, 1)

File “rf2.py”, line 120, in split

left, right = node[‘groups’]

TypeError: ‘NoneType’ object is not iterable

I have the same problem

Works in python 3.x also. The division in line 45 :

fold_size = len(dataset) / n_folds

renders a float which remains valid when length of dataset_copy goes to zero. randrange(0) gives this error.

Replacing this line with

fold_size = len(dataset) // n_folds

gives an integer and the loop executes properly

Thanks beedotkiran.

I’d recommend casting the result, in case python beginners are not familiar with the double slash operator:

I have updated the cross_validation_split() function in the above example to address issues with Python 3.

This was a fantastic tutorial thanks you for taking the time to do this! I was wondering if you had any suggestions for serialization or the tree for use against other similar data sets, would pickling working for this structure? Thanks for you help!

Hi Jake, using pickle on the learned object would be a good starting point.

Hi Jason, great tutorial! Just a question about the function build_tree: when you evaluate the root of the tree, shouldn’t you use the train sample and not the whole dataset?

I mean:

root = get_split(train, n_features) rather than

root = get_split(dataset, n_features)

Can I ask also what are the main differences of this algorithm if you want adapt it to a regression problem rather than classification?

Thank you very much! Best regards

Sorry I didn’t see that you had already settled the change

No problem, nice catch!

Hello Jason great approach. I’m wondering if you have any tips about transforming the above code in order to support multi-label classification.

Thank you very much !!!

Not off hand, sorry Mike. I would have to do some homework.

Consider a search on google scholar or consider some multi-label methods in sklearn:

http://scikit-learn.org/stable/modules/multiclass.html#multilabel-classification-format

Hello Jason, I like the approach that allows a person to ‘look under the hood’ of these machine learning methods. I look forward to learning more of the machine learning methods this way.

Random forest is completely new to me. I have a dataset that could use random forest regression. I would like to know what changes are needed to make random forest classification code (above) into random forest regression. This was asked earlier by Alessandro but I didn’t understand the reply. Random forest regression is not explained well as far as I can tell.

Thanks.

Thanks Steve.

As a start, consider using random forest regression in the sklearn library:

http://machinelearningmastery.com/ensemble-machine-learning-algorithms-python-scikit-learn/

Jason,

Thanks for the advice with random forest regression.

On the sonar dataset, I plotted a 60 x 60 correlation matrix from the data. Many of the successive rows, and even not so close rows, are highly correlated. For instance, row 17 and column 18 have the following correlation:

Number of Observations: 131

Number of Degrees of Freedom: 2

R-squared: 0.870

Rmse: 0.1046

F statistic 863.

and columns 14 and 15 have the correlation

Number of Observations: 131

Number of Degrees of Freedom: 2

R-squared: 0.8554

Rmse: 0.0708

F statistic 763.

What impact does this correlation have on the use of random forest? What can be done to remove or measure the effect of the correlation?

Also, for this dataset I was able to get the following results:

n_folds = 5

max_depth = 12

min_size = 1

sample_size = 0.75

for n_trees in [1, 5, 19]:

71.875%, 73.438%, 82.031%

Thanks for the great work. I am trying to absorb it all.

Nice results.

I don’t think RF is too affected by highly corrected features. Nevertheless, try removing some and see how it impacts model skill. I’d love to hear what you discover.

how did you find correlation and why would it create a problem.I am kinda new to this so I would like to know these things from experts like you.Thank you.

Hello Jason,thanks for awesome tutorial,can you please explain following things>

1.what is function of this line : row_copy[-1] = None : because it works perfectly without this line

2.When i tried n_trees=[3,5,10] it returned following result in which accuracy decreases with more trees>

Trees: 3

Scores: [63.41463414634146, 51.21951219512195, 68.29268292682927, 68.29268292682927, 63.41463414634146]

Mean Accuracy: 62.927%

Trees: 5

Scores: [65.85365853658537, 60.97560975609756, 60.97560975609756, 60.97560975609756, 58.536585365853654]

Mean Accuracy: 61.463%

Trees: 10

Scores: [48.78048780487805, 60.97560975609756, 58.536585365853654, 70.73170731707317, 53.65853658536586]

Mean Accuracy: 58.537%

1. To clear the output value so the algorithm/developer cannot accidentally cheat.

2. Yes, it is important to tune an algorithm to a problem.

Would you like to help me?I am a student and I am using this for a problem that I found online >https://github.com/barotdhrumil21/road_sign_prediction_using_random_forest_classifier/tree/master

I would recommend contacting the author of that code.

how do you suggest I should use this :https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/random_forest_mnist.py

or can I use it and is it same what you’ve done?

Use whatever code you like.

nice job! what kind of cost function should i use when doing regression problems?

Great question, consider mean squared error or mean absolute error.

test_split has return two values but here groups = test_split(index, row[index], dataset) just one variable, can anyone explain that, please, thanks a lot

The returned array is assigned a variable named groups.

Hi Jason,

I am trying to learn RF through your sample example. But while running the code I am getting an error. I am using Ipython Notebook.

in split(node, max_depth, min_size, n_features, depth)

6 # Create child splits for a node or make terminal

7 def split(node, max_depth, min_size, n_features, depth):

—-> 8 left, right = node[‘groups’]

9 del(node[‘groups’])

10 # check for a no split

TypeError: ‘NoneType’ object is not iterable

Please help.

The chain error list:

TypeError Traceback (most recent call last)

in ()

16 n_features = int(sqrt(len(dataset[0])-1))

17 for n_trees in [1, 5, 10]:

—> 18 scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)

19 print(‘Trees: %d’ % n_trees)

20 print(‘Scores: %s’ % scores)

in evaluate_algorithm(dataset, algorithm, n_folds, *args)

12 test_set.append(row_copy)

13 row_copy[-1] = None

—> 14 predicted = algorithm(train_set, test_set, *args)

15 actual = [row[-1] for row in fold]

16 accuracy = accuracy_metric(actual, predicted)

in random_forest(train, test, max_depth, min_size, sample_size, n_trees, n_features)

18 for i in range(n_trees):

19 sample = subsample(train, sample_size)

—> 20 tree = build_tree(sample, max_depth, min_size, n_features)

21 trees.append(tree)

22 predictions = [bagging_predict(trees, row) for row in test]

in build_tree(train, max_depth, min_size, n_features)

2 def build_tree(train, max_depth, min_size, n_features):

3 root = get_split(train, n_features)

—-> 4 split(root, max_depth, min_size, n_features, 1)

5 return root

6

in split(node, max_depth, min_size, n_features, depth)

6 # Create child splits for a node or make terminal

7 def split(node, max_depth, min_size, n_features, depth):

—-> 8 left, right = node[‘groups’]

9 del(node[‘groups’])

10 # check for a no split

TypeError: ‘NoneType’ object is not iterable

Sorry, I don’t use notebooks. Confirm Python version 2.

Shouldn’t dataset be sorted by a feature before calculating gini?

Hello, Jason

Thank you very much for your lessons. You code worked perfectly.

Now I am trying to use different dataset, which has also string values. And having difficulty with it. Is it even possible? I keep getting errors that cannot convert string to integer.

Thanks.

You must convert the strings to integers or real values. Perhaps you need to use a one hot encoding?

Hi,

is there a need to perform a sum of the the weighted gini indexes for each split?

Thanks

Danny

Hi Jason, I have posted this protocol on YouTube as a reference @ https://youtu.be/Appc0Hpnado

Thanks for taking the time to teach us this method!

Jeff

Nice one Jeffrey!

Hi Jason, your implementation helps me a lot! However, I have a question here: on each split, the algorithm randomly selects a subset of features from the total features and then pick the best feature with the best gini score. Then, is it possible for a tree that a single feature is used repeatedly during different splits? since in get_split(), the line index = randrange(len(dataset[0])-1) basically pick features from the whole pool. Could you explain this? Thanks!

It does not choose the best split, but a random split from among the best.

You can split a single feature many times, if it makes sense from a gini-score perspective.

Yeah I realized this point. Thanks!

rf_model = training(training_data2,RandomForestClassifier())

print rf_model

test(rf_model,test_data2)

RandomForestClassifier(bootstrap=True, class_weight=None, criterion=’gini’,

max_depth=None, max_features=’auto’, max_leaf_nodes=None,

min_impurity_split=1e-07, min_samples_leaf=1,

min_samples_split=2, min_weight_fraction_leaf=0.0,

n_estimators=10, n_jobs=1, oob_score=False, random_state=None,

verbose=0, warm_start=False)

I tried using number of trees =1,5,10 as per your example but not working could you pls say me where shld i need to make changes and moreover when i set randomstate = none each time i execute my accuracy keeps on changing but when i set a value for the random state giving me same accuracy.

Hi,

I would like to change the code so it will work for 90% of data for train and 10% for test, with no folds. If I use n_folds = 1, I get an error. How can I change the code so it will work?

Perhaps you would be better served by using scikit-learn to fit your model:

https://machinelearningmastery.com/ensemble-machine-learning-algorithms-python-scikit-learn/

Thanks a lot. I would like to use your code since I made another internal change of the algorithm that can’t be done using scikit-learn. I think the major (may be the only) change is in the evaluate_algorithm function.

Can we implement random forest using fitctree in matlab?

There is a function call TreeBagger that can implement random forest. However, if we use this function, we have no control on each individual tree. Can we use the MATLAB function fitctree, which build a decision tree, to implement random forest? Thanks a lot.

Sorry, I don’t have material on matlab.

Hi Jason,

I am trying to solve classification problem using RF, and each time I run RandomForestClassifier on my training data, feature importance shows different features everytime I run it. How can I make sure it gives me same top 5 features everytime I run the model ? Please let me know.

model_rc = RandomForestClassifier(n_estimators=10,max_depth=None,min_samples_split=2,random_state=0)

rc_fit=model_rc.fit(X_train, y_train.values.ravel())

Yes, this is a feature of the algorithm.

See this post:

https://machinelearningmastery.com/randomness-in-machine-learning/

I would like to know the difference between sklearn randomforest and random forest algorithm implemented by oneself. Is there any weakness or something in sklearn randomforest?

I would recommend only implementing the algorithm yourself for learning, I would expect the sklearn implementation will be more robust and efficient.

Could you implement rotation forest algorithm ?

Thanks for the suggestion.

Your blogs and tutorials have aided me throughout my PhD. Thank you for putting so much time and effort into sharing this information.

Thanks, I’m really glad to hear that!

Dear Jason,

thank you very much for this implementation, fantastic work!

Is it possible to know which features are most discriminative

for the task at hand and maybe the degree of importance

for each of these features?

Many thanks in advance !!!

Yes, you can use feature selection methods:

http://machinelearningmastery.com/an-introduction-to-feature-selection/

Hi Jason,

Thanks for sharing! I wonder how fast is your implementation. (I guess I should try it out myself. 🙂

I was a master student in biostatistics and doing a thesis project which applied a modified random forest (no existing implementation) to solve a problem. I was and still I am only comfortable with R. I implemented the modified random forest from scratch in R. Although I tried hard to improve my code and implement some parts in C++ (via Rcpp package), it was still so slow… I noticed random forests packages in R or Python were all calling codes writing in C at its core.

So, would you mind estimate how fast is your implementation comparing to mainstream implementation (e.g. 10 times slower than Scikit-learn) ?

I am new to Python. I should really try it myself but just can’t help ask for a quick answer for this to inspire me to learn Python! 🙂

Thanks!

Kehao

It is slow. It is for learning purposes only.

I am new to python and doing a mini project for self learning. It will be helpful if you guide that how can I use this algorithm to predict the class of some test data.

A good place to start is here:

https://machinelearningmastery.com/start-here/#python

Hi Jason,

I might send another message but I am not sure if it had sent or not.

I just wanted to say thank you for your informative website.

this post was also and very comprehensive with full of integrated ideas and topics.

If the python project is available I would appreciate if you send it.

best regards,

niloofar

Thanks, I’m glad it helped!

hi

please how can i evaluate the algorithme !?

Great question, I answer it here:

https://machinelearningmastery.com/faq/single-faq/how-do-i-evaluate-a-machine-learning-algorithm

Good Dr Brownlee,

Kudos for the good work sir, I have a quick question sir. How long did it take you to write such a wonderful piece of code up and what are the resources you used to help you sir?

Thank you sir and kind regards.

Perhaps a day or two. I used some textbooks.

Great work Jason..wonder if I can use this to conceptualize a 3 way split tree – a tree that can have 3 classes, instead of binary?

I don’t see why not.

Hi…

How can I implement this code for multiclass classification?.

Perhaps this tutorial is a bit advanced, I would recommend using scikit-learn to get started:

https://machinelearningmastery.com/start-here/#python

Hi!

How can I implement your code for multi-class classification?

Thanks.

I would encourage you to use scikit-learn instead, as modifying this example for multi-class classification is not for beginners.

Hello Jason,

You might never see this because its been so long since posted this article.

I am running your code with python 3.6 in PyCharm and I noticed that if I comment out the

def str_column_to_int(dataset, column)

function then the code runs just fine but the accuracy scores change to this:

Trees: 1

Scores: [56.09756097560976, 63.41463414634146, 60.97560975609756, 58.536585365853654, 73.17073170731707]

Mean Accuracy: 62.439%

Trees: 5

Scores: [70.73170731707317, 58.536585365853654, 85.36585365853658, 75.60975609756098, 63.41463414634146]

Mean Accuracy: 70.732%

Trees: 10

Scores: [82.92682926829268, 75.60975609756098, 97.5609756097561, 80.48780487804879, 68.29268292682927]

Mean Accuracy: 80.976%

Process finished with exit code 0

The accuracy increases for the 10 trees.

Any idea whats going on?

More trees is better for this problem!

hi,

very nice explanation! but I am thinking what if I create a random forest from a dataset and then pass a single document to test it. what will be the method to pass a single document in the clf of random forest?

This tutorial is for learning how random forest works. If you are working on a project, I’d recommend that you use random forest in sklearn.

HI Jason,

This is a great tutorial. I’ve been working on a random forest project in R and have been reading alot about using this method. I’m confused because some articles note that RF will NOT overfit, yet there seems to be a constant discussion about overfitting with RF in stackoverflow. Do RF models overfit? My second question pertains to the Gini decrease scores–are these impacted by correlated variables ? (I know RF handles correlated predictor variables fairly well). Final question– if using CV in caret, is train/test sample necessary? I have a very unbalanced outcome classifier and not a ton of data, so I didn’t want to split it further, unless absolutely necessary.

Thank you!

Generally, bagged trees don’t overfit. I’ve read this and observed this, it might even be true.

Trees are invariant to correlated inputs.

Probability just CV or train/test would be sufficient, probably not both.

i have ten variables one dependent and nine independent first i will take sample of independent then random sample of observation and after that of preductive model

Random forest will choose split points using independent variables only.

Hi Jason,

Thank you for your great work !

I am running your code with python 3.7 in Spyder but I have this error :

“left, right = node[‘groups’]

TypeError: cannot unpack non-iterable NoneType object”.

I don’t understand why… Do you have an idea ?

Thanks.

Perhaps try saving all code to a file and running from the command line instead:

https://machinelearningmastery.com/faq/single-faq/how-do-i-run-a-script-from-the-command-line

Hello Dr. Jason,

Thanks so much for this wonderful website and the amazing work you do over here.

I went through your tutorial and had the same accuracy as found in it the tutorial. I realized that the attributes are selected with replacement so I made the modification and applied cross entropy loss for n_trees = [1, 5, 10, 15, 20]. I had the following accuracy metrics:

Trees: 1

Scores: [68.29268292682927, 63.41463414634146, 65.85365853658537, 73.17073170731707, 75.60975609756098]

Mean Accuracy: 69.268%

Trees: 5

Scores: [73.17073170731707, 82.92682926829268, 70.73170731707317, 70.73170731707317, 75.60975609756098]

Mean Accuracy: 74.634%

Trees: 10

Scores: [80.48780487804879, 75.60975609756098, 65.85365853658537, 75.60975609756098, 87.8048780487805]

Mean Accuracy: 77.073%

Trees: 15

Scores: [90.2439024390244, 70.73170731707317, 78.04878048780488, 73.17073170731707, 80.48780487804879]

Mean Accuracy: 78.537%

Trees: 20

Scores: [65.85365853658537, 75.60975609756098, 85.36585365853658, 87.8048780487805, 85.36585365853658]

Mean Accuracy: 80.000%

Well done!

Hello Jason,

it looks like I wrote a comment to not proper article before 🙂

I am inspired and wrote the python random forest classifier from this site. I go one more step further and decided to implement Adaptive Random Forest algorithm. But I faced with many issues.

I implemented the window, where I store examples. But unfortunately, I am unable to perform the classification. I cannot translate the learning step to be a little adaptive. I’m stuck.

Could you give me some advices, examples, how to overcome this issues ?

Sorry, I don’t have an example of adaptive random forest, I’ve not heard of it before.

This is an random forest which is able to learn from streams. It includes some concept drift detection method.

This is not common topic unfortunately.

Intersting. Good luck with your project!

Hello Jason,

Could you explain me how is it possible, that every time I am running your script I always receive the same scores ?

This means that in fact we do not implement random mechanism.

I fix the random number seed.

You can learn more here:

https://machinelearningmastery.com/introduction-to-random-number-generators-for-machine-learning/

Thanks a lot for such a great tutorial.

I’m glad it helped.

Hi Jason,

I need to print the predicted data set. But while printing, it is returning only the class value. I want to print the data with predicted class values “M” for mine and “R” for rock. I need the result as :

Input data set + Predicted Class

Could you please tell me how to do that?

You can map the predicted integer back to the class label and print the class label.

Sorry, I cannot prepare a code example for you.

How to implement Network Guided Forest using Random Forest in Python or R

What is “Network Guided Forest”?

Hello, Jason

As I know, the tree should continue to make splits until either the max_depth is reached or the left observations are completely pure. But this code makes split even if the node is already pure (gini = 0), meaning that it makes leaves from the same node which both have class value zero, which is not feasible.

To make more clear: if you give to get_split() some number of rows with the same class values, it still makes a split, although it is already pure.

Should we add this condition to the get_split() function or did I understand something wrong?

How would the Random Forest Classifier from SKlearn perform in the same situation?

Thank you for your reply in advance.

Kind regards,

Arkadiy

Yes, that sounds like a great improvement.

Hello Jason, for random forest, can we convert a regression problem into a classification problem,

Yes, you can. I cannot perform this conversion for you.

Hi

Given the 208 rows of the sonar dataset applied to the cross_validation_split function we only consider the first 205 rows of the given dataset, so the last 3 rows are simply ignored. Is this on purpose?

So I would expect to change it to something like:

Thanks

Do you maybe know how I could add code-snippet properly on your site? Maybe an entry for this on FAQ?

Use HTML pre tags:

https://www.w3schools.com/tags/tag_pre.asp

Yes, to keep the folds the same size.

Thanks for your reply.

I understand your reasoning but that has the price of loosing the information given by those extra three rows. Isn‘t that bad?

What is the benefit of having all folds of the same size? I changed the code of that function accordingly and obviously got different accuracies than the ones you have got.

Perhaps.

All folds the same size means that summary statistics calculated on the sample of evaluation scores are appropriately iid.

Hi

What is the rational behind the following line of code above:

train_set = sum(train_set, [])

And would it have the same effect if I would do:

train_set = train_set[0]

Thanks

I believe the line is redundant.

Hi

I get an error if I remove that line:

Traceback (most recent call last):

File “implement-random-forest-scratch-python.py”, line 210, in

scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)

File “implement-random-forest-scratch-python.py”, line 67, in evaluate_algorithm

predicted = algorithm(train_set, test_set, *args)

File “implement-random-forest-scratch-python.py”, line 188, in random_forest

tree = build_tree(sample, max_depth, min_size, n_features)

File “implement-random-forest-scratch-python.py”, line 152, in build_tree

root = get_split(train, n_features)

File “implement-random-forest-scratch-python.py”, line 105, in get_split

class_values = list(set(row[-1] for row in dataset))

TypeError: unhashable type: ‘list’

I verified that before that line the dimension of the train_set list is always:

(4, 41, 61)

And after that line it become:

(164, 61)

It’s the side effect of sum function which merges the first and second dimension into one, like when one would do something similar in numpy as:

> X.shape

[a, b, c]

X = X.reshape([a*b, c])

> X.shape

[a*b, c]

Ah yes, I see. It’s been many years since I wrote this tutorial 🙂

Hi

To my understanding to calculate the gini index for a given feature, first we need to iterate over ALL the rows and considering the value of that feature by the given row and add entries to the groups and KEEP them until we have processed all the rows of the dataset. Only now we can go ahead and calculate the gini index for that given feature.

However looking at the get_split function that doesn’t seem to be the case as we calculate the gini index on a single row basis at each step.

Thanks

Yes. It enumerates index over all rows.

Sir,

I tried this code for my dataset, it gives accuracy of 86.6%.

How to predict for unlabeled data?

yhat = model.predict(X). For this statement which will be ‘model’. Fit and predict methods are showing error. Can you please give some suggestions, since I am beginner to object-oriented concepts.

Yes, you can modify it to make predictions instead of evaluate the model.

If this is challenging for you, I would instead recommend using the scikit-learn library directly:

https://machinelearningmastery.com/start-here/#python

Hi Jason,

I’m wondering if you had any posts related to non-stationary inputs in a random forest.

I’m working on a project with non-stationary data and have found out that my random forest model from Scikit-learn is more accurate in the predictions when I use the non-stationary data directly as an input than when I difference it to achieve stationarity, so I would like to see how random forest deals with non-stationarity.

PS: I love your posts!

I may, I don’t recall off hand sorry.

Try to make the data stationary prior to modeling.