Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 819
Rohan Dsouza Avatar asked Oct 17 '25 09:10

Rohan Dsouza


1 Answers

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.

like image 164
Genzo Ito Avatar answered Oct 19 '25 21:10

Genzo Ito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!