What is C parameter in sklearn Logistic Regression?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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
Cmeans strong penalty - large
Cmeans 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:
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:
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:
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
Cmeans stronger regularization. - Tuning
Cwithout scaling features first. - Changing
Cwithout considering the penalty type and solver compatibility. - Treating a very large
Cas automatically better because it fits training data more closely. - Assuming the default
C=1.0is optimal for every dataset.
Summary
- '
Cis the inverse of regularization strength in scikit-learn logistic regression.' - Smaller
Cmeans stronger regularization and usually smaller coefficients. - Larger
Cmeans weaker regularization and more freedom to fit the training data. - The impact of
Cdepends on feature scaling, penalty choice, and solver. - In practice,
Cshould usually be tuned with validation rather than chosen arbitrarily.
Related reading
- What is causing large jumps in training accuracy and loss between epochs?
- What is cross-entropy?
- What is Depth of a convolutional neural network?
- What is difference between tf.truncated_normal and tf.random_normal?
- What is conftest.py for in Pytest?
- What is double colon in Python when subscripting sequences?
- What is difference between tf.truncated_normal and tf.random_normal?
- What is difference frozen_inference_graph.pb and saved_model.pb?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.