Skip to content

The MiningField@invalidValueTreatment attribute gets silently overriden by OneHotEncoder transformer #428

Open
@kausmees

Description

@kausmees

Hello

I'm trying to create a PMML file that includes a specification for how to handle invalid values for both numerical and categorical features. I'm getting the PMML output I expect for the numerical features, but the categorical features don't have any tags mentioning invalidValueTreatment in the schema.

I've tried two things: handling invalid values as missing and handling invalid values by assigning them a specific value (both in code below), but am getting the same result.

Reproducible example:

import pandas as pd
import numpy as np 

from sklearn_pandas import DataFrameMapper
from sklearn2pmml.decoration import CategoricalDomain, ContinuousDomain
from sklearn2pmml import sklearn2pmml
from sklearn2pmml.pipeline import PMMLPipeline
from sklearn.preprocessing import OneHotEncoder
from jpmml_evaluator import make_evaluator
import xgboost as xgb

df = pd.DataFrame([(1, 1, 'a'), (0, 2,'a'), (1, 3, 'b'), (1, 4, 'b'), (0, 5, 'c'), (1, 6, 'c'), (0, 7, 'c')], columns = ['Y', 'X1', 'X2'])

print(df)

numerical_cols = ['X1']
categorical_cols = ['X2']

### attempt 1: handle invalid values as missing
mapper = DataFrameMapper([
  (categorical_cols, [CategoricalDomain(invalid_value_treatment = "as_missing",
                                        missing_value_replacement = "OTHER"), OneHotEncoder()]),    
  (numerical_cols, [ContinuousDomain(invalid_value_treatment = "as_missing", 
                                     missing_value_replacement = -1)]),
])

### attempt 2: assign invalid values a new value
# mapper = DataFrameMapper([
#   (categorical_cols, [CategoricalDomain(invalid_value_treatment = "as_value",
#                                         invalid_value_replacement = "OTHER",
#                                         missing_value_replacement = "OTHER"), OneHotEncoder()]),    
#   (numerical_cols, [ContinuousDomain(invalid_value_treatment = "as_missing", 
#                                      missing_value_replacement = -1)]),
# ])



classifier = xgb.XGBClassifier(n_estimators=500,
                               objective="binary:logistic",
                               random_state=0,
                               learning_rate=0.1,
                               missing=np.nan,
                               scale_pos_weight=3000,
                               max_depth=8
                              )

pipeline = PMMLPipeline([
  ("mapper", mapper),
  ("classifier", classifier)
])

pipeline.fit(df[df.columns.difference(["Y"])], df["Y"])

sklearn2pmml(pipeline, "pipeline_test.pmml")


## evaluate

evaluator = make_evaluator("pipeline_test.pmml").verify()

TEST_INPUT_1 = {
  "X1": 1,
  "X2": "a"
}

TEST_INPUT_2 = {
  "X1": 1,
  "X2": "d"
}

print(evaluator.evaluate(TEST_INPUT_1))
print(evaluator.evaluate(TEST_INPUT_2))

Evaluation of TEST_INPUT_2 using jpmml_evaluator results in an error

jpmml_evaluator.JavaError: org.jpmml.evaluator.ValueCheckException: Field "X2" cannot accept invalid value "d"

without the invalidValueTreatment tags, but if I manually add invalidValueTreatment="asMissing" for X2 in the PMML file, then evaluation works as expected.

Is there any way to get the PMML output to contain info on how to handle invalid values, or have I missed something about the intended behavior here? Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions