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 hhave 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] }


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 e.g.:


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


Theoretically, as in the linked post in @Sycorax's comment, you'll train with the whole training set, obtain a model and test on new data. This is advised, since you don't waste data, so your approach is correct. If you don't re-train with the whole training data, you'd have to choose one fold from the CV loop and train with it, which doesn't make much sense given that validation data is wasted.

In your situation, you don't need to re-train because it is already done by sklearn library, using the parameter refit, whose default value is True:

refit : boolean, string, or callable, default=True

Refit an estimator using the best found parameters on the whole dataset.


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