Closed mkuiack closed 3 years ago
I've changing the expression to
1 if ((X[0] % 10.) <= 0.1) or ((X[0] % 10.) >= 9.9) else 0
This solved the issue with sklearn2pmml, but in python this expression would return
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Moved this issue to the JPMML-Python project, because this is where the expression translator component actually resides.
The expression is valid python, but there seems to be an issue with the translation of | to LogicalOr?
For simplicity's sake, the grammar only defines boolean comparison operators 'and', 'or' and 'not`: https://github.com/jpmml/jpmml-python/blob/master/src/main/javacc/expression.jj#L350-L354
Haven't considered bitwise comparison operators, because I'm not entirely sure that they're functionally equivalent to boolean comparison operators. Also, it needs to be verified that they are functionally compatible with PMML built-in functions and
, or
and not
.
IIRC, PMML built-in function and
behaves more like the &&
operator (terms are evaluated lazily from left to right), and less like the &
operator (all terms are evaluated eagerly, even if some term is already known to have returned a false/0 value).
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
This error is raised by the Numpy's apply method: https://github.com/jpmml/sklearn2pmml/blob/0.74.4/sklearn2pmml/util/__init__.py#L18
Can you rewrite the sklearn2pmml.util.eval_rows()
function so that boolean comparison operators and
and or
would be tolerated?
https://github.com/jpmml/sklearn2pmml/blob/0.74.4/sklearn2pmml/util/__init__.py#L16-L23
Pardon my limited Python skills.
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Here's an StackOverflow answer, which suggests using numpy.logical_and
and numpy.logical_or
functions (as a replacement for and
and or
operators, respectively):
https://stackoverflow.com/a/45380236/
Looks like bitwise operators are verboten, but you could do:
import numpy
transformer = ExpressionTransformer("1 if numpy.logical_or((X[0] % 10.) <= 0.1, (X[0] % 10.) >= 9.9) else 0")
I can have a look, but my skills are also limited 😄
How would the numpy.logical_or be interpreted if the pmml is deployed on a system which doesn't have python or numpy installed?
How would the numpy.logical_or be interpreted if the pmml is deployed on a system which doesn't have python or numpy installed?
There is no deployment-time Numpy dependency whatsoever, because Numpy logical functions are translated to PMML built-in functions: http://dmg.org/pmml/v4-4/BuiltinFunctions.html#boolean3
The updated JPMML-Python library is included in the latest SkLearn2PMML version 0.75.0 (released this morning). Please upgrade, and see if your issue has been solved.
Upgrading to 0.75 and using numpy.logical_and solves the issue.
I'm writing a feature generation preprocessing step which checks if a value is close to being a round factor of 10, using the ExpressionTransformer
The expression is valid python, but there seems to be an issue with the translation of | to LogicalOr?