Skip to Content
OchreML
How-To Guides

Out-of-Core Streaming

Train on datasets larger than RAM (up to 100M+ rows) in constant memory using partial_fit.

When a dataset does not fit in memory, OchreML's streaming partial_fit API processes data in fixed-size chunks with constant RAM usage (VmRSS stays flat across millions of rows). (^▽^)

Estimators & Transformers Supporting partial_fit

Estimators

  • Naive Bayes: GaussianNB, MultinomialNB, BernoulliNB, ComplementNB
  • Classical Neural Networks: MLPClassifier, MLPRegressor
  • Clustering: MiniBatchKMeans

Preprocessing & Feature Extraction

  • Scalers: StandardScaler, MinMaxScaler, MaxAbsScaler
  • Text Feature Extraction: HashingVectorizer (stateless, zero vocabulary memory)

Streaming Classification Example

For classifiers, pass the complete list of target labels via classes= on the first partial_fit call so the model allocates class statistics upfront:

import csv
import ochreml
from ochreml.preprocessing import StandardScaler
from ochreml.naive_bayes import GaussianNB

scaler = StandardScaler()
clf = GaussianNB()

CLASSES = [0, 1, 2]
CHUNK_SIZE = 4096

def iter_chunks(csv_path: str, chunk_size: int = CHUNK_SIZE):
    X_buf, y_buf = [], []
    with open(csv_path, "r", encoding="utf-8", newline="") as f:
        reader = csv.DictReader(f)
        for row in reader:
            y_buf.append(int(row["target"]))
            X_buf.append([float(row[c]) for c in reader.fieldnames if c != "target"])
            if len(X_buf) >= chunk_size:
                yield X_buf, y_buf
                X_buf, y_buf = [], []
    if X_buf:
        yield X_buf, y_buf

# Pass 1: Fit streaming scaler in constant RAM
for X_chunk, _ in iter_chunks("large_dataset.csv"):
    scaler.partial_fit(X_chunk)

# Pass 2: Stream scaled chunks into the classifier
for idx, (X_chunk, y_chunk) in enumerate(iter_chunks("large_dataset.csv")):
    X_scaled = scaler.transform(X_chunk)
    if idx == 0:
        clf.partial_fit(X_scaled, y_chunk, classes=CLASSES)
    else:
        clf.partial_fit(X_scaled, y_chunk)

ochreml.save(scaler, "scaler.dit")
ochreml.save(clf, "gnb_stream.dit")

Streaming Clustering & Regression

MiniBatchKMeans

from ochreml.cluster import MiniBatchKMeans

kmeans = MiniBatchKMeans(n_clusters=8, batch_size=2048, random_state=42)
for X_chunk, _ in iter_chunks("embeddings.csv", chunk_size=2048):
    kmeans.partial_fit(X_chunk)

MLPRegressor

from ochreml.neural_network import MLPRegressor

reg = MLPRegressor(hidden_layer_sizes=(64, 32), learning_rate_init=0.001, random_state=42)
for X_chunk, y_chunk in iter_chunks("telemetry.csv", chunk_size=2048):
    reg.partial_fit(X_chunk, y_chunk)

Zero-Code Streaming via CLI

You can also stream CSV/TSV files directly from the command line using ochreml train --stream, which automatically discovers classes on a lightweight first pass and streams chunks of 2,048 rows into any partial_fit-capable estimator:

ochreml train \
  --data large_dataset.csv \
  --target label \
  --model GaussianNB \
  --stream \
  --out model.dit

Streaming via ochreml train --stream currently supports CSV and TSV files. For Parquet datasets, stream row groups in Python using pyarrow.parquet.ParquetFile.iter_batches() and feed each batch to partial_fit().

On this page