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.

Tendero
  • 1,136
  • 2
  • 19
  • 34
  • 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. – user1808924 Sep 19 '18 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 Sep 19 '18 at 18:09
  • Possible duplicate of [Is there a way to import a pmml file into python?](https://stackoverflow.com/questions/42722322/is-there-a-way-to-import-a-pmml-file-into-python) – nekomatic Sep 20 '18 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 Sep 21 '18 at 08:10
  • A couple of options are mentioned in [this question](https://stackoverflow.com/questions/15577427/apply-pmml-predictor-model-in-python), if you haven't found it already, but it doesn't look like they're complete or up-to-date. – nekomatic Sep 21 '18 at 08:14

3 Answers3

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

PredictFuture
  • 216
  • 2
  • 6
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).

Tendero
  • 1,136
  • 2
  • 19
  • 34
  • 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 Aug 24 '20 at 05: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.

mirekphd
  • 4,799
  • 3
  • 38
  • 59