Sparse Multiple-Instance Learning: SVM, NSK, sMIL and sAwMIL.
Project description
Sparse Multiple-Instance Learning in Python
MIL models based on the Support Vector Machines (NSK, sMIL, sAwMIL). Inspired by the outdated misvm package.
Note: This is an alpha version.
Installation
pip install sawmil
Requirements
numpy>=1.22
scikit-learn>=1.7.0
gurobipy>=12.0.3
python>=11.0 # recommended: >=12.3
At this point, sawmil package works only with the Gurobi optimizer. You need to obtain a academic/commercial license to use it. We plan to add implementations with other solvers.
Quick start
1. Generate dummy data
from dataset import make_complex_bags
import numpy as np
rng = np.random.default_rng(0)
ds = make_complex_bags(
n_pos=300, n_neg=100, inst_per_bag=(5, 15), d=2,
pos_centers=((+2,+1), (+4,+3)),
neg_centers=((-1.5,-1.0), (-3.0,+0.5)),
pos_scales=((2.0, 0.6), (1.2, 0.8)),
neg_scales=((1.5, 0.5), (2.5, 0.9)),
pos_intra_rate=(0.25, 0.85),
ensure_pos_in_every_pos_bag=True,
neg_pos_noise_rate=(0.00, 0.05),
pos_neg_noise_rate=(0.00, 0.20),
outlier_rate=0.1,
outlier_scale=8.0,
random_state=42,
)
2. NSK with RBF Kernel
Load a kernel:
from sawmil.kernels import get_kernel
from sawmil.bag_kernels import make_bag_kernel
k = get_kernel("rbf", gamma=0.5) # base (single-instance kernel)
bag_k = make_bag_kernel(k, use_intra_labels=False) # convert single-instance kernel to bagged kernel
Fit NSK Model:
from sawmil.nsk import NSK
clf = NSK(C=0.1, bag_kernel=bag_k, scale_C=True, tol=1e-8, verbose=False).fit(ds, None)
print("Train acc:", clf.score(ds, np.array([b.y for b in ds.bags])))
3. Fit sMIL Model with Linear Kernel
k = get_kernel("linear", normalizer="none") # base (single-instance kernel)
bag_k = make_bag_kernel(Linear(), normalizer="none", use_intra_labels=False)
clf = sMIL(C=0.1, bag_kernel=bag_k, scale_C=True, tol=1e-6, verbose=False).fit(ds, None)
print("Train acc:", clf.score(ds, np.array([1 if b.y > 0 else -1 for b in ds.bags])))
See more examples in the example.ipynb notebook.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sawmil-0.1.2.tar.gz.
File metadata
- Download URL: sawmil-0.1.2.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e71cb2236d7863613ca2bd3720c1dcb22b72b1de6e1b08ed6cbe44dfa42cb044
|
|
| MD5 |
987fbb9f25a9374bfca44b921ca9c44d
|
|
| BLAKE2b-256 |
cb8241b290bd29966b9c230e901be7938da379d45b117624a6787e1ff9215acb
|
Provenance
The following attestation bundles were made for sawmil-0.1.2.tar.gz:
Publisher:
publish.yml on carlomarxdk/sawmil
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sawmil-0.1.2.tar.gz -
Subject digest:
e71cb2236d7863613ca2bd3720c1dcb22b72b1de6e1b08ed6cbe44dfa42cb044 - Sigstore transparency entry: 447231852
- Sigstore integration time:
-
Permalink:
carlomarxdk/sawmil@36d2f1e38479568d7b9c68c39aca3fdc79b6d262 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/carlomarxdk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@36d2f1e38479568d7b9c68c39aca3fdc79b6d262 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sawmil-0.1.2-py3-none-any.whl.
File metadata
- Download URL: sawmil-0.1.2-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcd2c33ebb37123e0f242d61cde5a2f07e5c84e1115d1080abb2c09c7e7d746e
|
|
| MD5 |
6740fbfe74f8764f2ed200090cd91d50
|
|
| BLAKE2b-256 |
64fd2e478f1e90e84e11ef4349de62fd6419d011d7f7679bbc0b39c59df1772d
|
Provenance
The following attestation bundles were made for sawmil-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on carlomarxdk/sawmil
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sawmil-0.1.2-py3-none-any.whl -
Subject digest:
bcd2c33ebb37123e0f242d61cde5a2f07e5c84e1115d1080abb2c09c7e7d746e - Sigstore transparency entry: 447231864
- Sigstore integration time:
-
Permalink:
carlomarxdk/sawmil@36d2f1e38479568d7b9c68c39aca3fdc79b6d262 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/carlomarxdk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@36d2f1e38479568d7b9c68c39aca3fdc79b6d262 -
Trigger Event:
release
-
Statement type: