Skip to content

Instantly share code, notes, and snippets.

@monspo1
Last active March 30, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monspo1/85170fc26448901c0f0569d2ef8ffc27 to your computer and use it in GitHub Desktop.
Save monspo1/85170fc26448901c0f0569d2ef8ffc27 to your computer and use it in GitHub Desktop.
import sklearn.grid_search as gs
# Fit a random forest on the training set.
from sklearn import ensemble
rf = ensemble.RandomForestClassifier()
rf.fit(x_train, y_train) # fit
#print "- The training error is: %.5f" %(1-rf.score(x_train, y_train))
#print "- The test error is: %.5f" %(1-rf.score(x_test, y_test))
# Find the best parameters using grid_search.GridSearchCV to
# The initial combination of the parameters:
# grid_para_forest = {'criterion': ['gini', 'entropy'], 'max_depth': range(1, 31), "n_estimators": range(10, 110, 10)}
grid_para_forest = [{'criterion': ['gini', 'entropy'], 'max_depth': range(1, 31), "n_estimators": range(10, 110, 10)}]
grid_search_forest = gs.GridSearchCV(rf, grid_para_forest, scoring='accuracy', cv=5)
grid_search_forest.fit(x_train, y_train)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment