-3

I am using scikit-learn ensemble classifiers for classification.I have separate training and testing data sets.When I use the same data sets and classify using machine learning algorithms I am getting consistent accuracies. Inconsistency is only in case of ensemble classifiers. I have even set random_state to 0.

bag_classifier = BaggingClassifier(n_estimators=10,random_state=0)
bag_classifier.fit(train_arrays,train_labels)   
bag_predict = bag_classifier.predict(test_arrays)  
bag_accuracy = bag_classifier.score(test_arrays,test_labels)   
bag_cm = confusion_matrix(test_labels,bag_predict)   
print("The Bagging Classifier accuracy is : " ,bag_accuracy)   
print("The Confusion Matrix is ")  
print(bag_cm)
avinash
  • 155
  • 1
  • 2
  • 8
  • 1
    post your code also. – Saurabh Agrawal Dec 14 '17 at 09:59
  • 1
    Look for `random_state` param in all the methods or classes you use and set that. Also, please post the complete code. – Vivek Kumar Dec 14 '17 at 10:07
  • 1
    Please see these questions which are duplicate to yours: [Question1](https://stackoverflow.com/questions/28673442/getting-different-result-each-time-i-run-a-linear-regression-using-scikit) and [Question2](https://stackoverflow.com/questions/43901083/sgdclassifier-giving-different-accuracy-each-time-for-text-classification) – Vivek Kumar Dec 14 '17 at 10:11

1 Answers1

0

You will normally find different results for same model because every time when the model is executed during training, the train/test split is random. You can reproduce the same results by giving the seed value to the train/test split.

train, test = train_test_split(your data , test_size=0.3,  random_state=57)

Keep the same random_state value in each turn of training.

Stupid420
  • 1,347
  • 3
  • 19
  • 44