Skip to main content

A scikit-learn compatible transformer for proportional categorical imputation.

Project description

sklearn-propfill

Python scikit-learn PyPI

ProportionalImputer is a scikit-learn compatible transformer that fills missing categorical values so the filled values match the observed distribution.


✨ Features

  • Works with pandas DataFrames and NumPy arrays
  • Integrates directly into scikit-learn Pipelines
  • Supports both exact proportional filling and probabilistic sampling
  • Deterministic with random_state for reproducibility

📦 Installation

Clone the repo and install in editable mode:

git clone https://github.com/your-username/sklearn-propfill.git
cd sklearn-propfill
pip install -e .

🚀 Usage Example

Basic Example with pandas

import pandas as pd
from propfill import ProportionalImputer

data = pd.DataFrame({
    "city": ["A", "B", None, "A", "C", None, "B", "A"]
})

imp = ProportionalImputer(random_state=42, exact=True)
imp.fit(data)
filled = imp.transform(data)
print(filled)

In a scikit-learn Pipeline

from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LogisticRegression
from propfill import ProportionalImputer
import pandas as pd

X = pd.DataFrame({
    "city": ["A", "B", None, "A", "C", None, "B", "A"],
    "age":  [22, 30, 27, 24, 19, 28, None, 26],
})
y = [0, 1, 0, 0, 1, 1, 0, 1]

ct = ColumnTransformer([
    ("city_imp", ProportionalImputer(random_state=42), ["city"]),
], remainder="passthrough")

pipe = Pipeline([
    ("prep", ct),
    ("clf", LogisticRegression(max_iter=500))
])

pipe.fit(X, y)

⚖️ Exact vs Approximate Filling

The exact parameter controls how missing values are filled:

exact=True (default)

  • Ensures missing values are filled in exact proportion to the observed distribution.
  • Example: if 10 values are missing and the non-missing data is 50% A, 30% B, 20% C, the imputer fills exactly 5 A, 3 B, 2 C.
  • Great for reproducibility and fairness.

exact=False

  • Fills missing values by random sampling according to probabilities.
  • Same example: each missing value is filled by drawing randomly with probabilities [0.5, 0.3, 0.2].
  • The final counts may differ slightly (e.g., 6 A, 2 B, 2 C) depending on randomness.
  • Useful when you want natural variation instead of strict counts.

✅ Testing

Run unit tests with:

pytest -q

📜 License

MIT License – feel free to use, modify, and share.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sklearn_propfill-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sklearn_propfill-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file sklearn_propfill-0.1.0.tar.gz.

File metadata

  • Download URL: sklearn_propfill-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for sklearn_propfill-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bfd4a5e03947b3ff0f71998090940ac1e333d15668a6611f1726b18cee8eb9e5
MD5 eca0faa8a01841f8dc1a7804ad6d49b1
BLAKE2b-256 3ee9abc44322698bb11b7b13e6a3182a79f2d910d7ca32ffb81911624c7b6ac3

See more details on using hashes here.

File details

Details for the file sklearn_propfill-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sklearn_propfill-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75635f46ac9b4c4bd547189b2e87ace8571ac652bac1c05fdff873343aa77462
MD5 d94cd279062b907867b455072410a241
BLAKE2b-256 330731410fe61e0aa77cc977f26025ae24d1f50a547f504f452b84c570f9648d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page