Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

Perceptron

Perceptron is a linear classifier backed by SGDClassifier. It uses perceptron loss with a constant learning rate and supports optional regularization, early stopping, and incremental fitting.

How it works

Perceptron is a linear classifier implemented through SGDClassifier with perceptron loss and a constant learning rate. It learns coefficients and, when enabled, an intercept by repeatedly passing over training data until the iteration limit or stopping criterion is reached.

Syntax

class sklearn.linear_model.Perceptron(*, penalty=None, alpha=0.0001, l1_ratio=0.15, fit_intercept=True, max_iter=1000, tol=0.001, shuffle=True, verbose=0, eta0=1.0, n_jobs=None, random_state=0, early_stopping=False, validation_fraction=0.1, n_iter_no_change=5, class_weight=None, warm_start=False)

Parameters

NameTypeDescription
penaltystr or None, default=NoneRegularization choice: None, l2, l1, or elasticnet.
alphafloat, default=0.0001Multiplier for the regularization term.
l1_ratiofloat, default=0.15Elastic-net mixing value between 0 and 1.
fit_interceptbool, default=TrueWhether to estimate an intercept.
max_iterint, default=1000Maximum passes over the training data for fit.
tolfloat or None, default=0.001Stopping threshold based on loss improvement.
shufflebool, default=TrueWhether to shuffle training data each epoch.
verboseint, default=0Verbosity level.
eta0float, default=1.0Constant learning-rate multiplier.
n_jobsint or None, default=NoneNumber of CPU cores used for prediction, where applicable.
random_stateint, RandomState instance, or None, default=0Controls pseudo-randomness.
early_stoppingbool, default=FalseWhether to use an internal validation split for early stopping.

Returns

A fitted Perceptron estimator from fit; predictions or scores from its estimator methods.

Key points

  • Perceptron is a linear classifier for classification tasks.
  • Its implementation delegates to SGDClassifier.
  • The class fixes perceptron loss and constant learning rate.
  • max_iter specifies the maximum number of training-data passes.
  • tol provides an optional stopping criterion.
  • penalty selects the regularization method.

Common mistakes

  • max_iter does not limit iterations performed by partial_fit.
  • Disabling fit_intercept requires already centered input data.
  • tol controls stopping based on insufficient loss improvement, not a fixed accuracy target.

Example: Train and predict with Perceptron

from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron

X, y = load_iris(return_X_y=True)
model = Perceptron(random_state=0)
model.fit(X, y)
print(model.predict(X[:3]))

This trains a perceptron classifier on the Iris data and predicts the first three samples.

When to use it

Linear baseline

Build a fast linear baseline for supervised classification.

Incremental learning

Train on data that arrives in batches.

Large feature spaces

Classify sparse, high-dimensional feature vectors.

Interview questions

QWhat is Perceptron?

Perceptron is a linear classification algorithm. Its implementation fixes SGDClassifier to perceptron loss and a constant learning rate.

Follow-up: How can it be expressed with SGDClassifier? Use SGDClassifier(loss="perceptron", eta0=1, learning_rate="constant", penalty=None).

QHow does Perceptron stop training?

max_iter is the maximum number of passes over the training data, also called epochs, when using fit. tol can stop training when the loss improvement is too small.

Follow-up: Does max_iter constrain partial_fit? No; the documentation says it affects fit, not partial_fit.

QHow are regularization settings configured?

The estimator supports regularization through penalty, with options such as l2, l1, and elasticnet. alpha scales the regularization term, while l1_ratio controls the L1/L2 mixture for elastic net.

Follow-up: What do the extreme l1_ratio values mean? Zero corresponds to L2 and one corresponds to L1.

  • SGDClassifier
  • LogisticRegression
  • RidgeClassifier
  • PassiveAggressiveClassifier

Source: Perceptron — scikit-learn 1.9.1 documentation. Checked 2026-10-15.