Random Forest Improve Accuracy

Viewed 126

I'm using RandomForestClassifier method for object detection the problem is that even i know that my random state should be zero as default i got very bad accuracy, so is there anyway to know what is the best values for my n_estimators, random_state parameters ?

from sklearn.ensemble import RandomForestClassifier
RF_model = RandomForestClassifier(n_estimators = 250, random_state = 120)
1 Answers

To determine the best parameters for a model, you can use a process known as Grid Search. Sklearn provides a class for performing this, GridSearchCV. I've provided a code sample of how to use it for the Random Forest classifier.

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV

# provide iterables of values to be tested each parameter
parameters = {'n_estimators': [100, 250, 500, 750]}
clf = GridSearchCV(RandomForestClassifier(), parameters)
clf.fit(X, y)  # X and y are your training data and targets

It is worth noting that in your question you mention specifically looking for the best values for the n_estimators and random_state parameters. I have not included the random_state as part of the GridSearch as that parameter is typically present for the reproducibility of results. Here's some additonal reading from Sklearns Glossary on that parameter.

Related