Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def RandomForestFuction():
- rf = RandomForestClassifier(random_state = 42)
- n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
- max_features = ['auto', 'sqrt'] # Number of features to consider at every split
- max_depth = [int(x) for x in np.linspace(10, 110, num = 11)] # Maximum number of levels in tree
- max_depth.append(None)
- min_samples_split = [2, 5, 10] # Minimum number of samples required to split a node
- min_samples_leaf = [1, 2, 4] # Minimum number of samples required at each leaf node
- bootstrap = [True, False] # Method of selecting samples for training each tree
- # Create the random grid
- random_grid = {'n_estimators': n_estimators,
- 'max_features': max_features,
- 'max_depth': max_depth,
- 'min_samples_split': min_samples_split,
- 'min_samples_leaf': min_samples_leaf,
- 'bootstrap': bootstrap}
- rf = RandomForestClassifier() # Number of trees in random forest
- # Random search of parameters, using 3 fold cross validation,
- # Search across 100 different combinations, and use all available cores
- rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 3, verbose=2, random_state=42, n_jobs = -1)
- rf_random.fit(train, y_train) # Fit the random search model
- print(rf_random.best_params_) # Printing the params
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement