Search code examples
pythonmachine-learningscikit-learnlogistic-regression

Setting exact number of iterations for Logistic regression in python


I'm creating a model to perform Logistic regression on a dataset using Python. This is my code:

from sklearn import linear_model
my_classifier2=linear_model.LogisticRegression(solver='lbfgs',max_iter=10000)

Now, according to Sklearn doc page, max_iter is maximum number of iterations taken for the solvers to converge. How do I specifically state that I need 'N' number of iterations ?

Any kind of help would be really appreciated.


Solution

  • I’m not sure, but, Do you want to know the optimal number of iterations for your model? If so, you are better off utilizing GridSearchCV that scan tune hyper parameter like max_iter. Briefly,

    1. Split your data into two groups: train/test data with train_test_split or KFold that can be imported from sklean
    2. Set your parameter, for instance para=[{‘max_iter’:[1,10,100,100]}]
    3. Instance, for example clf=GridSearchCV(LogisticRegression, param_grid=para, cv=5, scoring=‘r2’)
    4. Implement with using train data like this: clf.fit(x_train, y_train)

    You can also fetch the best number of iterations with RandomizedSearchCV or BayesianOptimization.