Skip to main content

pso-segmentation

Python Version Code style: ruff License: MIT

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.

Metadata

Release files for pso-segmentation 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 pso-segmentation 0.1.0
File Size Uploaded Provenance Actions
pso_segmentation-0.1.0.tar.gz 122.8 kB Without provenance Details for pso_segmentation-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 152.4 kB

Release files / pso_segmentation-0.1.0.tar.gz

Download URL pso_segmentation-0.1.0.tar.gz
Size 122.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d3d4e343a2b481a3061e2f28d1463b5607bb56cd5b96ab0aa3885d32a5d49e38
BLAKE2b-256 checksum
How to use checksums
1dc7edaa82c852bb53b5e6458ca5dc588d441c1bb4ede6850b75a55de4758849
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

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

Download URL pso_segmentation-0.1.0-py3-none-any.whl
Size 29.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89c0798fa799ec837619346cc5511e4b1bbd4d81600d8ee4426b98ddcd8715d5
BLAKE2b-256 checksum
How to use checksums
a6d9c840d097015c2ba6f355ad07f0a5986ebb7f82587e544de0578acb15954a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Security for pso-segmentation 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