# -*- coding: utf-8 -*-
"""
Created on Tue Nov 17 11:53:37 2020

@author: Mahamed
"""
#https://towardsdatascience.com/hyperparameter-tuning-the-random-forest-in-python-using-scikit-learn-28d2aa77dd74
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV







param_grid = {
    'bootstrap': [True],
    'max_depth': [80, 90, 100, 110],
    'max_features': [2, 3],
    'min_samples_leaf': [3, 4, 5],
    'min_samples_split': [8, 10, 12],
    'n_estimators': [100, 200, 300, 1000]
}
# Create a based model
rf = RandomForestClassifier()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid, 
                          cv = 5, n_jobs = -1, verbose = 2, scoring = 'accuracy')


grid_search.fit(X, y)


best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print("Best Accuracy: {:.2f} %".format(best_accuracy*100))
print("Best Parameters:", best_parameters)


































#
#from sklearn.model_selection import RandomizedSearchCV
## Number of trees in random forest
#n_estimators = [int(x) for x in np.linspace(start = 200, stop = 1000, num = 10)]
## Number of features to consider at every split
#max_features = ['auto', 'sqrt']
## Maximum number of levels in tree
#max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]
#max_depth.append(None)
## Minimum number of samples required to split a node
#min_samples_split = [2, 5, 10]
## Minimum number of samples required at each leaf node
#min_samples_leaf = [1, 2, 4]
## Method of selecting samples for training each tree
#bootstrap = [True, False]
## 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}
#pprint(random_grid)
#
#
#
## Use the random grid to search for best hyperparameters
## First create the base model to tune
#rf = RandomForestClassifier()
## 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 = 5, verbose=2, random_state=42, n_jobs = -1)
## Fit the random search model
#rf_random.fit(X, y)
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 17 11:53:37 2020

@author: Mahamed
"""
#https://towardsdatascience.com/hyperparameter-tuning-the-random-forest-in-python-using-scikit-learn-28d2aa77dd74
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV







param_grid = {
    'bootstrap': [True],
    'max_depth': [80, 90, 100, 110],
    'max_features': [2, 3],
    'min_samples_leaf': [3, 4, 5],
    'min_samples_split': [8, 10, 12],
    'n_estimators': [100, 200, 300, 1000]
}
# Create a based model
rf = RandomForestClassifier()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid, 
                          cv = 5, n_jobs = -1, verbose = 2, scoring = 'accuracy')


grid_search.fit(X, y)


best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print("Best Accuracy: {:.2f} %".format(best_accuracy*100))
print("Best Parameters:", best_parameters)


































#
#from sklearn.model_selection import RandomizedSearchCV
## Number of trees in random forest
#n_estimators = [int(x) for x in np.linspace(start = 200, stop = 1000, num = 10)]
## Number of features to consider at every split
#max_features = ['auto', 'sqrt']
## Maximum number of levels in tree
#max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]
#max_depth.append(None)
## Minimum number of samples required to split a node
#min_samples_split = [2, 5, 10]
## Minimum number of samples required at each leaf node
#min_samples_leaf = [1, 2, 4]
## Method of selecting samples for training each tree
#bootstrap = [True, False]
## 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}
#pprint(random_grid)
#
#
#
## Use the random grid to search for best hyperparameters
## First create the base model to tune
#rf = RandomForestClassifier()
## 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 = 5, verbose=2, random_state=42, n_jobs = -1)
## Fit the random search model
#rf_random.fit(X, y)
# -*- coding: utf-8 -*- Created on Tue Nov 17 11:53:37 2020
 
 @author: Mahamed #https://towardsdatascience.com/hyperparameter-tuning-the-random-forest-in-python-using-scikit-learn-28d2aa77dd74 # Create a based model # Instantiate the grid search model # #from sklearn.model_selection import RandomizedSearchCV ## Number of trees in random forest #n_estimators = [int(x) for x in np.linspace(start = 200, stop = 1000, num = 10)] ## Number of features to consider at every split #max_features = ['auto', 'sqrt'] ## Maximum number of levels in tree #max_depth = [int(x) for x in np.linspace(10, 110, num = 11)] #max_depth.append(None) ## Minimum number of samples required to split a node #min_samples_split = [2, 5, 10] ## Minimum number of samples required at each leaf node #min_samples_leaf = [1, 2, 4] ## Method of selecting samples for training each tree #bootstrap = [True, False] ## 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} #pprint(random_grid) # # # ## Use the random grid to search for best hyperparameters ## First create the base model to tune #rf = RandomForestClassifier() ## 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 = 5, verbose=2, random_state=42, n_jobs = -1) ## Fit the random search model #rf_random.fit(X, y)