Skip to main content

milgboost

Multiple Instance Learning for Gradient Boosting Models.

MIL is a weakly supervised learning paradigm where labels are available for bags (groups of instances) rather than individual instances. milgboost brings MIL to gradient boosting by wrapping LightGBM and XGBoost with custom differentiable objectives — currently the LogSumExp Binary Cross-Entropy (LSE-BCE) loss, a smooth approximation of the max-instance MIL loss.

Installation

uv add milgboost

Extra options

Install with a specific boosting backend:

uv add milgboost[xgboost-cpu]
uv add milgboost[xgboost] # GPU enabled
uv add milgboost[lightgbm]
uv add milgboost[xgboost-cpu,lightgbm]

Module overview

Module Description
milgboost.types Bag / LabeledBag dataclasses + array↔bag conversion helpers
milgboost.datasets make_mil_data() — synthetic MIL data generator
milgboost.model.base BaseMILModel abstract class (fit / predict / predict_proba)
milgboost.model.xgboost XGBoostMILModel — XGBoost-backed MIL classifier
milgboost.model.lightgbm LightGBMMILModel — LightGBM-backed MIL classifier
milgboost.objective.base BaseMILObjective abstract interface for custom MIL objectives
milgboost.objective.lse LSEBCE — LogSumExp binary cross-entropy objective

Output ordering

All prediction methods (predict, predict_proba, predict_bags, predict_proba_bags) return results sorted by bag_id in ascending order. For example, if your bag IDs are [3, 1, 2], the output will be ordered as bags [1, 2, 3].

Recommendation: Sort both x and z by z values before prediction to ensure output aligns with your expected ordering:

# Sort x and z by z values before prediction
sort_idx = np.argsort(z)
x_sorted, z_sorted = x[sort_idx], z[sort_idx]

# Predictions will follow the sorted order
probs = model.predict_proba(x_sorted, z_sorted)
# probs[i] corresponds to bag i (after sorting)

Using sequential bag IDs (0, 1, 2, ...) is the simplest approach to avoid confusion.

Sample code

import numpy as np
from milgboost.datasets import make_mil_data
from milgboost.objective import LSEBCE
from milgboost.model import LightGBMMILModel

# Generate synthetic MIL data: 200 bags, 10 features
x, y, z = make_mil_data(
    n_bags=200,
    n_features=10,
    n_informative=5,
    key_instance_ratio=0.3,
    random_state=42,
)

# Split into train/test bags
n_train = 150
train_idx = z < n_train
test_idx = z >= n_train

x_train, y_train, z_train = x[train_idx], y[train_idx], z[train_idx]
x_test, y_test, z_test = x[test_idx], y[test_idx], z[test_idx]

# Train LSE-BCE LightGBM MIL model
model = LightGBMMILModel(
    objective=LSEBCE(r=1.0),
    lgb_params={"verbose": -1, "num_leaves": 15},
    num_boost_round=100,
)
model.fit(x_train, y_train, z_train)

# Predict
probs = model.predict_proba(x_test, z_test)
preds = model.predict(x_test, z_test)
print(f"Accuracy: {(preds == y_test[: len(preds)]).mean():.3f}")

Development

git clone <repo>
cd milgboost

# Create virtualenv and install all extras + dev deps
uv sync --all-extras --group dev

# Type check
uv run poe check

# Lint & format
uv run poe lint
uv run poe format

# Run tests
uv run poe test

License

MIT

Metadata

Release files for milgboost 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for milgboost 0.1.0
File Size Uploaded Provenance
milgboost-0.1.0.tar.gz 6.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for milgboost 0.1.0
File Interpreter ABI Platform
milgboost-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / milgboost-0.1.0.tar.gz

Download URL milgboost-0.1.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cd4840b001f0cce132c8542223af803890b24bf4b1dbcc66f0098ba95d66aa8b
BLAKE2b-256 checksum
How to use checksums
e7c3f9577ede7368420b577ff99d17ea9aa7f921b4363b2e18c410a34ee6eb4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / milgboost-0.1.0-py3-none-any.whl

Download URL milgboost-0.1.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7974ead2e5a08641459f44bbc99a299b7de6910cfd7e6e52161b7acd506f1ac7
BLAKE2b-256 checksum
How to use checksums
913892b2b796250a8fc9f88ee68d6eba1e1f7b18b6bce03902925565a00bc644
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for milgboost 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page