I am confused about random_state parameter in some algorithms like AdaboostClasifier, DecisionTree and so on

Here is an example

from sklearn.model_selection import *
from sklearn.ensemble import AdaBoostClassifier
param_grid = {
    'learning_rate':[0.001, 0.10, 0.1, 1],

             'n_estimators':range(50, 400, 50)

abc = AdaBoostClassifier(random_state=123)

# run grid search
grid_abc=GridSearchCV(abc, param_grid, scoring = 'accuracy')
grid_abc.fit(X_train, y_train)
#The best hyper parameters set
print("Best Hyper Parameters:\n",grid_abc.best_params_)
print("training accuracy:\n",grid_abc.best_score_)
#importing the metrics module
from sklearn import metrics
#evaluation(Confusion Matrix)
print("Confusion Matrix:\n",metrics.confusion_matrix(prediction,y_test))
print("\t\tclassification report")
print("-" * 52)

The accuracy results is 0.9420289855072463

But when i change the random_state value to 0 I got another accuracy results 0.8584070796460177

How can i fix the result and be sure of the final results.It is ricky by the way Do i have to set random_state value of the train_test split as the classifier or no?


If i understand correctly you want to make sure your results will remain constant and won't change?
Or in other words, are you trying to make sure your results are reproducible?

If so, this FAQ in the scikit-learn site has exactly what you are looking for.

I'll also repeat what it says here:
An algorithm might have multiple points that introduce randomness to the process and thus introduce randomness to the result.

One method to make sure your result are constant is to set every possible random_state available in the functions that you use.
The risk in this method is that you might miss some of the places that uses a seed.

The second method, which they also describe in the FAQ, is to set a global seed once.
scikit-learn will use it for all it's random processes.
So setting a global seed like this

import numpy as np

will make your results constant and reproducible.

  • $\begingroup$ thanks exctely what i asked about as even in splitting data X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=0) there is also a random state $\endgroup$ Nov 28 '18 at 20:15
  • $\begingroup$ Each time i changed the seed i got a different results Do i have to repeat the suffle to get the final results because results with np.random.seed(42) is not as np.random.seed(0) and so on. $\endgroup$ Nov 28 '18 at 20:24
  • $\begingroup$ It's a common trick to change seeds and ensembles :) but the difference shouldn't be this big imho $\endgroup$
    – Aditya
    Nov 28 '18 at 20:38
  • $\begingroup$ @RawiaSammout i'm not sure what you are asking than. I'd like to ask you some questions to better understand. 1. What are you trying to achieve? 2. When you say 'final results', what do you mean? 3. Why are you trying to use different seeds? 4. Why would you like to receive the same results with different seeds? and why is it important if you do or do not gain the same result with different seeds? :) $\endgroup$ Nov 28 '18 at 22:06
  • $\begingroup$ i am implenmeting Adaboost as a classifier to test its accuracy comparing to other classifiers. The problem is when i use np.random.seed(42) as global seed for train_test_split( X, y, test_size=0.3, random_state=None) and for all the classifiers random_state=None. Then, i got an accuracy result. So if i use np.random.seed(123) as global seed i got another accuracy result. Which one i have to assume or i need to understand $\endgroup$ Nov 29 '18 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.