Skip to main content

Python integration package for MLeap

This package contains libraries to integrate MLeap with:

  • PySpark
  • Scikit-Learn
  • gensim (optional, Word2Vec serialization)
  • TensorFlow (coming soon)

Installation

$ pip install mleap

PySpark Integration

MLeap's PySpark library provides serialization and deserialization functionality to/from Bundle.ML. There is 100% parity between MLeap's PySpark and Scala/Spark support and all of the supported transformers can be found here.

We have both a basic tutorial and an advance demo of serializing and de-serializing using PySpark, but in short you can continue to write ML Pipelines as you normally would and we provide the following interface for serialization/de-serialization:

# Define your pipeline
feature_pipeline = [string_indexer, feature_assembler]

featurePipeline = Pipeline(stages=feature_pipeline)

# Fit your pipeline
fittedPipeline = featurePipeline.fit(df)

# Serialize your pipeline
fittedPipeline.serializeToBundle("jar:file:/tmp/pyspark.example.zip", fittedPipeline.transform(df))

StringMap transformer

# dict of label mappings
labels = {'a': 1.0}

string_map_transformer = StringMap(
    labels, 'key_col', 'value_col', handleInvalid='keep', defaultValue=0.0)

MathUnary transformer

Example usage:

# dict of label mappings
input_dataframe = pd.DataFrame([[0.1, 0.2, 0.3]], columns=['f1'])

sin_transformer = MathUnary(
    operation=UnaryOperation.Sin,
    inputCol="f1",
    outputCol="sin(f1)",
)

sin_transformer.transform(input_dataframe)

Scikit-Learn Integration

MLeap's Scikit-Learn library provides serialization (de-serialization coming soon) functionality to Bundle.ML. There is already parity between the math that Scikit and Spark transformers execute, and MLeap takes advantage of that to provide a common serialization format for the two technologies.

A simple example is the StandardScaler transformer that normalizes the data given the mean and standard deviation. Both Spark and Scikit perform the standard normal transform on the data, and can both be serialized to the following format:

{
    "op": "standard_scaler",
    "attributes": {
        "mean": {
              "double": [0.2354223, 1.34502332],
              "shape": {
                "dimensions": [{
                  "size": 2,
                  "name": ""
                }]
              },
             "type": "tensor"             
        },
        "std": {
              "double": [0.13842223, 0.78320249],
              "shape": {
                "dimensions": [{
                  "size": 2,
                  "name": ""
                }]
              },
             "type": "tensor"
        }
    }
}

Scikit-Learn pipelines, just like Spark Pipelines, can be serialized to an MLeap Bundle and deployed to an MLeap runtime environment.

You can also take your scikit pipelines and deploy them to your Spark cluster, because MLeap can de-serialize them into a Spark ML Pipeline and execute them on data frames.

gensim Integration

MLeap provides an optional integration that serializes a gensim Word2Vec model to Bundle.ML. gensim is not installed as a dependency of this package, so install it yourself if you want to use this integration.

from gensim.models import Word2Vec
import mleap.gensim.word2vec

model = Word2Vec(sentences, vector_size=100)
model.mlinit(input_features='sentence', prediction_column='word_vectors')
model.serialize_to_bundle('/tmp', 'word2vec-pipeline')

Documentation

Documentation can be found on our mleap docs page:

Contributions

Contributions are welcome! Make sure all python tests pass. You can run them from the top-level makefile:

make test_python

If you'd rather use the inner python/Makefile, remember to source SCALA_CLASS_PATH by running:

source scripts/scala_classpath_for_python.sh
cd python/ && make test

Metadata

Release files for mleap 0.25.3

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

Source distribution (sdist)

Source distribution for mleap 0.25.3
File Size Uploaded
mleap-0.25.3.tar.gz 40.0 kB Details

Built distribution (wheel)

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

Total release size: 101.0 kB

Release files / mleap-0.25.3.tar.gz

Download URL mleap-0.25.3.tar.gz
Size 40.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0a067dd1ea76fd79e771c4678d924955b01a0ff4a25142edef0d0ae9dfddc7d8
BLAKE2b-256 checksum
How to use checksums
d9c9821a7eff0abb98c7f0a53027f3aade6cc811bc4ff7843f9b9470cf75f638
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / mleap-0.25.3-py3-none-any.whl

Download URL mleap-0.25.3-py3-none-any.whl
Size 61.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9eb73662358afd12714fa649376e356025d687d3b3f2d5ec682a9b68b791bac
BLAKE2b-256 checksum
How to use checksums
5ab2821be725ca7a63bc51012faa17ed5de47e4ae81c78f96344a9e14db78d95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release history Release notifications | RSS feed

This release

0.25.3 This release

2 release files

0.25.2

2 release files

0.25.1

2 release files

0.25.0

2 release files

0.24.2

2 release files

0.24.1

2 release files

0.24.0

2 release files

0.23.4

2 release files

0.23.3

2 release files

0.23.2

2 release files

0.23.1

2 release files

0.23.0

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.19.0

2 release files

0.18.1

2 release files

0.18.0

2 release files

0.16.0

2 release files

0.15.0

2 release files

0.8.1

1 release file

0.7.0

1 release file

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