1
$\begingroup$

I am used Grid search to find the optimal paramters of random forest classifier such as max_depth,max_features,min_samples_split,min_samples_leaf",bootstrap,criterion,n_estimators and the problem i have faced that each time i run the model i got different results. i tried with other datasets and i got the same issue. i think it's related to set the seed of the training set i did but it still the same issue.

the code is below

import numpy as np

from time import time
from scipy.stats import randint as sp_randint

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV

from sklearn.ensemble import RandomForestClassifier



#X=datascaled.iloc[:,0:71]
#Selected_features=['Event','AVK','Beta blockers','proton pump inhibitor','Previous stroke','CYP2C19*17','Clopidogrel active metabolite','Obesity']
Selected_features=['Event time','CYP2C19*17','Clopidogrel active metabolite', 'proton pump inhibitor', 'DOSE BB','Previous stroke', 'Obesity','AVK']
X=datascaled[Selected_features]
y=datascaled['Cardio1']

# build a classifier
clf = RandomForestClassifier(n_estimators=20)


# Utility function to report best scores
def report(results, n_top=3):
    for i in range(1, n_top + 1):
        candidates = np.flatnonzero(results['rank_test_score'] == i)
        for candidate in candidates:
            print("Model with rank: {0}".format(i))
            print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
                  results['mean_test_score'][candidate],
                  results['std_test_score'][candidate]))
            print("Parameters: {0}".format(results['params'][candidate]))
            print("")


# specify parameters and distributions to sample from
param_dist = {"max_depth": [3, None],
              "max_features": sp_randint(1, 8),
              "min_samples_split": sp_randint(2, 11),
              "min_samples_leaf": sp_randint(1, 11),
              "bootstrap": [True, False],
              "criterion": ["gini", "entropy"],'n_estimators': [10,20,30,50,100,200,500]}

# run randomized search
n_iter_search = 20
random_search = RandomizedSearchCV(clf, param_distributions=param_dist,
                                   n_iter=n_iter_search, random_state=123)

start = time()
random_search.fit(X, y)
print("RandomizedSearchCV took %.2f seconds for %d candidates"
      " parameter settings." % ((time() - start), n_iter_search))
report(random_search.cv_results_)

# use a full grid over all parameters
param_grid = {"max_depth": [3, None],
              "max_features": [1, 3, 8],
              "min_samples_split": [2, 3, 10],
              "min_samples_leaf": [1, 3, 10],
              "bootstrap": [True, False],
              "criterion": ["gini", "entropy"],'n_estimators': [10,20,30,50,100,200,500]}

# run grid search
grid_search = GridSearchCV(clf, param_grid=param_grid)
start = time()
grid_search.fit(X, y)

print("GridSearchCV took %.2f seconds for %d candidate parameter settings."
      % (time() - start, len(grid_search.cv_results_['params'])))
report(grid_search.cv_results_) 
$\endgroup$

closed as off-topic by Björn, Ferdi, Peter Flom Sep 24 '18 at 11:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Björn, Ferdi, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.