Robust package for segmentation optimization using Particle Swarm Optimization (PSO)
Project description
pso-segmentation
pso-segmentation is a PSO-based package for building interpretable segmentations on any continuous variable.
Overview
The package gives you a compact way to:
- optimize cut points on continuous variables
- encode business-specific constraints inside custom objective functions
- select the number of segments with a dedicated helper
- export results and persist optimizer state
- compare candidates with a business-specific selection function
Installation
pip install pso-segmentation
For development and documentation work:
pip install -e ".[dev,docs]"
Quick Start
Functional API
import numpy as np
from pso_segmentation import make_objective, segment_scores
scores = np.random.uniform(0, 100, 1000)
labels = np.random.binomial(1, 0.15, 1000) # target (binary here)
objective = make_objective(scores, labels, metric="r2")
result = segment_scores(scores, labels, objective)
print(f"R2: {result.r2:.3f}")
print(f"Segments: {result.n_segments}")
Object-Oriented API
import numpy as np
from pso_segmentation import (
make_objective,
monotonic_penalty,
OptimizerConfig,
SegmentationOptimizer,
segment_size_penalty,
)
scores = np.random.uniform(0, 100, 1000)
labels = np.random.binomial(1, 0.15, 1000) # target (binary here)
objective = make_objective(
scores,
labels,
metric="r2",
penalties=[
monotonic_penalty(weight=0.3),
segment_size_penalty(min_size=0.05, max_size=0.4, weight=0.2),
],
)
config = OptimizerConfig(n_segments=5, pop_size=50, max_iter=100, seed=42)
optimizer = SegmentationOptimizer(config)
optimizer.fit(scores, labels, objective)
print(optimizer.summary())
print(optimizer.get_metrics())
Selecting the number of segments
from pso_segmentation import make_objective, monotonic_penalty, select_n_segments
def objective_factory(scores, labels, n_segments, params):
return make_objective(
scores,
labels,
metric="r2",
penalties=[monotonic_penalty(weight=params["monotonic_weight"])],
)
selection = select_n_segments(
scores,
labels,
segment_range=(3, 7),
objective_factory=objective_factory,
param_grid={"monotonic_weight": [0.0, 0.2, 0.5]},
)
print(selection.best_candidate.n_segments)
print(selection.best_candidate.cuts)
Documentation
The full user guide lives in the docs/ folder. The notebooks are intentionally limited to:
notebooks/00_quick_start.ipynb(generic segmentation + objective contract)notebooks/01_business_use_case.ipynb(PD segmentation with a custom objective)
Development
Run the test and quality checks from the repository root:
pytest
ruff check .
ruff format .
mypy src/
License
MIT License - see LICENSE.
Contributing
See CONTRIBUTING.md for the development workflow and contribution rules.
Status
Version 0.1.0 is the current alpha release line. The package API is stable enough for experimentation, notebooks, and internal use, while production release work continues.
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 pso_segmentation-0.1.0.tar.gz.
File metadata
- Download URL: pso_segmentation-0.1.0.tar.gz
- Upload date:
- Size: 122.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3d4e343a2b481a3061e2f28d1463b5607bb56cd5b96ab0aa3885d32a5d49e38
|
|
| MD5 |
f094230f25a0f7ebe49f0638a75df21d
|
|
| BLAKE2b-256 |
1dc7edaa82c852bb53b5e6458ca5dc588d441c1bb4ede6850b75a55de4758849
|
File details
Details for the file pso_segmentation-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pso_segmentation-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89c0798fa799ec837619346cc5511e4b1bbd4d81600d8ee4426b98ddcd8715d5
|
|
| MD5 |
f7b99c19c11a149d2c265b5a50a9f0a8
|
|
| BLAKE2b-256 |
a6d9c840d097015c2ba6f355ad07f0a5986ebb7f82587e544de0578acb15954a
|