You now have the ability to make a random forest using your own decision trees. However, scikit-learn
has a RandomForestClassifier
class that will do all of this work for you! RandomForestClassifier
is in the sklearn.ensemble
module.
RandomForestClassifier
works almost identically to DecisionTreeClassifier
— the .fit()
, .predict()
, and .score()
methods work in the exact same way.
When creating a RandomForestClassifier
, you can choose how many trees to include in the random forest by using the n_estimators
parameter like this:
classifier = RandomForestClassifier(n_estimators = 100)
We now have a very powerful machine learning model that is fairly resistant to overfitting!
Instructions
Create a RandomForestClassifier
named classifier
. When you create it, pass two parameters to the constructor:
n_estimators
should be2000
. Our forest will be pretty big!random_state
should be0
. There’s an element of randomness when creating random forests thanks to bagging. Setting therandom_state
to0
will help us test your code.
Train the forest using the training data by calling the .fit()
method. .fit()
takes two parameters — training_points
and training_labels
.
Test the random forest on the testing set and print the results. How accurate was the model?