Random forest consists a (large) number of decision trees operating together (ensemble learning). The class with the most votes from the trees will be chosen as the final result of the RF's prediction. These decision tree models are relatively uncorrelated so that they can protect each other from their individual errors.
❓ How (decision) trees are chosen? RF ensures that the chosen trees are not too correlated to the others.
- Bagging: From a sample of size N, trees are chosen so that they also have size N with replacement. For example, if our training data was [1, 2, 3, 4, 5] (size 5), then we might give one of our tree the list [1, 2, 2, 5, 5] (with replacement).
- Feature randomness: The features in the original dataset are chosen randomly. There may be some trees that are lacking in some features.
So in our random forest, we end up with trees that are not only trained on different sets of data (thanks to bagging) but also use different features to make decisions. (ref)
For each tree, we can use Decision Tree Classifier or Decision Tree Regression depending on the type our problem (classification or regression).
- Decision tree algorithms easily lead to overfitting problems. Random forest algorithm can overcome this.
- Capable of both regression and classification problems.
- Handle a large number of features.
- Estimating which features are important in the underlying data being modeled (ref).
- Random forest is capable of learning without carefully crafted data transformations (ref).
- Output probabilities for classification problems.
Load the library,
A sample dataset:
Create RF classifier(other parameters),
If a problem has imbalanced classes, use
class_weight="balanced"
. (ref)Training & predict (other methods),
Some premilinaries,
Select feature importance (ref),
Visualize,
Select features with importance greater than a threshold,
- Tony Yiu -- Understanding Random Forest.
- Scikit-learn -- Random Forest CLassifier official doc.
- Scikit-learn -- Random Forest Regression official doc.
- Chris Albon -- Titanic Competition With Random Forest.
- The Yhat Blog -- Random Forests in Python.
- fast.ai -- Introduction to Random Forest and a solution to "Bull Book for Bulldozers" problem on Kaggle.