logistic regression
scikit-learn
sklearn
machine learning
logisticregression

Kernel in a logistic regression model LogisticRegression scikit-learn sklearn

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

sklearn.linear_model.LogisticRegression is a linear classifier. It does not accept a kernel parameter the way SVC does, so there is no built-in “kernel logistic regression” switch in scikit-learn’s standard logistic regression estimator.

That does not mean you are stuck with only straight-line decision boundaries. It means you create non-linear decision boundaries by transforming the features before they reach logistic regression.

Why LogisticRegression Has No Kernel Argument

A kernel method works by replacing the original feature representation with an implicit similarity computation in another space. In scikit-learn, that pattern is exposed directly in estimators such as SVC(kernel="rbf").

LogisticRegression, by contrast, is implemented as a linear model over the feature matrix you give it. So if you want non-linearity, you must change the input features rather than expecting the classifier to do kernel expansion internally.

Use Polynomial Features for a Simple Non-Linear Boundary

One practical option is explicit feature expansion with PolynomialFeatures.

python
1from sklearn.datasets import make_moons
2from sklearn.pipeline import make_pipeline
3from sklearn.preprocessing import PolynomialFeatures
4from sklearn.linear_model import LogisticRegression
5
6X, y = make_moons(noise=0.15, random_state=42)
7
8model = make_pipeline(
9    PolynomialFeatures(degree=3, include_bias=False),
10    LogisticRegression(max_iter=2000)
11)
12
13model.fit(X, y)
14print(model.score(X, y))

This is still logistic regression, but now the classifier sees richer feature interactions that allow curved decision boundaries.

Use Kernel Approximation When You Want RBF-Like Behavior

If your mental model is “I want something closer to an RBF kernel,” scikit-learn offers approximation transformers such as RBFSampler and Nystroem.

python
1from sklearn.datasets import make_circles
2from sklearn.kernel_approximation import RBFSampler
3from sklearn.linear_model import LogisticRegression
4from sklearn.pipeline import make_pipeline
5from sklearn.preprocessing import StandardScaler
6
7X, y = make_circles(noise=0.1, factor=0.4, random_state=42)
8
9model = make_pipeline(
10    StandardScaler(),
11    RBFSampler(gamma=1.0, n_components=300, random_state=42),
12    LogisticRegression(max_iter=2000)
13)
14
15model.fit(X, y)
16print(model.score(X, y))

This is not true built-in kernel logistic regression, but it is often the closest workflow inside scikit-learn when you want logistic loss with non-linear transformed features.

When SVC May Be the Better Tool

Sometimes the honest answer is that you should use a support vector classifier if you specifically want a native kernelized classifier.

python
from sklearn.svm import SVC

model = SVC(kernel="rbf", gamma="scale")

That changes the learning objective, so it is not interchangeable with logistic regression. But if the main goal is a strong non-linear classifier and not calibrated log-odds interpretation, SVC may be the cleaner choice.

Keep the Reason for Logistic Regression in Mind

Logistic regression remains attractive because it gives:

  • simple linear decision logic in the transformed feature space
  • probability estimates
  • interpretable coefficients in some settings
  • fast training on many tabular problems

If those properties matter, feature engineering plus logistic regression can be a better fit than switching models immediately.

Regularization becomes more important once you expand features. If you move from a small raw feature space to hundreds of polynomial or sampled kernel features, tune C carefully and validate on held-out data instead of trusting the default. Otherwise the model may appear to solve the training set while generalizing poorly.

Common Pitfalls

  • Looking for a kernel= parameter on LogisticRegression and assuming you missed an option.
  • Assuming polynomial expansion and true kernel methods are exactly the same thing in cost and behavior.
  • Forgetting feature scaling before kernel approximation methods.
  • Comparing kernelized SVC and logistic regression without noticing that they optimize different objectives.
  • Adding huge feature expansions without regularization or validation, which leads to overfitting.

Summary

  • Scikit-learn LogisticRegression does not support kernels directly.
  • To get non-linear behavior, transform the features before fitting logistic regression.
  • 'PolynomialFeatures is a simple explicit approach.'
  • 'RBFSampler or Nystroem can approximate kernel-style behavior while keeping logistic regression as the classifier.'
  • If you specifically need a native kernel classifier, SVC is often the more direct tool.

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.