简体   繁体   中英

Random Forest Improve Accuracy

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)

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 someadditonal reading from Sklearns Glossary on that parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM