Skip to main content

Python library for converting Scikit-Learn pipelines to PMML

Project description

SkLearn2PMML Build Status

Python package for converting Scikit-Learn pipelines to PMML.

Features

This package is a thin Python wrapper around the JPMML-SkLearn library.

News and Updates

The current version is 0.130.1 (2 June, 2026):

pip install sklearn2pmml==0.130.1

See the NEWS.md file.

Prerequisites

  • Java 11 or newer. The Java executable must be available on system path.
  • Python 3.8 or newer.

Installation

Installing a release version from PyPI:

pip install sklearn2pmml

Alternatively, installing the latest snapshot version from GitHub:

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

Usage

Native Scikit-Learn

SkLearn2PMML can convert a wide variety of Scikit-Learn and Scikit-Learn adjacent estimators as-is.

The list of supported transformer, selector and predictor (aka model) classes is given in the features.md file of the JPMML-SkLearn project.

Keep SkLearn2PMML maximally up-to-date. One and the same package version -- preferably the latest and greatest -- is able to work with all Scikit-Learn 0.17 (ca 2015) and newer versions.

Library

Use the sklearn2pmml.sklearn2pmml(estimator, pmml_path) utility function to convert a fitted estimator object to PMML:

from sklearn2pmml import sklearn2pmml

estimator = ...
estimator.fit(X, y)

# Convert a live estimator object
sklearn2pmml(estimator, "Estimator.pmml")

The estimator argument may also be a path-like object to an estimator pickle file in local filesystem:

from sklearn2pmml import sklearn2pmml

import joblib

joblib.dump(estimator, "Estimator.pkl")

sklearn2pmml("Estimator.pkl", "Estimator.pmml")

SkLearn2PMML uses a custom Java component (rather than the built-in Python unpickler component) for reading pickle files. As such, it is safe to use with unvetted pickle files.

Command-line application

The sklearn2pmml module is executable.

The main application simply calls the sklearn2pmml.sklearn2pmml() utility function. At minimum, it is necessary to provide the input pickle file (-i or --input; supports joblib, pickle or dill variants) and output PMML file paths (-o or --output):

python -m sklearn2pmml --input Estimator.pkl --output Estimator.pmml

To see all supported command-line options, pass --help:

python -m sklearn2pmml --help

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

sklearn2pmml --input pipeline.pkl --output pipeline.pmml

PMML-enhanced Scikit-Learn

Native Scikit-Learn estimators have rather limited portability between environments, because they lack adequate metadata. For example, they did not collect and store even the most crucial metadata about the feature matrix (ie. the feature_names_in_ attribute) prior to Scikit-Learn 1.0 (ca 2021).

SkLearn2PMML provides the sklearn2pmml.pipeline.PMMLPipeline meta-estimator class, which extends the sklearn.pipeline.Pipeline class with the following functionality:

  • Collect feature and label metadata using the fit(X, y) method:
    • The column names of the X dataset become input field names. Otherwise, they default to x1, x2, ..., x{n_features_in_}.
    • The column names of the y dataset become target field name(s). Otherwise, they default to y (single-output case) or y1, y2, ..., y{n_outputs_} (multi-output case).
  • Perform prediction post-processing using predict_transform(X), predict_proba_transform(X) and apply_transform(X) methods (operating on predict_transformer, predict_proba_transformer and apply_transformer attributes, respectively).
  • Embed model verification data using the verify(X) method.
  • Configure the representation of final estimator step using the configure(**pmml_options) method.
  • Perform extra edits (ie. insert, update or delete PMML XML fragments) on the PMML document using the customize(command, xpath_expr, pmml_element) method.

PMML-enhanced workflow:

#from sklearn.pipeline import Pipeline
from sklearn2pmml import sklearn2pmml
from sklearn2pmml.pipeline import PMMLPipeline

#pipeline = Pipeline(...)
# Activate prediction post-processing
pipeline = PMMLPipeline(..., predict_transformer = ...)
pipeline.fit(X, y)

# Embed small but representative sample for self-check purposes during deployment
pipeline.verify(X.sample(n = 10))

# Default prediction
yt = pipeline.predict(X)
# Default prediction, together with its transformation results
yt_transformed = pipeline.predict_transform(X)

# Default PMML representation
sklearn2pmml(pipeline, "Pipeline.pmml")

pipeline.configure(...)
#pipeline.customize(...)

# Customized PMML representation
sklearn2pmml(pipeline, "Pipeline-customized.pmml")

Additionally, SkLearn2PMML provides a number of PMML-oriented transformer, selector and predictor classes:

  • sklearn2pmml.decoration. Capture or declare the domain of individual features by their operational type using ContinuousDomain, CategoricalDomain or OrdinalDomain meta-transformers. Give transformed features meaningful names using Alias and MultiAlias meta-transformers.
  • sklearn2pmml.preprocessing. Transform features using ExpressionTransformer (any to any), CutTransformer (continuous to discrete), LookupTransformer (discrete to discrete), and many other transformers.
  • sklearn2pmml.cross_reference. Cross-reference features and transformed features at subsequent transformer steps using Memorizer and Recaller meta-transformers.
  • sklearn2pmml.ensemble. Estimate conditionally using the SelectFirstTransformer meta-transformer, plus SelectFirstClassifier and SelectFirstRegressor meta-predictors. Combine predictors using GBDTLRClassifier and GBDTLMRegressor meta-predictors.
  • sklearn2pmml.postprocessing. Transform predictions using the BusinessDecisionTransformer transformer.

For example, mapping and pre-processing the Audit dataset:

from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn2pmml.decoration import Alias, CategoricalDomain, ContinuousDomain
from sklearn2pmml.preprocessing import ExpressionTransformer

import pandas

df = pandas.read_csv("Audit.csv")

# Group features by type (operational type plus data type)
cat_cols = ["Education", "Employment", "Marital", "Occupation", "Gender"]
cont_int_cols = ["Age", "Hours"]
cont_float_cols = ["Income"]

transformer = ColumnTransformer([
	# Features
	("cat", make_pipeline(CategoricalDomain(), OneHotEncoder()), cat_cols),
	("cont_int", ContinuousDomain(), cont_int_cols),
	("cont_float", ContinuousDomain(), cont_float_cols),
	# Transformed features
	("hourly_income", Alias(ExpressionTransformer("X['Income'] / (X['Hours'] * 52)"), name = "Hourly_Income"), ["Income", "Hours"])
], remainder = "drop")
transformer.fit(df)

Xt = transformer.transform(df)

Documentation

Integrations:

Extensions:

Miscellaneous:

Archived:

License

SkLearn2PMML is licensed under the terms and conditions of the GNU Affero General Public License, Version 3.0.

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

Additional information

SkLearn2PMML is developed and maintained by Openscoring Ltd, Estonia.

Interested in using Java PMML API software in your company? Please contact info@openscoring.io

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sklearn2pmml-0.131.0.tar.gz (7.5 MB view details)

Uploaded Source

Built Distribution

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

sklearn2pmml-0.131.0-py3-none-any.whl (7.5 MB view details)

Uploaded Python 3

File details

Details for the file sklearn2pmml-0.131.0.tar.gz.

File metadata

  • Download URL: sklearn2pmml-0.131.0.tar.gz
  • Upload date:
  • Size: 7.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for sklearn2pmml-0.131.0.tar.gz
Algorithm Hash digest
SHA256 1a4e422c0a6a6717cbd4e38d9c8a7f675da27830e1c3b243fda490511c396c02
MD5 dae0b6ec1ba244aff5a0191a9e6017cf
BLAKE2b-256 ef69ea01588603eb4ae01efa7fcb081e8f2673c99d9acb67727cd6e0b72b54c0

See more details on using hashes here.

File details

Details for the file sklearn2pmml-0.131.0-py3-none-any.whl.

File metadata

  • Download URL: sklearn2pmml-0.131.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for sklearn2pmml-0.131.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc31371d4ee9b3206f97543163ac310ade6c6c11efd5195d96412b3299078d11
MD5 6401647e47089f63d3489c5004e738b0
BLAKE2b-256 986fa590b38d081b57c7f9a69fcf4a6acfb2662cd8bfbeeb89bd66893586b15b

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