# Do i need to use hyperparamters from Gridsearch to train on WHOLE training set to get final model?

I just want to make sure i am on the right lines so please correct me if wrong. I am testing which hyperparmets are best for logisitic regession on my data X, y where X is featrues and y is target. X, y are made from my training set. I also have a test set.

from sklearn.linear_model import LogisticRegression

# split train into target and features
y = Train['target']
X = Train.drop(['target'], axis = 1)
X = pd.get_dummies(X)
#split test data into target and features

y_test = Test['target']
X_test = Test.drop(['target'], axis = 1)
X_test = pd.get_dummies(X_test)

logistic = LogisticRegression()  # initialize the model
# Create regularization penalty space

param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000] }

clf=GridSearchCV(logistic,param_grid=param_grid,cv=5)

best_model = clf.fit(X, y)# View best hyperparameters
print('Best Penalty:', best_model.best_estimator_.get_params()['penalty'])
print('Best C:', best_model.best_estimator_.get_params()['C']) #


I will now use these hyper parameters and 'train' it on my training data. Just so i'm sure when we say train do i then take best_model and train on the whole X data. Then i use my X_test which is my hold out data on this new trained model:

bestLog=best_model.best_estimator_
trained_model=bestLog.fit(X,y)
predicted=trained_model.predict(X_test)


then use this output above as my final model to test?

• Yes, this is how you (generally) produce the final model, but GridSearchCV by default has refit=True, and in this case you can, e.g., call clf.predict. – Ben Reiniger Feb 9 at 2:22