4

I've found many topics related to this on the Internet but I could find no solutions.

Suppose I want to download any PMML model from this examples list, and run them in Python (Python 3 preferably). Is there any way to do this?

I'm looking for a way to import a PMML that was deployed OUTSIDE Python and import it to use it with this language.

5
  • You better ignore that examples list, because half of these PMML models are either invalid or do not match the corresponding data files - may lead you to think that a (Python-) tool is broken, whereas it's not. Commented Sep 19, 2018 at 18:07
  • @user1808924 Do you know where I could find some working examples with their datasets to try out some code in Python?
    – Tendero
    Commented Sep 19, 2018 at 18:09
  • Possible duplicate of Is there a way to import a pmml file into python?
    – nekomatic
    Commented Sep 20, 2018 at 10:27
  • It's a duplicate because it asks the same thing: 'how can I use a PMML model in Python?'. Unfortunately, as you have found, there is currently no good answer to this because there doesn't appear to be any good native Python PMML importer. If there were one, it would be the answer to that question as well as this one, so they're duplicates.
    – nekomatic
    Commented Sep 21, 2018 at 8:10
  • A couple of options are mentioned in this question, if you haven't found it already, but it doesn't look like they're complete or up-to-date.
    – nekomatic
    Commented Sep 21, 2018 at 8:14

3 Answers 3

8

You could use PyPMML to apply PMML in Python, for example:

from pypmml import Model

model = Model.fromFile('DecisionTreeIris.pmml')
result = model.predict({
    "Sepal_Length" : 5.1,
    "Sepal_Width" : 3.5,
    "Petal_Length" : 1.4,
    "Petal_Width" : 0.2
})

For more info about other PMML libraries, be free to see: https://github.com/autodeployai

1
  • FYI, PyPMML needs java
    – masaya
    Commented Nov 16, 2022 at 13:30
1

After some research I found the solution to this: the 'openscoring' library.

Using it is very simple:

import subprocess
from openscoring import Openscoring
import numpy as np

p = subprocess.Popen('java -jar openscoring-server-executable-1.4.3.jar',
             shell=True)

os = Openscoring("http://localhost:8080/openscoring")

# Deploying a PMML document DecisionTreeIris.pmml as an Iris model:   
os.deployFile("Iris", "DecisionTreeIris.pmml")

# Evaluating the Iris model with a data record:
arguments = {
    "Sepal_Length" : 5.1,
    "Sepal_Width" : 3.5,
    "Petal_Length" : 1.4,
    "Petal_Width" : 0.2
}
result = os.evaluate("Iris", arguments)
print(result)

This returns the value of the target variable in a dictionary. There is no need to go outside of Python to use PMML models anymore (you just have to run the server with Java, which can be done with Python as well as I showed above).

1
  • 1
    We need to use the latest version of the jar if we are using this solution. Posting here so that it helps everyone
    – NG_21
    Commented Aug 24, 2020 at 5:22
0

Isn't it like trying to host H2O models in python apps? Looks like a bridge from python to Java is required here too. Such bridges are not stable at all, been there, tested them. Just a general suggestion: do not mix languages between ML algos and apps code, train in python, serve in python, re-validate also in python. Legacy R and H2O models can be always re-fitted in python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.