Skip to main content

mlModelSaver documentation

Tutorial youtube

Introducing mlModelSaver – a streamlined Python module designed for data scientists and developers who seek a straightforward solution for model saving and serving.

While numerous tools are available for training machine learning models, many lightweight statistical models lack simple, efficient saving mechanisms. Existing enterprise solutions like MLflow are robust but come with considerable complexity. Based on my experience, I saw the need for an abstract model registry concept that simplifies this process.

mlModelSaver fills this gap, offering an intuitive way to save machine learning models and transformers. It facilitates seamless integration with frameworks like FastAPI (Examples), Flask, and Django, enabling easy deployment and serving of models in production environments. Empower your machine learning workflow with mlModelSaver – the easy and efficient tool for model management.

Demo

curl --location 'https://ml.jasonjafari.com/models/list'

result

[
    "logisticRegYFromX1AndX2ModelFit",
    "salaryBasedOnGpaMisStatistics_Transfoms_misXStatisticsFit",
    "modelPredictSaleByTemperatureAdvertisingDiscountFit",
    "wageEducAgePower2ModelFit",
    "logRentWithBedsandLogSqftFit",
    "spamBasedOnRecipientsHyperlinksCharactersLogitModelFit",
    ...
]

demo example 1

modelPredictSaleByTemperatureAdvertisingDiscountFit train notebook

Model info

curl --location 'https://ml.jasonjafari.com/model/info/modelPredictSaleByTemperatureAdvertisingDiscountFit'

result

{
    "modelName": "modelPredictSaleByTemperatureAdvertisingDiscountFit",
    "description": "modelPredictSaleByTemperatureAdvertisingDiscountFit",
    "modelType": "sm.OLS",
    "inputs": [
        {
            "name": "Temperature",
            "type": "float"
        },
        {
            "name": "Advertising",
            "type": "float"
        },
        {
            "name": "Discount",
            "type": "float"
        }
    ],
    "outputs": [
        {
            "name": "Sales",
            "type": "float"
        }
    ]
}

predict

curl --location 'https://ml.jasonjafari.com/model/predict' \
--header 'Content-Type: application/json' \
--data '{
    "name": "modelPredictSaleByTemperatureAdvertisingDiscountFit",
    "inputs": [
        {
            "Temperature": 42,
            "Advertising": 15,
            "Discount": 5
        }
    ]
}'

result

[
    {
        "Sales": 19590.467270313893
    }
]

demo example2 train Notebook * interaction transformer

salaryBasedOnGpaMisStatistics_Transfoms_misXStatisticsFit

# info
curl --location 'https://ml.jasonjafari.com/model/info/salaryBasedOnGpaMisStatistics_Transfoms_misXStatisticsFit'
# predict
curl --location 'https://ml.jasonjafari.com/model/predict' \
--header 'Content-Type: application/json' \
--data '{
    "name": "salaryBasedOnGpaMisStatistics_Transfoms_misXStatisticsFit",
    "inputs": [
        {
            "GPA": 3.53,
            "MIS": 1,
            "Statistics": 0
        }
    ]
}'

demo example3 train Notebook * quadratic eq transformer

wageEducAgePower2ModelFit

# info
curl --location 'https://ml.jasonjafari.com/model/info/wageEducAgePower2ModelFit'
# predict
curl --location 'https://ml.jasonjafari.com/model/predict' \
--header 'Content-Type: application/json' \
--data '{
    "name": "wageEducAgePower2ModelFit",
    "inputs": [
        {
            "Educ": 12,
            "Age": 76
        }
    ]
}'

demo example4 train Notebook * log eq transformer for dependent adn independent attributes

logRentWithBedsandLogSqftFit

# info
curl --location 'https://ml.jasonjafari.com/model/info/logRentWithBedsandLogSqftFit'
# predict
curl --location 'https://ml.jasonjafari.com/model/predict' \
--header 'Content-Type: application/json' \
--data '{
    "name": "logRentWithBedsandLogSqftFit",
    "inputs": [
        {
            "Beds": 2,
            "Sqft": 900
        }
    ]
}'

demo example5 train Notebook * Logistic regression

logisticRegYFromX1AndX2ModelFit

# info
curl --location 'https://ml.jasonjafari.com/model/info/logisticRegYFromX1AndX2ModelFit'
# predict
curl --location 'https://ml.jasonjafari.com/model/predict' \
--header 'Content-Type: application/json' \
--data '{
    "name": "logisticRegYFromX1AndX2ModelFit",
    "inputs": [
        {
            "x1": 16.35,
            "x2": 49.44
        }
    ]
}'

demo example6 train Notebook * KNN

gymEnrollAgeIncomeHoursDfKnnFit

# info
curl --location 'https://ml.jasonjafari.com/model/info/gymEnrollAgeIncomeHoursDfKnnFit'
# predict
curl --location 'https://ml.jasonjafari.com/model/predict' \
--header 'Content-Type: application/json' \
--data '{
    "name": "gymEnrollAgeIncomeHoursDfKnnFit",
    "inputs": [
        {
            "Age": 26,
            "Income": 18000,
            "Hours": 14
        },
        {
            "Age": 55,
            "Income": 42000,
            "Hours": 16
        }
    ]
}'

Installation

You can install mlModelSaver via pip:

pip install mlModelSaver

mlModelSaver Example: Simple Linear Regression

In this example, we demonstrate how to use mlModelSaver to export a simple linear regression model based on a notebook from ml_models_deployments.

Example Description

This example builds a simple linear regression model to predict sales based on temperature, advertising, and discount factors. Once the model is fitted and satisfactory, mlModelSaver allows you to easily save and deploy the model for use in production environments.

Example Code - notebook available here

def add_constant_columnTransformer(df):
    # example transformer
    df_with_const = df.copy()
    df_with_const.insert(0, 'const', 1)
    return df_with_const

from mlModelSaver import MlModelSaver
mlModelSaverInstance = MlModelSaver({
    "baseRelativePath": "..",
    "modelsFolder": "models"
})

# Export the model using MlModelSaver
loadedModel = mlModelSaverInstance.exportModel(
     # the models is fitted and ready for usage
    simpleLinearRegressionFittedModel,
    {
        "modelName": "modelPredictSaleByTemperatureAdvertisingDiscountFit",
        "description": "Example model predicting sales based on temperature, advertising, and discount.",
        "modelType": "sm.OLS",  # Example model type (replace with actual type)
        "inputs": [
            {"name": "Temperature", "type": "float"},
            {"name": "Advertising", "type": "float"},
            {"name": "Discount", "type": "float"}
        ],
        "transformer": add_constant_columnTransformer,  # Use your transformation function here
        "outputs": [
            {
                "name": "Sales",
                "type": "float"
            }
        ]
    }
)

testData = [
    {
        "Temperature": 42,
        "Advertising": 15,
        "Discount": 5
    }
]

# Create a DataFrame from the dictionary
testDf = pd.DataFrame(testData)

# this will be your model without export
modelPredictSaleByTemperatureAdvertisingDiscountFit.predict( add_constant_column(testDf)

# result 0    19590.46727 \n dtype: float64

# this will be result of predict with exported model
loadedModel.mlModelSavePredict(testDf)
#result [{'Sales': 19590.467270313893}]

Supported Models

Current supported models by mlModelSaver:

supportedModels = {
    "sm.OLS": {
        "supported": True,
        "normalPredictorFunction": "predict"
    },
    "sm.Logit": {
        "supported": True,
        "normalPredictorFunction": "predict"
    },
    "sklearn.neighbors.KNeighborsClassifier": {
        "supported": True,
        "normalPredictorFunction": "predict_proba"
    },
    "sklearn.tree.DecisionTreeClassifier": {
        "supported": True,
        "normalPredictorFunction": "predict_proba"
    },
}

Next Steps

  • [-] Support More Models WIP: Extend mlModelSaver to support various types of models beyond simple linear regression, such as decision trees, neural networks, and ensemble methods.

  • [-] Additional Examples: Provide diverse examples demonstrating the use of mlModelSaver with different machine learning models and data preprocessing techniques.

  • [] Video Tutorial: Create a comprehensive video tutorial demonstrating how to train models, use mlModelSaver for saving and deployment, and integrate with popular frameworks like Flask and FastAPI.

  • [] Save Models to S3: Implement functionality to save models directly to Amazon S3 for scalable and reliable storage, ensuring robust deployment in cloud environments.

Support More Models

Expand mlModelSaver to handle a variety of machine learning models beyond simple linear regression. Example models could include decision trees, support vector machines, and deep learning models. Ensure that each model type integrates seamlessly with mlModelSaver's saving and deployment functionalities.

Save Models to S3

Enhance mlModelSaver to include options for saving models directly to Amazon S3. This feature ensures that models are stored securely and can be accessed from any location, facilitating deployment across distributed systems and cloud environments.

Additional Examples

Include a range of examples demonstrating mlModelSaver's capabilities across different use cases. Examples should cover various scenarios such as regression, classification, and time series forecasting. Provide clear instructions and code snippets for each example, showcasing how to prepare data, train models, and deploy them using mlModelSaver.

Video Tutorial

Produce a video tutorial that guides users through the entire process of using mlModelSaver. The tutorial should include steps for training a model, integrating with mlModelSaver for saving and loading, and deploying the model using popular web frameworks like Flask or FastAPI. Emphasize best practices and common pitfalls to help users maximize efficiency and reliability in their machine learning projects.

These next steps will enhance mlModelSaver's usability and scalability, enabling users to leverage advanced machine learning models effectively in production environments.

Metadata

Release files for mlModelSaver 1.0.33

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlModelSaver 1.0.33
File Size Uploaded
mlmodelsaver-1.0.33.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mlModelSaver 1.0.33
File Interpreter ABI Platform
mlModelSaver-1.0.33-py3-none-any.whl Python 3 none any Details

Total release size: 14.9 kB

Release files / mlmodelsaver-1.0.33.tar.gz

Download URL mlmodelsaver-1.0.33.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a7085c2429d66e9d8163ef8c91b85d39717a300700a90227d4514b7ba929f7a3
BLAKE2b-256 checksum
How to use checksums
d3793b9193e41166908e6667e84d3ede736cfb626b2ffb351f3c02518ccae608
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.3

Release files / mlModelSaver-1.0.33-py3-none-any.whl

Download URL mlModelSaver-1.0.33-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a865fc9a934185fe869f1b427d9127fcc29af0343098e935468354dabfe130b
BLAKE2b-256 checksum
How to use checksums
85a4a16e557b9a4abb2a32b35d7763c64a8848f96b24e590f4685648bbb3c7fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.0.33 This release

2 release files

1.0.32

2 release files

1.0.31

2 release files

1.0.30

2 release files

1.0.29

2 release files

1.0.28

2 release files

1.0.27

2 release files

1.0.26

2 release files

1.0.25

2 release files

1.0.24

2 release files

1.0.23

2 release files

1.0.22

2 release files

1.0.21

2 release files

1.0.20

2 release files

1.0.19

2 release files

1.0.18

2 release files

1.0.17

2 release files

1.0.16

2 release files

1.0.15

2 release files

1.0.14

2 release files

1.0.13

2 release files

1.0.12

2 release files

1.0.11

1 release file

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

0.1

2 release files

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