this is my first question ever here I hope I am doing this right,
I was working on titanic dataset which is popular on kaggle, this tutarial if u wanna check A Data Science Framework: To Achieve 99% Accuracy
the part 5.2, it teaches how to gridsearch and tune hyper-parameters. let me share related codes with you before I get spesific on my question;
this is tuning the model with GridSearchCV:
cv_split = model_selection.ShuffleSplit(n_splits = 10, test_size = .3, train_size = .6, random_state = 0)
#cv_split = model_selection.KFold(n_splits=10, shuffle=False, random_state=None)
param_grid = {'criterion': ['gini', 'entropy'],
'splitter': ['best', 'random'], #splitting methodology; two supported strategies - default is best
'max_depth': [2,4,6,8,10,None], #max depth tree can grow; default is none
'min_samples_split': [2,5,10,.03,.05], #minimum subset size BEFORE new split (fraction is % of total); default is 2
'min_samples_leaf': [1,5,10,.03,.05], #minimum subset size AFTER new split split (fraction is % of total); default is 1
'max_features': [None, 'auto'], #max features to consider when performing split; default none or all
'random_state': [0] }
tune_model = model_selection.GridSearchCV(tree.DecisionTreeClassifier(), param_grid=param_grid, scoring = 'roc_auc', return_train_score = True ,cv = cv_split)
tune_model.fit(data1[data1_x_bin], data1[Target])`
tune_model.best_params_
result is:
{'criterion': 'gini',
'max_depth': 4,
'max_features': None,
'min_samples_leaf': 5,
'min_samples_split': 2,
'random_state': 0,
'splitter': 'best'}
and acording to code, training and test accuracy sopposed to be like that when tuned with those:
print(tune_model.cv_results_['mean_train_score'][tune_model.best_index_], tune_model.cv_results_['mean_test_score'][tune_model.best_index_])
output of this: 0.8924916598172832 0.8767742588186237
out of curiousity, I wanted to make my own DecisionTreeClassifier() with parameters I got from GridSearchCV,
dtree = tree.DecisionTreeClassifier(criterion = 'gini',max_depth = 4,max_features= None, min_samples_leaf= 5, min_samples_split= 2,random_state = 0, splitter ='best')
results = model_selection.cross_validate(dtree, data1[data1_x_bin], data1[Target],return_train_score = True, cv = cv_split)
Same hyperparameters, same cross validation dataframes, different results. Why?
print(results['train_score'].mean(), results['test_score'].mean())
0.8387640449438202 0.8227611940298509
that one was tune_model results:
0.8924916598172832 0.8767742588186237
difference is not even small. Both results should be same if u ask me,
I don'T understand what is different? what is different so results are different?
I tried cross validating with k-fold instead of shufflesplit,
in both scenarios I tried with different random_state values, tried also random_state = None,
still different results.
can someone explain the difference please?
edit: btw, I also wanted to check test sample results:
dtree.fit(data1[data1_x_bin],data1[Target])
dtree.score(test1_x_bin,test1_y), tune_model.score(test1_x_bin,test1_y)
output: (0.8295964125560538, 0.9033059266872216)
same models(decisiontreeclassifier), same hyper-parameters, very different results
( obviously they are not same models but I can't see how and why )