问题描述
有人可以帮忙解决这个错误吗?我正在 Pyspark 中编程,我正在尝试使用以下代码计算某个偏差:
Result = data.select(count(((coalesce(data["pred"],lit(0)))!=0 & (coalesce(data["val"],lit(0)) !=0
& (abs(coalesce(data["pred"],lit(0)) - coalesce(data["val"],lit(0)))/(coalesce(data["val"],lit(0)))) > 0.1))))
出现以下错误:
"Py4JError: An error occurred while calling o230.and. Trace:
py4j.Py4JException: Method and([class java.lang.Integer]) does not exist
at py4j.reflection.ReflectionEngine.getmethod(ReflectionEngine.java:318)
at py4j.reflection.ReflectionEngine.getmethod(ReflectionEngine.java:326)
at py4j.Gateway.invoke(Gateway.java:274)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.lang.Thread.run(Thread.java:748)"
我对使用 pyspark 编程非常陌生,根本无法发现我的代码有什么问题;我用非常相似的代码做了一个非常相似的计算,但效果很好……有人知道这个问题吗?
Abs_avg = data.select(avg(abs(coalesce(data["pred"],lit(0)))))
解决方法
条件需要用括号括起来,否则会解释为0 & something
。此外,您不需要将 ...
包裹在 (...) != 0
中。
Result = data.select(
count(
(coalesce(data["pred"],lit(0)) != 0) &
(coalesce(data["val"],lit(0)) != 0) &
(abs(
coalesce(data["pred"],lit(0)) -
coalesce(data["val"],lit(0))
) / coalesce(data["val"],lit(0)) > 0.1
)
)
)