logistic regression
model accuracy
Scikit-learn
Python
machine learning techniques

How to increase the model accuracy of logistic regression in Scikit python?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Improving logistic regression accuracy in scikit-learn is usually less about finding one magic parameter and more about fixing the training pipeline. Logistic regression is a linear model, so feature scaling, class balance, target quality, and reasonable regularization matter more than many beginners expect.

Start With a Proper Baseline

A strong baseline uses a pipeline so preprocessing and model fitting stay coupled during cross-validation:

python
1from sklearn.datasets import load_breast_cancer
2from sklearn.linear_model import LogisticRegression
3from sklearn.model_selection import cross_val_score
4from sklearn.pipeline import make_pipeline
5from sklearn.preprocessing import StandardScaler
6
7X, y = load_breast_cancer(return_X_y=True)
8
9model = make_pipeline(
10    StandardScaler(),
11    LogisticRegression(max_iter=5000)
12)
13
14scores = cross_val_score(model, X, y, cv=5, scoring="accuracy")
15print(scores.mean())

The StandardScaler is important because logistic regression is sensitive to feature scale. Without scaling, optimization may converge slowly and large-magnitude features may dominate the learned coefficients.

Tune Regularization Instead of Trusting Defaults

The scikit-learn logistic regression implementation uses regularization by default. The key hyperparameter is C, which is the inverse of regularization strength.

  • Smaller C means stronger regularization.
  • Larger C means weaker regularization.

A quick grid search is often the simplest improvement:

python
1from sklearn.datasets import load_breast_cancer
2from sklearn.linear_model import LogisticRegression
3from sklearn.model_selection import GridSearchCV
4from sklearn.pipeline import make_pipeline
5from sklearn.preprocessing import StandardScaler
6
7X, y = load_breast_cancer(return_X_y=True)
8
9pipeline = make_pipeline(
10    StandardScaler(),
11    LogisticRegression(max_iter=5000)
12)
13
14grid = GridSearchCV(
15    pipeline,
16    {
17        "logisticregression__C": [0.01, 0.1, 1, 10, 100],
18        "logisticregression__solver": ["lbfgs", "liblinear"],
19    },
20    cv=5,
21    scoring="accuracy",
22)
23
24grid.fit(X, y)
25print(grid.best_params_)
26print(grid.best_score_)

This is more defensible than changing random knobs without measurement.

Improve Features, Not Just the Model

Because logistic regression learns a linear boundary, the quality of the features strongly limits the ceiling. Accuracy often improves when you:

  • Remove irrelevant or noisy columns.
  • Encode categories correctly.
  • Add interaction terms.
  • Handle missing values consistently.
  • Standardize numeric columns.

For some problems, polynomial features help create a more expressive boundary:

python
1from sklearn.datasets import make_classification
2from sklearn.linear_model import LogisticRegression
3from sklearn.model_selection import train_test_split
4from sklearn.pipeline import make_pipeline
5from sklearn.preprocessing import PolynomialFeatures, StandardScaler
6
7X, y = make_classification(
8    n_samples=1000,
9    n_features=4,
10    n_informative=3,
11    n_redundant=0,
12    random_state=0,
13)
14
15X_train, X_test, y_train, y_test = train_test_split(
16    X, y, test_size=0.2, random_state=0
17)
18
19model = make_pipeline(
20    PolynomialFeatures(degree=2, include_bias=False),
21    StandardScaler(),
22    LogisticRegression(max_iter=5000)
23)
24
25model.fit(X_train, y_train)
26print(model.score(X_test, y_test))

This can improve results when the original feature space is too simple, but it can also overfit if you add too many derived terms.

Watch for Class Imbalance

Accuracy can be misleading when one class dominates the dataset. In those cases, a model can score well by favoring the majority class while performing badly on the minority class.

A better baseline for imbalanced data is:

python
1from sklearn.metrics import classification_report
2from sklearn.model_selection import train_test_split
3
4X_train, X_test, y_train, y_test = train_test_split(
5    X, y, test_size=0.2, stratify=y, random_state=0
6)
7
8model = make_pipeline(
9    StandardScaler(),
10    LogisticRegression(max_iter=5000, class_weight="balanced")
11)
12
13model.fit(X_train, y_train)
14pred = model.predict(X_test)
15
16print(classification_report(y_test, pred))

If recall or precision matters more than raw accuracy, optimize for the metric that matches the real problem rather than forcing everything into one accuracy number.

Check Data Quality Before Tuning Harder

Some accuracy problems are not model problems at all. Logistic regression will struggle if:

  • Labels are wrong.
  • Important features are missing.
  • Train and test data come from different distributions.
  • Missing values were handled inconsistently.
  • Data leakage inflated earlier expectations.

Before trying more complex solvers or larger feature spaces, confirm that the dataset itself supports the task you want the model to solve.

Common Pitfalls

  • Training on unscaled numeric features and then blaming the algorithm.
  • Ignoring convergence warnings because max_iter was too small.
  • Optimizing only accuracy on an imbalanced dataset.
  • Using logistic regression on a strongly nonlinear problem without feature engineering.
  • Tuning the model before checking labels, splits, and data quality.

Summary

  • Scale features before fitting logistic regression.
  • Tune C, solver choice, and class weighting with cross-validation.
  • Improve feature quality and add interactions when the problem is not linearly separable enough.
  • Use evaluation metrics that match the business goal, not just raw accuracy.
  • Fix data quality and convergence issues before reaching for a more complex model.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.