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

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.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification

# Create and train a scikit-learn model
X, y = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=0, shuffle=False)
model = RandomForestClassifier(n_estimators=10, max_depth=5, random_state=0)
model.fit(X, y)

# 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
predictions = deserialized_model.predict(X[:5])
print(predictions)

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

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

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

For a full list of supported models, please refer to the SUPPORTED_ESTIMATORS dictionary in serializers/sklearn_serializer.py.

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

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 dependencies:

    pip install -e .
    
  3. Run the tests:

    pytest
    

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.0a1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

openmodels-0.1.0a1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openmodels-0.1.0a1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for openmodels-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 0b22507fda4d2237ec47528e4354b572ab7e36b654c00b3e89ff89ecad70f569
MD5 59e6199a184ddd7a674b91257a9cc7b5
BLAKE2b-256 f8a2cdb36aa771fda91d44b3f3d02d9ccdf4bce557b6d1f1fd3450524961020a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: openmodels-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for openmodels-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 47887eca051afa8312877161b924aaa9a38be11c07edd14f68b14811ad625b24
MD5 79a7e929d1b33c7d776bd101669bb27f
BLAKE2b-256 4c10ae033d4c1c977f7728b75ee70de29b5434dd7b5350922af9663151710282

See more details on using hashes here.

Supported by

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