PyPMML
PyPMML is a Python PMML scoring library, it really is the Python API for PMML4S.
Prerequisites
- Java >= 8
- Python 2.7 or >= 3.5
Dependencies
Installation
pip install pypmml
Or install the latest version from github:
pip install --upgrade git+https://github.com/autodeployai/pypmml.git
Usage
-
Load model from various sources, e.g. readable, file path, string, or an 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.load('single_iris_dectree.xml')
-
Call
predict(data)to predict new values that can be in different types, e.g. dict, list, json, ndarray of NumPy, Series or DataFrame of Pandas.datain dict:
>>> model.predict({'sepal_length': 5.1, 'sepal_width': 3.5, 'petal_length': 1.4, 'petal_width': 0.2}) {'probability_Iris-setosa': 1.0, 'probability_Iris-versicolor': 0.0, 'probability': 1.0, 'predicted_class': 'Iris-setosa', 'probability_Iris-virginica': 0.0, 'node_id': '1'}
datain list:
NOTE: the order of values must match the input names, and the order of results always matches the output names.
>>> model.inputNames ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'] >>> model.predict([5.1, 3.5, 1.4, 0.2]) ['Iris-setosa', 1.0, 1.0, 0.0, 0.0, '1'] >>> model.outputNames ['predicted_class', 'probability', 'probability_Iris-setosa', 'probability_Iris-versicolor', 'probability_Iris-virginica', 'node_id']
datainrecordsjson:
>>> model.predict('[{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}]') [{"probability":1.0,"probability_Iris-versicolor":0.0,"probability_Iris-setosa":1.0,"probability_Iris-virginica":0.0,"predicted_class":"Iris-setosa","node_id":"1"}]
datainsplitjson:
>>> model.predict('{"columns": ["sepal_length", "sepal_width", "petal_length", "petal_width"], "data": [[5.1, 3.5, 1.4, 0.2]]}') {"columns":["predicted_class","probability","probability_Iris-setosa","probability_Iris-versicolor","probability_Iris-virginica","node_id"],"data":[["Iris-setosa",1.0,1.0,0.0,0.0,"1"]]}
datain ndarray of NumPy:
NOTE: as the list above, the order of ndarray values must match the input names, and the order of results always matches the output names.
>>> import numpy as np >>> model.predict(np.array([5.1, 3.5, 1.4, 0.2])) ['Iris-setosa', 1.0, 1.0, 0.0, 0.0, '1'] >>> >>> model.predict(np.array([[5.1, 3.5, 1.4, 0.2], [7, 3.2, 4.7, 1.4]])) [['Iris-setosa', 1.0, 1.0, 0.0, 0.0, '1'], ['Iris-versicolor', 0.9074074074074074, 0.0, 0.9074074074074074, 0.09259259259259259, '3']]
datain Series of Pandas:
>>> import pandas as pd >>> model.predict(pd.Series({'sepal_length': 5.1, 'sepal_width': 3.5, 'petal_length': 1.4, 'petal_width': 0.2})) node_id 1 predicted_class Iris-setosa probability 1 probability_Iris-setosa 1 probability_Iris-versicolor 0 probability_Iris-virginica 0 Name: 0, dtype: object
datain DataFrame of Pandas:
>>> import pandas as pd >>> data = pd.read_csv('Iris.csv') # The data is from here: http://dmg.org/pmml/pmml_examples/Iris.csv >>> model.predict(data) node_id predicted_class 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 2 1 Iris-setosa 1.000000 1.0 0.000000 0.000000 3 1 Iris-setosa 1.000000 1.0 0.000000 0.000000 4 1 Iris-setosa 1.000000 1.0 0.000000 0.000000 .. ... ... ... ... ... ... 145 10 Iris-virginica 0.978261 0.0 0.021739 0.978261 146 10 Iris-virginica 0.978261 0.0 0.021739 0.978261 147 10 Iris-virginica 0.978261 0.0 0.021739 0.978261 148 10 Iris-virginica 0.978261 0.0 0.021739 0.978261 149 10 Iris-virginica 0.978261 0.0 0.021739 0.978261
Support Java gateways
PyPMML supports both backends access to Java from Python: "py4j" and "jpype", Py4j is used by default, you can call the following code to switch to jpype before loading models:
from pypmml import PMMLContext
PMMLContext.getOrCreate(gateway="jpype")
Use PMML in Scala or Java
See the PMML4S project. PMML4S is a PMML scoring library for Scala. It provides both Scala and Java Evaluator API for PMML.
Use PMML in Spark
See the PMML4S-Spark project. PMML4S-Spark is a PMML scoring library for Spark as SparkML Transformer.
Use PMML in PySpark
See the PyPMML-Spark project. PyPMML-Spark is a Python PMML scoring library for PySpark as SparkML Transformer, it really is the Python API for PMML4s-Spark.
Deploy PMML as REST API
See the AI-Serving project. AI-Serving is serving AI/ML models in the open standard formats PMML and ONNX with both HTTP (REST API) and gRPC endpoints.
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.
Metadata
Release files for pypmml 1.5.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pypmml-1.5.8.tar.gz | 14.1 MB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pypmml-1.5.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.2 MB
Release files / pypmml-1.5.8.tar.gz
| Download URL | pypmml-1.5.8.tar.gz |
|---|---|
| Size | 14.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
14d08c8a71303f87a2766853c4662b07653cba70916a51aed3c6e0fa2c3dcf48
|
|
BLAKE2b-256 checksum How to use checksums |
4ac5c4c200b056c55f78b6eb2dbb848b6c6a1d957b48cfe605d9c0b5cc273920
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / pypmml-1.5.8-py3-none-any.whl
| Download URL | pypmml-1.5.8-py3-none-any.whl |
|---|---|
| Size | 14.1 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cfa4e82b6d8d92f989e18a95fb4740a0ad81db094880cf7ac8dd5ff84147e617
|
|
BLAKE2b-256 checksum How to use checksums |
b8a8d34857abefece98c3befdf27ee806c46f87bd036fa5a346fe7e3cd8ef87a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Security for pypmml 1.5.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.