Skip to content

Quick start

Streaming updates

import numpy as np
from renewable_huber import RenewableHuberRegressor

model = RenewableHuberRegressor(penalty="l1", lambda_scale=0.5)

for X_batch, y_batch in data_stream:
    model.partial_fit(X_batch, y_batch)

print(model.coef_, model.intercept_)
prediction = model.predict(X_test)
model.save("checkpoints/model.npz")

restored = RenewableHuberRegressor.load("checkpoints/model.npz")
assert np.allclose(prediction, restored.predict(X_test))

data_stream is any iterable of (X_batch, y_batch) pairs; X_test is a feature matrix with the same columns. The model keeps only its coefficients and an accumulated information matrix, so memory use does not grow with the length of the stream.

pandas, scikit-learn and sample weights

The scikit-learn adapter works directly with Pipeline, clone and GridSearchCV:

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from renewable_huber.integrations.sklearn import SklearnRenewableHuberRegressor

pipeline = make_pipeline(StandardScaler(), SklearnRenewableHuberRegressor())
pipeline.fit(X_train, y_train)
prediction = pipeline.predict(X_test)

numpy.ndarray and tabular objects with .to_numpy() (such as pandas.DataFrame / Series) can be used directly as input. When a DataFrame has string column names, later DataFrame batches and predictions must keep the same names in the same order. fit, partial_fit and score support a non-negative sample_weight:

model.partial_fit(X_batch, y_batch, sample_weight=batch_weights)
weighted_r2 = model.score(X_test, y_test, sample_weight=test_weights)

Weights have frequency-weight semantics: an integer weight is equivalent to repeating the observation, and an all-zero batch is rejected. A SciPy sparse matrix is never densified behind your back; assess the memory cost and call X.toarray() explicitly.

Streaming semantics

fit(X, y) resets the model and then processes a single batch; a genuine streaming workflow should call partial_fit(X_batch, y_batch) repeatedly.

PyTorch input is detached first, and this package is not an autograd layer; the TensorFlow backend supports eager execution only and cannot be placed directly inside a tf.function. Streaming updates use the previous batch's coefficients and information matrix, so the way batches are split and the order of the data are part of the computation; results are not guaranteed to be bit-identical to a whole-data fit or to a different permutation.

Continuing a stream from a saved state is covered in Checkpoints; running on another engine or device is covered in Backends.