Skip to main content

MLeap Python API

Project description

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

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

mleap-0.25.2.tar.gz (40.0 kB view details)

Uploaded Source

Built Distribution

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

mleap-0.25.2-py3-none-any.whl (61.0 kB view details)

Uploaded Python 3

File details

Details for the file mleap-0.25.2.tar.gz.

File metadata

  • Download URL: mleap-0.25.2.tar.gz
  • Upload date:
  • Size: 40.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for mleap-0.25.2.tar.gz
Algorithm Hash digest
SHA256 0c61b6a1131e84d548dfe16fcaee9dc05f53fd3e9dcbda7a69bf125a0d2a771c
MD5 270426f5bb2c12ca486ad05392dd8b9a
BLAKE2b-256 80e690f46fcf60e6acf86892b04b1e52c6126eab35938986da50c749cc4e8f64

See more details on using hashes here.

File details

Details for the file mleap-0.25.2-py3-none-any.whl.

File metadata

  • Download URL: mleap-0.25.2-py3-none-any.whl
  • Upload date:
  • Size: 61.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for mleap-0.25.2-py3-none-any.whl
Algorithm Hash digest
SHA256 05801ac2735a066de9263cd6ef3272aec2d5e218d75b54a6113241413be062b7
MD5 45cf5fde65a7cb6db21760d1e1f4226b
BLAKE2b-256 5180d6814ba888cf619fbea525afd4c9dd895f8185bacd73257e758050be8dcd

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