import numpy as np
from pprint import pprint
from crabbymetrics import FTRL
np.set_printoptions(precision=4, suppress=True)FTRL Example
This page mirrors examples/ftrl_example.py.
Warning
The current Python wrapper performs one aggregate batch update from a fixed random FTRL state. It has no intercept, epochs, convergence criterion, or persistent online update API. This example displays that update; it is not evidence of a converged classifier. See the FTRL API page for the exact equations and limitations.
1 Run The Experimental FTRL Update
rng = np.random.default_rng(6)
n = 1000
k = 5
beta = np.array([0.8, -1.2, 0.4, 0.0, 0.6])
x = rng.normal(size=(n, k))
logits = x @ beta
probs = 1.0 / (1.0 + np.exp(-logits))
y = rng.binomial(1, probs).astype(np.int32)
model = FTRL(alpha=0.1, beta=1.0, l1_ratio=1.0, l2_ratio=1.0)
model.fit(x, y)
print("true coef:", beta)
pprint(model.summary())true coef: [ 0.8 -1.2 0.4 0. 0.6]
{'alpha': 0.1,
'beta': 1.0,
'coef': array([ 0.0986, -0.0992, 0.0974, 0.0829, 0.0978]),
'coef_se': None,
'inference_available': False,
'l1_ratio': 1.0,
'l2_ratio': 1.0}