Python library for converting Scikit-Learn pipelines to PMML
Project description
SkLearn2PMML 
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
Xdataset become input field names. Otherwise, they default tox1,x2, ...,x{n_features_in_}. - The column names of the
ydataset become target field name(s). Otherwise, they default toy(single-output case) ory1,y2, ...,y{n_outputs_}(multi-output case).
- The column names of the
- Perform prediction post-processing using
predict_transform(X),predict_proba_transform(X)andapply_transform(X)methods (operating onpredict_transformer,predict_proba_transformerandapply_transformerattributes, 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 usingContinuousDomain,CategoricalDomainorOrdinalDomainmeta-transformers. Give transformed features meaningful names usingAliasandMultiAliasmeta-transformers.sklearn2pmml.preprocessing. Transform features usingExpressionTransformer(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 usingMemorizerandRecallermeta-transformers.sklearn2pmml.ensemble. Estimate conditionally using theSelectFirstTransformermeta-transformer, plusSelectFirstClassifierandSelectFirstRegressormeta-predictors. Combine predictors usingGBDTLRClassifierandGBDTLMRegressormeta-predictors.sklearn2pmml.postprocessing. Transform predictions using theBusinessDecisionTransformertransformer.
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:
- Training Scikit-Learn GridSearchCV StatsModels pipelines
- Converting Scikit-Learn H2O.ai pipelines to PMML
- Converting customized Scikit-Learn estimators to PMML
- Training Scikit-Learn StatsModels pipelines
- Upgrading Scikit-Learn XGBoost pipelines
- Training Python-based XGBoost accelerated failure time models
- Converting Scikit-Learn PyCaret 3 pipelines to PMML
- Training Scikit-Learn H2O.ai pipelines
- One-hot encoding categorical features in Scikit-Learn XGBoost pipelines
- Training Scikit-Learn TF(-IDF) plus XGBoost pipelines
- Converting Scikit-Learn TF(-IDF) pipelines to PMML
- Converting Scikit-Learn Imbalanced-Learn pipelines to PMML
- Converting logistic regression models to PMML
- Stacking Scikit-Learn, LightGBM and XGBoost models
- Converting Scikit-Learn GridSearchCV pipelines to PMML
- Converting Scikit-Learn TPOT pipelines to PMML
- Converting Scikit-Learn LightGBM pipelines to PMML
Extensions:
- Extending Scikit-Learn with feature cross-references
- Extending Scikit-Learn with UDF expression transformer
- Extending Scikit-Learn with CHAID models
- Extending Scikit-Learn with prediction post-processing
- Extending Scikit-Learn with outlier detector transformer
- Extending Scikit-Learn with date and datetime features
- Extending Scikit-Learn with feature specifications
- Extending Scikit-Learn with GBDT+LR ensemble models
- Extending Scikit-Learn with business rules model
Miscellaneous:
- Upgrading Scikit-Learn decision tree models
- Measuring the memory consumption of Scikit-Learn models
- Benchmarking Scikit-Learn against JPMML-Evaluator
- Analyzing Scikit-Learn feature importances via PMML
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a4e422c0a6a6717cbd4e38d9c8a7f675da27830e1c3b243fda490511c396c02
|
|
| MD5 |
dae0b6ec1ba244aff5a0191a9e6017cf
|
|
| BLAKE2b-256 |
ef69ea01588603eb4ae01efa7fcb081e8f2673c99d9acb67727cd6e0b72b54c0
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc31371d4ee9b3206f97543163ac310ade6c6c11efd5195d96412b3299078d11
|
|
| MD5 |
6401647e47089f63d3489c5004e738b0
|
|
| BLAKE2b-256 |
986fa590b38d081b57c7f9a69fcf4a6acfb2662cd8bfbeeb89bd66893586b15b
|