Skip to content

jpmml/sklearn2pmml

Repository files navigation

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.132.0 (30 June, 2026):

pip install sklearn2pmml==0.132.0

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

About

Python library for converting Scikit-Learn pipelines to PMML

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors