Logistic Regression
sklearn
Machine Learning
C parameter
Python

What is C parameter in sklearn Logistic Regression?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In scikit-learn LogisticRegression, C controls the strength of regularization. More precisely, it is the inverse of regularization strength: a smaller C means stronger regularization, and a larger C means weaker regularization.

Regularization is the real concept behind C

Logistic regression does not just fit coefficients to match the training labels. It usually also applies a penalty that discourages overly large coefficients. That penalty helps reduce overfitting.

In scikit-learn, C is how you tune that penalty:

  • small C means strong penalty
  • large C means weak penalty

So C is not "model complexity" directly. It is a knob that changes how aggressively the optimizer shrinks the coefficients.

A small C shrinks weights more aggressively

Here is a simple example:

python
1from sklearn.datasets import make_classification
2from sklearn.linear_model import LogisticRegression
3
4X, y = make_classification(n_samples=200, n_features=10, random_state=0)
5
6model_strong = LogisticRegression(C=0.1, max_iter=1000)
7model_weak = LogisticRegression(C=10.0, max_iter=1000)
8
9model_strong.fit(X, y)
10model_weak.fit(X, y)
11
12print(model_strong.coef_)
13print(model_weak.coef_)

The model with C=0.1 typically has smaller-magnitude coefficients because the regularization is stronger.

C is inverse regularization strength

It is easy to remember the direction incorrectly, because many textbooks talk about regularization with a parameter often called lambda, where larger means stronger regularization.

Scikit-learn uses the inverse:

  • stronger regularization = smaller C
  • weaker regularization = larger C

That is the single most important fact to remember about this parameter.

The effect depends on the penalty type

C works together with the penalty choice:

  • 'penalty="l2" shrinks weights smoothly'
  • 'penalty="l1" can drive some weights exactly to zero'

Example:

python
1from sklearn.linear_model import LogisticRegression
2
3model = LogisticRegression(
4    penalty="l1",
5    solver="liblinear",
6    C=0.5,
7    max_iter=1000
8)

Here, C still controls overall regularization strength, but the shape of the penalty is determined by penalty.

Feature scaling matters when tuning C

Regularization acts on the coefficients, so feature scale affects how that penalty is felt. If one feature is on a huge numeric scale and another is tiny, the meaning of a given C becomes less stable.

That is why logistic regression is often used inside a pipeline with scaling:

python
1from sklearn.pipeline import make_pipeline
2from sklearn.preprocessing import StandardScaler
3from sklearn.linear_model import LogisticRegression
4
5model = make_pipeline(
6    StandardScaler(),
7    LogisticRegression(C=1.0, max_iter=1000)
8)

If you tune C without scaling, the results can be harder to interpret.

Tune C with validation, not intuition alone

There is no universally correct C value. The right value depends on:

  • amount of data
  • noise level
  • feature dimensionality
  • feature scaling
  • chosen penalty and solver

In practice, you usually tune C with cross-validation rather than guessing it by feel.

Solver compatibility still matters

Not every solver supports every penalty. If you change C while also changing penalty, confirm that the selected solver actually supports that combination before drawing conclusions from the results.

Common Pitfalls

  • Forgetting that smaller C means stronger regularization.
  • Tuning C without scaling features first.
  • Changing C without considering the penalty type and solver compatibility.
  • Treating a very large C as automatically better because it fits training data more closely.
  • Assuming the default C=1.0 is optimal for every dataset.

Summary

  • 'C is the inverse of regularization strength in scikit-learn logistic regression.'
  • Smaller C means stronger regularization and usually smaller coefficients.
  • Larger C means weaker regularization and more freedom to fit the training data.
  • The impact of C depends on feature scaling, penalty choice, and solver.
  • In practice, C should usually be tuned with validation rather than chosen arbitrarily.

Course illustration
Course illustration

All Rights Reserved.