Metadata-Version: 2.4
Name: sklearn-serialize
Version: 0.1
Summary: JSON serialization for scikit-learn models
Author-email: Jesse Grabowski <jessegrabowski@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Jesse Grabowski
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Requires-Python: >=3.12
Requires-Dist: numpy
Requires-Dist: pandas
Requires-Dist: scikit-learn
Requires-Dist: scipy
Provides-Extra: dev
Requires-Dist: polars; extra == 'dev'
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Provides-Extra: polars
Requires-Dist: polars; extra == 'polars'
Description-Content-Type: text/markdown

# sklearn-serialize

JSON serialization for scikit-learn pipelines and the Python/NumPy/SciPy/pandas types that appear inside them. The goal is lossless round-trips: `json_to_data(data_to_json(obj))` reproduces the original object, including fitted model state.

```python
from sklearn_serialize import data_to_json, json_to_data

json_str = data_to_json(fitted_pipeline)
restored  = json_to_data(json_str)

restored.predict(X)  # identical output to the original
```

## Installation

```bash
pip install sklearn-serialize
```

## Supported types

- **sklearn**: `Pipeline`, `FeatureUnion`, `ColumnTransformer`, and any `BaseEstimator` subclass
- **NumPy**: `ndarray`, scalar integer/float/complex types, `datetime64`, `dtype`, `Generator`, `RandomState`
- **SciPy**: sparse matrices (`csr`, `csc`, `coo`, `lil`, `dok`)
- **pandas**: `Series`, `DataFrame`
- **polars**: `Series`, `DataFrame` (optional — requires `polars` to be installed)
- **Python**: `tuple`, `set`, `frozenset`, `bytes`, `bytearray`, `slice`, `complex`, `OrderedDict`, `namedtuple`, `datetime`, `date`

## Custom estimators

Custom estimators work out of the box as long as their class is importable at deserialization time. Call `trust_module` once at startup to allow deserialization from your package:

```python
from sklearn_serialize import trust_module

trust_module("my_package.transformers")
```

The argument is a module prefix — `"my_package"` covers `my_package.transformers`, `my_package.pipelines`, etc. Only exact matches and dotted submodules are allowed; `"my_pack"` does not cover `"my_package"`.

`json_to_data` will raise `ValueError` if it encounters a class from an untrusted module. This prevents arbitrary code execution when deserializing JSON from untrusted sources. Only call `json_to_data` on JSON you produced yourself or received from a trusted source.

The default trusted set covers `sklearn`, `numpy`, `scipy`, `pandas`, `builtins`, and `sklearn_serialize`.

To trust modules globally without calling `trust_module` in every script, create `~/.sklearnserialize`:

```ini
[trusted_modules]
my_package
polars
```

One module prefix per line. Blank lines and lines starting with `#` are ignored. This file is loaded once at import time.
