Skip to main content

Export scikit-learn model files to JSON for sharing or deploying predictive models with peace of mind.

Project description

OpenModels

PyPI version License: MIT Python Versions TestPyPI

OpenModels is a flexible and extensible library for serializing and deserializing machine learning models. It's designed to support any serialization format through a plugin-based architecture, providing a safe and transparent solution for exporting and sharing predictive models.

Key Features

  • Format Agnostic: Supports any serialization format through a plugin-based system.
  • Extensible: Easily add support for new model types and serialization formats.
  • Safe: Provides alternatives to potentially unsafe serialization methods like Pickle.
  • Transparent: Supports human-readable formats for easy inspection of serialized models.

Installation

pip install openmodels

Quick Start

from openmodels import SerializationManager, SklearnSerializer
from sklearn.decomposition import PCA
from sklearn.datasets import make_classification

# Create and train a scikit-learn model
X, _ = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=0, shuffle=False)
model = PCA(n_components=2, random_state=0)
model.fit(X)

# Create a SerializationManager
manager = SerializationManager(SklearnSerializer())

# Serialize the model (default format is JSON)
serialized_model = manager.serialize(model)

# Deserialize the model
deserialized_model = manager.deserialize(serialized_model)

# Use the deserialized model
transformed_data = deserialized_model.transform(X[:5])
print(transformed_data)

Extensibility

OpenModels is designed to be easily extended with new serialization formats and model types.

Adding a New Format

To add a new serialization format, create a class that implements the FormatConverter protocol and register it with the FormatRegistry:

from openmodels.protocols import FormatConverter
from openmodels.format_registry import FormatRegistry
from typing import Dict, Any

class YAMLConverter(FormatConverter):
    @staticmethod
    def serialize_to_format(data: Dict[str, Any]) -> str:
        import yaml
        return yaml.dump(data)

    @staticmethod
    def deserialize_from_format(formatted_data: str) -> Dict[str, Any]:
        import yaml
        return yaml.safe_load(formatted_data)

FormatRegistry.register("yaml", YAMLConverter)

Adding a New Model Serializer

To add support for a new type of model, create a class that implements the ModelSerializer protocol:

from openmodels.protocols import ModelSerializer
from typing import Any, Dict

class TensorFlowSerializer(ModelSerializer):
    def serialize(self, model: Any) -> Dict[str, Any]:
        # Implementation for serializing TensorFlow models
        ...

    def deserialize(self, data: Dict[str, Any]) -> Any:
        # Implementation for deserializing TensorFlow models
        ...

Supported Models (scikit-learn)

OpenModels currently supports a wide range of scikit-learn models, including:

  • Classification: LogisticRegression, SVC, etc.
  • Regression: LinearRegression, SVR, etc.
  • Clustering: KMeans
  • Dimensionality Reduction: PCA

For a full list of supported models, you can programmatically retrieve them using the SklearnSerializer.all_estimators() method:

from openmodels.serializers import SklearnSerializer

# Get all supported estimators (classifiers, regressors, etc.)
all_supported = SklearnSerializer.all_estimators()
print([name for name, cls in all_supported])

# To get only classifiers:
classifiers = SklearnSerializer.all_estimators(type_filter="classifier")
print([name for name, cls in classifiers])

# To get only regressors:
regressors = SklearnSerializer.all_estimators(type_filter="regressor")
print([name for name, cls in regressors])

This will print the names of all scikit-learn estimators supported by OpenModels, filtered to exclude those that are not currently supported.

Contributing

We welcome contributions to OpenModels! Whether you want to add support for new models, implement new serialization formats, or improve the existing codebase, your help is appreciated.

Please refer to our Contributing Guidelines for more information on how to get started.

Running Tests

The package utilizes Taskfile as a task runner to automate and standardize development flows.

To run the tests:

  1. Clone the repository:

    git clone https://github.com/your-repo/openmodels.git
    cd openmodels
    
  2. Install the package and its development dependencies:

    task install:dev
    
  3. Run the tests:

    task test
    

License

This project is licensed under the MIT License. See the LICENSE file for details.

Changelog

For a detailed changelog, please see the CHANGELOG.md file.

Support

If you encounter any issues or have questions, please file an issue on our GitHub repository.

We're always looking to improve OpenModels. If you have any suggestions or feature requests, please let us know!

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

openmodels-0.1.0a18.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

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

openmodels-0.1.0a18-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file openmodels-0.1.0a18.tar.gz.

File metadata

  • Download URL: openmodels-0.1.0a18.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.14 Linux/6.11.0-1018-azure

File hashes

Hashes for openmodels-0.1.0a18.tar.gz
Algorithm Hash digest
SHA256 ec9febf7625a03178142596bb231cccd3ce3f7d55d5c1d16838ef0ca194131f9
MD5 2327d85754eaafe38d9d064548f4a543
BLAKE2b-256 75a8362027495313b8be9d6ffbdad500a2b33f01226b1b9455dbe18436da0f2f

See more details on using hashes here.

File details

Details for the file openmodels-0.1.0a18-py3-none-any.whl.

File metadata

  • Download URL: openmodels-0.1.0a18-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.14 Linux/6.11.0-1018-azure

File hashes

Hashes for openmodels-0.1.0a18-py3-none-any.whl
Algorithm Hash digest
SHA256 2739c37b196d669a4a57902bdddf22b1ae150b276118695f03def8b29c68e73c
MD5 b27f6442c571a26eff38505d885b83d8
BLAKE2b-256 74173cdced6ec2877a1f446202553e6dc213ffdfd9b1d47685c8a63188410666

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