Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Linear Model Perceptron

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Classification, Linear_Models
Last Updated 2026-02-08 16:00 GMT

Overview

The Perceptron is a fundamental online linear classifier implemented as a special case of logistic regression with hinge loss and fixed learning rate.

Description

This implementation views the Perceptron as logistic regression with specific parameter choices: Hinge loss with threshold 0, SGD optimizer with learning rate 1.0 for both weights and intercept. It inherits all functionality from LogisticRegression, making it a simple but effective baseline classifier. The model updates weights only when misclassifications occur, following the classic perceptron update rule. Optional L2 regularization and gradient clipping are available for improved stability.

Usage

Use Perceptron as a fast, simple baseline for binary classification problems. It's particularly effective for linearly separable data and works well with preprocessing like StandardScaler. The perceptron converges quickly on simple problems but may struggle with noisy, non-linearly separable data where more sophisticated methods would be preferable.

Code Reference

Source Location

Signature

class Perceptron(LogisticRegression):
    def __init__(
        self,
        l2=0.0,
        clip_gradient=1e12,
        initializer: optim.initializers.Initializer | None = None,
    ):
        super().__init__(
            optimizer=optim.SGD(1),
            intercept_lr=1,
            loss=optim.losses.Hinge(threshold=0.0),
            l2=l2,
            clip_gradient=clip_gradient,
            initializer=initializer,
        )

Import

from river import linear_model

I/O Contract

Parameters

Parameter Type Default Description
l2 float 0.0 L2 regularization to push weights towards 0
clip_gradient float 1e12 Absolute value to clip gradients
initializer Initializer or None None Weight initialization scheme

Attributes

Attribute Type Description
weights dict Current weight vector (inherited)

Input/Output

Method Input Output
learn_one x: dict, y: bool None
predict_proba_one x: dict dict[bool, float]
predict_one x: dict bool

Usage Examples

from river import datasets
from river import evaluate
from river import linear_model as lm
from river import metrics
from river import preprocessing as pp

dataset = datasets.Phishing()

model = pp.StandardScaler() | lm.Perceptron()

metric = metrics.Accuracy()

evaluate.progressive_val_score(dataset, model, metric)
# Accuracy: 85.84%

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment