Skip to main content

PMML4S Python API

Project description

PyPMML

PyPMML is the Python API for PMML4S.

Prerequisites

  • Java >= 1.8
  • Python 2.7 or >= 3.5

Dependencies

  • Py4J
  • Pandas (optional)

Installation

pip install pypmml

Or install the latest version from github:

pip install --upgrade git+https://github.com/autodeployai/pypmml.git

Usage

  1. Load model from various sources, e.g. filename, string, or array of bytes.

    from pypmml import Model
    
    # The model is from http://dmg.org/pmml/pmml_examples/KNIME_PMML_4.1_Examples/single_iris_dectree.xml
    model = Model.fromFile('single_iris_dectree.xml')
    
  2. Call predict(data) to predict new values that can be in different types, e.g. dict, json, Series or DataFrame of Pandas.

    # data in dict
    result = model.predict({'sepal_length': 5.1, 'sepal_width': 3.5, 'petal_length': 1.4, 'petal_width': 0.2})
     >>> print(result)
    {'Probability': 1.0, 'Node_ID': '1', 'Probability_Iris-virginica': 0.0, 'Probability_Iris-setosa': 1.0, 'Probability_Iris-versicolor': 0.0, 'PredictedValue': 'Iris-setosa'}
    
    # data in 'records' json
    result = model.predict('[{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}]')
     >>> print(result)
    [{"Probability":1.0,"Probability_Iris-versicolor":0.0,"Probability_Iris-setosa":1.0,"Probability_Iris-virginica":0.0,"PredictedValue":"Iris-setosa","Node_ID":"1"}]
    
    # data in 'split' json
    result = model.predict('{"columns": ["sepal_length", "sepal_width", "petal_length", "petal_width"], "data": [[5.1, 3.5, 1.4, 0.2]]}')
     >>> print(result)
    {"columns":["PredictedValue","Probability","Probability_Iris-setosa","Probability_Iris-versicolor","Probability_Iris-virginica","Node_ID"],"data":[["Iris-setosa",1.0,1.0,0.0,0.0,"1"]]}
    

    How to work with Pandas

    import pandas as pd
    
    # data in Series
    result = model.predict(pd.Series({'sepal_length': 5.1, 'sepal_width': 3.5, 'petal_length': 1.4, 'petal_width': 0.2}))
    >>> print(result)
    Node_ID                                  1
    PredictedValue                 Iris-setosa
    Probability                              1
    Probability_Iris-setosa                  1
    Probability_Iris-versicolor              0
    Probability_Iris-virginica               0
    Name: 0, dtype: object
    
    # The data is from here: http://dmg.org/pmml/pmml_examples/Iris.csv
    data = pd.read_csv('Iris.csv')
    
    # data in DataFrame
    result = model.predict(data)
     >>> print(result)
        Node_ID   PredictedValue  Probability  Probability_Iris-setosa  Probability_Iris-versicolor  Probability_Iris-virginica
    0         1      Iris-setosa     1.000000                      1.0                     0.000000                    0.000000
    1         1      Iris-setosa     1.000000                      1.0                     0.000000                    0.000000
    ..      ...              ...          ...                      ...                          ...                         ...
    148      10   Iris-virginica     0.978261                      0.0                     0.021739                    0.978261
    149      10   Iris-virginica     0.978261                      0.0                     0.021739                    0.978261
    
    [150 rows x 6 columns]
    
  3. Shutdown the gateway of Py4J to free resources.

    Model.close()
    

Use in PySpark

See the PyPMML-Spark project.

Support

If you have any questions about the PyPMML library, please open issues on this repository.

Feedback and contributions to the project, no matter what kind, are always very welcome.

License

PyPMML is licensed under APL 2.0.

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

pypmml-0.9.0.tar.gz (14.7 MB view details)

Uploaded Source

File details

Details for the file pypmml-0.9.0.tar.gz.

File metadata

  • Download URL: pypmml-0.9.0.tar.gz
  • Upload date:
  • Size: 14.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for pypmml-0.9.0.tar.gz
Algorithm Hash digest
SHA256 f77c20843684b8586ad0f912b70f1c4c69035bbc2ecd270e430c07b4789b1a95
MD5 088404bfedf8bda1c3a6257f5cbde916
BLAKE2b-256 2ffe22e754e0528c968296926e0f0a61123e09be4400f889846a492a9fde0295

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