Implementation:Scikit learn Scikit learn Perceptron
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Classification |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for performing linear classification using the perceptron algorithm, provided by scikit-learn.
Description
The Perceptron class implements a linear perceptron classifier. It is a wrapper around SGDClassifier with the loss fixed to "perceptron" and the learning rate set to "constant". It supports L2, L1, and Elastic Net regularization, partial fitting for online learning, and class weighting for imbalanced datasets.
Usage
Use this classifier for simple, fast linear classification tasks, especially when you need an online learning algorithm or a baseline linear classifier. It is particularly suitable for large-scale sparse datasets.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/linear_model/_perceptron.py
Signature
class Perceptron(BaseSGDClassifier):
def __init__(
self,
*,
penalty=None,
alpha=0.0001,
l1_ratio=0.15,
fit_intercept=True,
max_iter=1000,
tol=1e-3,
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,
):
Import
from sklearn.linear_model import Perceptron
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| penalty | str or None | No | Regularization term: 'l2', 'l1', 'elasticnet', or None (default None) |
| alpha | float | No | Regularization constant (default 0.0001) |
| l1_ratio | float | No | Elastic Net mixing parameter (default 0.15) |
| fit_intercept | bool | No | Whether to fit the intercept (default True) |
| max_iter | int | No | Maximum number of passes over training data (default 1000) |
| tol | float or None | No | Stopping criterion tolerance (default 1e-3) |
| shuffle | bool | No | Whether to shuffle training data each epoch (default True) |
| random_state | int, RandomState or None | No | Random seed (default 0) |
Outputs
| Name | Type | Description |
|---|---|---|
| coef_ | ndarray of shape (1, n_features) or (n_classes, n_features) | Weight vectors |
| intercept_ | ndarray of shape (1,) or (n_classes,) | Intercept (bias) terms |
| n_iter_ | int | Actual number of iterations to reach convergence |
Usage Examples
Basic Usage
from sklearn.linear_model import Perceptron
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=100, random_state=0)
clf = Perceptron(random_state=0)
clf.fit(X, y)
print(clf.score(X, y))
print(clf.predict([[0.5] * 20]))