问题
I have trained a model using sklearn and exported it into a pmml format using sklearn2pmml. Is there a way to convert that pmml file back into something that can be imported and run in python?
The reason I am looking to do this is because I have noticed slight differences in the way the pmml model behaves compared to the sklearn model. Specifically, the pmml file sets hard upper and lower bounds for variables (uses the max and min of the variable in the training set) whereas sklearn does not. I encounter problems when the pmml model encounters data that is outside of these bounds. This is just one difference between the pmml model and the sklearn model and I want to be able to re-import the pmml file into python to run it and see if there are any others.
回答1:
You don't need to test the correctness of sklearn2pmml generated models. It's based on the JPMML-SkLearn library, which has full coverage with integration tests - Scikit-Learn predictions and PMML predictions are provably identical.
Your real issue is that you want to apply models outside of their intended "applicability domain". It's a bead idea, because model's behaviour is not specified in that case - garbage input, garbage predictions.
However, if you insist that you must be able to feed garbage to your models in production environment, then simply disable PMML value bounds checking. There are many ways how this can be accomplished:
- Remove
Value
andInterval
child elements from/PMML/DataDictionary/DataField
elements. - Modify
Value
andInterval
child elements so that those previously unseen values would be recognized as valid values. For example, you can define the margins of theInput
element to include all values [-Inf, +Inf]. See the explanation of Value and Interval elements in the PMML specification for correct syntax. - Change the
invalidValueTreatment
attribute value of all/PMML/<Model>/MiningSchema/MiningField
elements from "returnInvalid" to "asIs". If this attribute is missing, then it defaults to "returnInvalid". So you'd need to insertinvalidValueTreatment=asIs
there.
I would recommend option #3. You can automate the process using JPMML-Model library:
org.dmg.pmml.PMML pmml = loadFromFile(..)
org.dmg.pmml.Visitor mfUpdater = new org.jpmml.model.visitors.AbstractVisitor(){
@Override
public VisitorAction visit(MiningField miningField){
miningField.setInvalidValueTreatment(InvalidValueTreatmentMethod.AS_IS);
return VisitorAction.CONTINUE;
}
}
mfUpdater.applyTo(pmml);
saveToFile(pmml, ...)
来源:https://stackoverflow.com/questions/42722322/is-there-a-way-to-import-a-pmml-file-into-python