我有一条sklearn管道。其步骤之一是
ExpressionTransformer(f"round(X[0]*{nRoundMin})/{nRoundMin}")
(从sklearn2pmml.preprocessing导入ExpressionTransformer)。当我尝试将此管道写入PMML文件时,出现以下错误:
Standard output is empty
Standard error:
Jun 27, 2019 8:39:55 AM org.jpmml.sklearn.Main run
INFO: Parsing PKL..
Jun 27, 2019 8:40:53 AM org.jpmml.sklearn.Main run
INFO: Parsed PKL in 58085 ms.
Jun 27, 2019 8:40:53 AM org.jpmml.sklearn.Main run
INFO: Converting..
Jun 27, 2019 8:41:03 AM org.jpmml.sklearn.Main run
SEVERE: Failed to convert
java.lang.IllegalArgumentException: round
at org.jpmml.sklearn.ExpressionTranslator.translateFunction(ExpressionTranslator.java:94)
at org.jpmml.sklearn.ExpressionTranslator.FunctionInvocationExpression(ExpressionTranslator.java:513)
at org.jpmml.sklearn.ExpressionTranslator.PrimaryExpression(ExpressionTranslator.java:416)
at org.jpmml.sklearn.ExpressionTranslator.UnaryExpression(ExpressionTranslator.java:374)
at org.jpmml.sklearn.ExpressionTranslator.MultiplicativeExpression(ExpressionTranslator.java:316)
at org.jpmml.sklearn.ExpressionTranslator.AdditiveExpression(ExpressionTranslator.java:280)
at org.jpmml.sklearn.ExpressionTranslator.ComparisonExpression(ExpressionTranslator.java:228)
at org.jpmml.sklearn.ExpressionTranslator.NegationExpression(ExpressionTranslator.java:217)
at org.jpmml.sklearn.ExpressionTranslator.LogicalAndExpression(ExpressionTranslator.java:187)
at org.jpmml.sklearn.ExpressionTranslator.LogicalOrExpression(ExpressionTranslator.java:166)
at org.jpmml.sklearn.ExpressionTranslator.IfElseExpression(ExpressionTranslator.java:147)
at org.jpmml.sklearn.ExpressionTranslator.Expression(ExpressionTranslator.java:140)
at org.jpmml.sklearn.ExpressionTranslator.translateExpressionInternal(ExpressionTranslator.java:134)
at org.jpmml.sklearn.ExpressionTranslator.translate(ExpressionTranslator.java:33)
at org.jpmml.sklearn.ExpressionTranslator.translate(ExpressionTranslator.java:22)
at sklearn2pmml.preprocessing.ExpressionTransformer.encodeFeatures(ExpressionTransformer.java:43)
at sklearn.pipeline.Pipeline.encodeFeatures(Pipeline.java:81)
尽管round
在PMML内置函数(http://dmg.org/pmml/v4-2-1/BuiltinFunctions.html#math)中。我应该如何编写此流水线步骤来正确获取round函数?
如果我在上面的ExpressionTransfomer
中删除了round函数,则创建PMML不会出现任何问题。
我还尝试将上述ExpressionTransfomer
替换为
Pipeline([
("i", ExpressionTransformer("X[0]*"+str(nRoundMin))),
("r", FunctionTransformer(np.round)),
("f", ExpressionTransformer("X[0]/"+str(nRoundMin)))
])
和
Pipeline([
("i", ExpressionTransformer("X[0]*"+str(nRoundMin))),
("r", FunctionTransformer(round)),
("f", ExpressionTransformer("X[0]/"+str(nRoundMin)))
])
但这没有帮助。
[编辑]
按照user1808924的建议,我还尝试了ExpressionTransformer(f"numpy.rint(X[0]*{nRoundMin})/{nRoundMin}")
,它产生了类似的错误:
Standard output is empty
Standard error:
Jun 27, 2019 12:38:51 PM org.jpmml.sklearn.Main run
INFO: Parsing PKL..
Jun 27, 2019 12:39:54 PM org.jpmml.sklearn.Main run
INFO: Parsed PKL in 63076 ms.
Jun 27, 2019 12:39:54 PM org.jpmml.sklearn.Main run
INFO: Converting..
Jun 27, 2019 12:40:05 PM org.jpmml.sklearn.Main run
SEVERE: Failed to convert
java.lang.IllegalArgumentException: numpy.rint
at org.jpmml.sklearn.ExpressionTranslator.translateFunction(ExpressionTranslator.java:94)
at org.jpmml.sklearn.ExpressionTranslator.FunctionInvocationExpression(ExpressionTranslator.java:513)
at org.jpmml.sklearn.ExpressionTranslator.PrimaryExpression(ExpressionTranslator.java:416)
at org.jpmml.sklearn.ExpressionTranslator.UnaryExpression(ExpressionTranslator.java:374)
at org.jpmml.sklearn.ExpressionTranslator.MultiplicativeExpression(ExpressionTranslator.java:316)
at org.jpmml.sklearn.ExpressionTranslator.AdditiveExpression(ExpressionTranslator.java:280)
at org.jpmml.sklearn.ExpressionTranslator.ComparisonExpression(ExpressionTranslator.java:228)
at org.jpmml.sklearn.ExpressionTranslator.NegationExpression(ExpressionTranslator.java:217)
at org.jpmml.sklearn.ExpressionTranslator.LogicalAndExpression(ExpressionTranslator.java:187)
at org.jpmml.sklearn.ExpressionTranslator.LogicalOrExpression(ExpressionTranslator.java:166)
at org.jpmml.sklearn.ExpressionTranslator.IfElseExpression(ExpressionTranslator.java:147)
at org.jpmml.sklearn.ExpressionTranslator.Expression(ExpressionTranslator.java:140)
at org.jpmml.sklearn.ExpressionTranslator.translateExpressionInternal(ExpressionTranslator.java:134)
at org.jpmml.sklearn.ExpressionTranslator.translate(ExpressionTranslator.java:33)
at org.jpmml.sklearn.ExpressionTranslator.translate(ExpressionTranslator.java:22)
at sklearn2pmml.preprocessing.ExpressionTransformer.encodeFeatures(ExpressionTransformer.java:43)
答案 0 :(得分:0)
ExpressionTransformer
期望一个字符串参数,该字符串参数是有效的Python表达式(可在本地工作空间环境中评估)。因此,如果要应用舍入,则需要使用一些内置的Python函数。
例如,您可以使用Numpy's rint
universal function:
transformer = ExpressionTransformer("numpy.rint(X[0] / X[1])")