Skip to main content

JPMML-Evaluator-Python Build Status

PMML evaluator library for Python.

Features

This package provides Python wrapper classes and functions for the JPMML-Evaluator library.

Prerequisites

  • Java Platform, Standard Edition 11 or newer.
  • Python 3.8 or newer.

Installation

Installing a release version from PyPI:

pip install jpmml_evaluator

Installing a release version with Polars support:

pip install "jpmml_evaluator[polars]"

Alternatively, installing the latest snapshot version from GitHub:

pip install --upgrade git+https://github.com/jpmml/jpmml-evaluator-python.git

Usage

Command-line application

The jpmml_evaluator module is executable. The main application loads the model from the PMML file, and scores all data records from the input CSV file or stream; scoring results are written to the output CSV file or stream:

python -m jpmml_evaluator DecisionTreeIris.pmml --input Iris.csv --output DecisionTreeIris.csv

If the input CSV file (-i or --input) and/or the output CSV file (-o or --output) is not specified, then system input (sys.stdin) and system output (sys.stdout), respectively, are assumed:

python -m jpmml_evaluator DecisionTreeIris.pmml < Iris.csv > DecisionTreeIris.csv

Getting help:

python -m jpmml_evaluator --help

On some platforms, the Pip package installer additionally makes the main application available as a top-level command:

jpmml_evaluator DecisionTreeIris.pmml < Iris.csv > DecisionTreeIris.csv

Library

Java-to-Python API mapping

Guiding principles:

  1. Java package prefix org.jpmml.evaluator becomes Python package prefix jpmml_evaluator.
  2. Java classes and interfaces become Python classes with the same name.
  3. Java methods become Python methods with the same name. In case of method overloading, the names of Python methods may have a disambiguating suffix (eg. loadFile, loadInputStream) appended to them.
  4. Java parameter types become Python parameter types.

For example, the Java method org.jpmml.evaluator.Evaluator#evaluate(Map<String, ?> arguments) has become a Python method jpmml_evaluator.Evaluator.evaluate(arguments: dict).

Java backend

The communication with the JPMML-Evaluator library is managed by a jpmml_evaluator.JavaBackend object.

Currently, it's possible to choose between three backend implementations:

  JPype PyJNIus Py4J
GitHub project page jpype-project/jpype kivy/pyjnius bartdag/py4j
Python package jpype1 pyjnius py4j
Implementation class jpmml_evaluator \ .jpype.JPypeBackend jpmml_evaluator \ .pyjnius.PyJNIusBackend jpmml_evaluator \ .py4j.Py4JBackend
Implementation class alias "jpype" "pyjnius" "py4j"
Type Local JVM via JNI Local JVM via JNI Local or remote JVM via TCP/IP sockets
Restartable No No Yes

For performance comparison, see the Benchmarking section below.

The chosen backend starts the JVM automatically when it is first needed (ie. lazy start). If you need to pass specific configuration options to the JVM, then start the JVM explicitly beforehand:

from jpmml_evaluator.jpype import JPypeBackend

JPypeBackend.createJVM(jvm_options = ["-Xmx4G"])

Use the same approach to set up a custom classpath (eg. Java UDF libraries together with their supporting libraries):

from jpmml_evaluator.jpype import JPypeBackend

JPypeBackend.createJVM(user_classpath = ["udf-library.jar", "udf-library-support.jar"])

The createJVM class method is available on all backend classes (ie. JPypeBackend, PyJNIusBackend and Py4JBackend).

Workflow

Building a verified model evaluator from a PMML file:

from jpmml_evaluator import make_evaluator

evaluator = make_evaluator("DecisionTreeIris.pmml") \
	.verify()

Printing model schema:

inputFields = evaluator.getInputFields()
print("Input fields: " + str([inputField.getName() for inputField in inputFields]))

targetFields = evaluator.getTargetFields()
print("Target field(s): " + str([targetField.getName() for targetField in targetFields]))

outputFields = evaluator.getOutputFields()
print("Output fields: " + str([outputField.getName() for outputField in outputFields]))

Evaluating a single data record:

from jpmml_evaluator import JavaError

arguments = {
	"Sepal.Length" : 5.1,
	"Sepal.Width" : 3.5,
	"Petal.Length" : 1.4,
	"Petal.Width" : 0.2
}

try:
	results = evaluator.evaluate(arguments)
	print(results)
except JavaError as je:
	pass

Evaluating a collection of data records:

import pandas

arguments_df = pandas.read_csv("Iris.csv", sep = ",")

results_df = evaluator.evaluateAll(arguments_df, error_col = "errors")
print(results_df)

# The error column is added to the results DataFrame only if there was something erroneous happening
errors = results_df["errors"] if "errors" in results_df.columns else None
if errors is not None:
	pass

Alternatively, getting the results DataFrame and errors Series as separate objects:

results_df, errors = evaluator.evaluateAll(arguments_df, error_col = None)
print(results_df)

if errors is not None:
	pass

The evaluateAll method also supports Polars DataFrame arguments and results:

import polars

arguments_df = polars.read_csv("Iris.csv")

results_df = evaluator.evaluateAll(arguments_df)
print(results_df)

Benchmarking

The examples/benchmark.py script evaluates a model with all three backends, and reports the timings in microseconds per data record (min -- median of five repetitions; smaller is better).

Results for JPMML-Evaluator-Python 0.17.0, Python 3.11.15, Pandas 2.2.3, Java 21.0.10, 22 CPUs:

  JPype PyJNIus Py4J
Python package jpype1==1.8.1 pyjnius==1.9.0 py4j==0.10.9.9

Small batch

Benchmarking the DecisionTreeIris.pmml model with the Iris.csv dataset (150 data records):

python -m examples.benchmark DecisionTreeIris.pmml Iris.csv
Interpreted model JPype PyJNIus Py4J
evaluate(X) 9.88 -- 12.11 13.60 -- 15.67 103.53 -- 106.91
evaluateAll(X, parallelism = 1) 12.90 -- 13.37 13.06 -- 13.50 17.27 -- 19.55
evaluateAll(X, parallelism = -1) 14.11 -- 16.72 13.10 -- 14.50 18.33 -- 21.75

Large batch

Benchmarking the same model with the Iris.csv dataset repeated a thousand times (150 000 data records):

python -m examples.benchmark DecisionTreeIris.pmml Iris.csv --multiplier 1000
Interpreted model JPype PyJNIus Py4J
evaluate(X) 10.59 -- 10.68 12.58 -- 12.62 96.41 -- 108.11
evaluateAll(X, parallelism = 1) 2.88 -- 2.94 2.86 -- 3.01 5.72 -- 5.95
evaluateAll(X, parallelism = -1) 2.21 -- 2.53 2.22 -- 2.33 4.93 -- 5.05

License

JPMML-Evaluator-Python is licensed under the terms and conditions of the GNU Affero General Public License, Version 3.0. For a quick summary of your rights ("Can") and obligations ("Cannot" and "Must") under AGPLv3, please refer to TLDRLegal.

If you would like to use JPMML-Evaluator-Python in a proprietary software project, then it is possible to enter into a licensing agreement which makes it available under the terms and conditions of the BSD 3-Clause License instead.

Additional information

JPMML-Evaluator-Python is developed and maintained by Openscoring Ltd, Estonia.

Interested in using JPMML software in your software? Please contact info@openscoring.io

Metadata

Release files for jpmml-evaluator 0.17.0

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

Source distribution (sdist)

Source distribution for jpmml-evaluator 0.17.0
File Size Uploaded Provenance Actions
jpmml_evaluator-0.17.0.tar.gz 9.2 MB Without provenance Details for jpmml_evaluator-0.17.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jpmml-evaluator 0.17.0
File Interpreter ABI Platform Actions
jpmml_evaluator-0.17.0-py3-none-any.whl Python 3 none any Details for jpmml_evaluator-0.17.0-py3-none-any.whl

Total release size: 18.3 MB

Release files / jpmml_evaluator-0.17.0.tar.gz

Download URL jpmml_evaluator-0.17.0.tar.gz
Size 9.2 MB
Tags Source
SHA-256 checksum
How to use checksums
5dbc672cd7c7a279e9c2fcdd00246147f67184af5e0e4638a396a0b02c653724
BLAKE2b-256 checksum
How to use checksums
07e0024585baa477809baee5cc40e4f634c0577cfbb3f7bebbc041d4a2ddc50a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / jpmml_evaluator-0.17.0-py3-none-any.whl

Download URL jpmml_evaluator-0.17.0-py3-none-any.whl
Size 9.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
5463b445a21ecd0cdab23814a5654e42133568cb0ec66f97fa2aea6b4fa143e7
BLAKE2b-256 checksum
How to use checksums
a702632a99d71b16dc4d5e88a32022f388ad4fd9ce56bcf02fd31283ccca4e99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for jpmml-evaluator 0.17.0

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.

Release history Release notifications | RSS feed

This release

0.17.0 This release

2 release files No provenance

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