SKLearner Home | About | Contact | Examples

Configure LogisticRegression "max_iter" Parameter

The max_iter parameter in scikit-learn’s LogisticRegression controls the maximum number of iterations taken for the solvers to converge.

Logistic Regression is a linear model used for binary classification problems. It predicts the probability of a binary outcome using a logistic function.

The max_iter parameter determines how many iterations the solver will run before stopping. If the solver does not converge within the specified number of iterations, it stops and reports a convergence warning.

The default value for max_iter is 100.

Common values for max_iter range from 100 to 1000, depending on the complexity of the data and the need for model convergence.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=0, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train with different max_iter values
max_iter_values = [50, 100, 200, 500]
accuracies = []

for max_iter in max_iter_values:
    lr = LogisticRegression(max_iter=max_iter, random_state=42)
    lr.fit(X_train, y_train)
    y_pred = lr.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)
    print(f"max_iter={max_iter}, Accuracy: {accuracy:.3f}")

Running the example gives an output like:

max_iter=50, Accuracy: 0.770
max_iter=100, Accuracy: 0.770
max_iter=200, Accuracy: 0.770
max_iter=500, Accuracy: 0.770

The key steps in this example are:

  1. Generate a synthetic binary classification dataset.
  2. Split the data into train and test sets.
  3. Train LogisticRegression models with different max_iter values.
  4. Evaluate the accuracy of each model on the test set.

Some tips and heuristics for setting max_iter:

Issues to consider:



See Also