Convert Decision Tree with default condition to PMML using sklearn2pmml

127 views
Skip to first unread message

NAYAN GUPTA

unread,
Aug 20, 2020, 3:29:28 PM8/20/20
to Java PMML API
Hi Villu, 

Hope you are safe, healthy and doing well .
I am using scikit-learn to train a decision tree  and am able to convert it into a PMML file using the sklearn2pmml library . 

However, in some of the requirements . I need to introduce a default condition in the PMML  along with the trained decision tree. 

For example I have explained here
https://stackoverflow.com/questions/63511829/convert-decision-tree-with-default-condition-to-pmml-using-sklearn2pmml

A high level logic is as follows

if  some_condition == True:
     score = x
     return(score)

else:
  score = Decision_Tree(x)
  return(score)

Is is possible to create such a node in PMML ? 
Has anyone in the group tried it out. 

Thanks
Nayan Gupta

Villu Ruusmann

unread,
Aug 20, 2020, 3:48:36 PM8/20/20
to Java PMML API
Hi Nayan,
It's easy - see the sklearn2pmml.ensemble.SelectFirstClassifier estimator type:
https://github.com/jpmml/sklearn2pmml/blob/0.61.0/sklearn2pmml/ensemble/__init__.py#L181-L199

>
> if some_condition == True:
> score = x
> return(score)
> else:
> score = Decision_Tree(x)
> return(score)
>

As the name suggests, SelectFirstClassifier returns the prediction of
the first child estimator whose "controlling predicate" evaluates to
True. For constant prediction you could use a
sklearn.dummy.DummyClassifier estimator.

Something like this:

classifier = SelectFirstClassifier([
("condition", "X['A1'] == 'Beta'", DummyClassifier(..)),
("default", "True", DecisionTreeClassifier(..))
])


Villu

NAYAN GUPTA

unread,
Aug 21, 2020, 2:11:59 AM8/21/20
to Villu Ruusmann, Java PMML API
Hi Villu, Thanks for the prompt reply. Highly appreciate your support. 

I tried what you are suggesting and I am getting the following error in the last step of the code. 

Here is the error message : 
java.lang.IllegalArgumentException: The transformer object (Python class sklearn2pmml.ensemble.SelectFirstEstimator) is not a supported Transformer

Attaching the python code to reproduce the example 
Python version 3.5.x
sklearn2pmml version 0.61








Best Regards
Nayan Gupta,
Data Science , Robert Bosch Gmbh



select_first_classifier.py

Villu Ruusmann

unread,
Aug 21, 2020, 1:54:12 PM8/21/20
to Java PMML API
Hi Nayan,

>
> java.lang.IllegalArgumentException: The transformer object
> (Python class sklearn2pmml.ensemble.SelectFirstEstimator)
> is not a supported Transformer
>

Class SelectFirstEstimator is an abstract base class that has two
concrete subclasses - SelectFirstClassifier and SelectFirstRegressor.

As you're dealing with a classification-type problem, then you should
be using SelectFirstClassifier.


Villu
Reply all
Reply to author
Forward
0 new messages