如何在pyspark中使用MultiClassMetrics计算f-score?

时间:2017-01-17 21:31:55

标签: python apache-spark machine-learning pyspark rdd

正如我在pyspark文档中看到的那样,fmeasure()函数有两个参数labelbeta

fMeasure(label=None, beta=None)

这里有什么测试版?

我使用的是RDD中非常简单的数据集: (它在数据帧中,但我将其转换为RDD)

+----------+-----+
|prediction|label|
+----------+-----+
|         1|    1|
|         1|    1|
|         0|    1|
|         0|    0|
|         1|    0|
|         1|    0|
|         0|    0|
|         0|    0|
|         1|    1|
|         1|    1|
+----------+-----+

当我运行此命令时:

multi_metrics = MulticlassMetrics(rdd)
print 'fMeasure: ', multi_metrics.fMeasure(1)

我收到此错误:

print 'fMeasure: ', multi_metrics.fMeasure(1)
  File "/usr/hdp/current/spark-client/python/pyspark/mllib/evaluation.py", line 259, in fMeasure
    return self.call("fMeasure", label)
  File "/usr/hdp/current/spark-client/python/pyspark/mllib/common.py", line 146, in call
    return callJavaFunc(self._sc, getattr(self._java_model, name), *a)
  File "/usr/hdp/current/spark-client/python/pyspark/mllib/common.py", line 123, in callJavaFunc
    return _java2py(sc, func(*args))
  File "/usr/hdp/current/spark-client/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py", line 813, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "/usr/hdp/current/spark-client/python/pyspark/sql/utils.py", line 45, in deco
    return f(*a, **kw)
  File "/usr/hdp/current/spark-client/python/lib/py4j-0.9-src.zip/py4j/protocol.py", line 312, in get_return_value
    format(target_id, ".", name, value))
Py4JError: An error occurred while calling o154.fMeasure. Trace:
py4j.Py4JException: Method fMeasure([class java.lang.Integer]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:335)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:344)
    at py4j.Gateway.invoke(Gateway.java:252)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:209)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:2)

  

这里有什么测试版?

Spark的MulticlassMetrics实现了$ F _ {\ beta} $ - measure,如果你把$ \ beta $ = 1,那么它与传统的F-measure一致。$ \ beta $参数允许{{3} }。

关于错误:如果查看实现,它实际上需要Double。这是control relative contributions of precision vs recall in F-measure方法的pyspark包装器,这里是实际的fMeasure(在Scala中)。

所以你应该好好这样称呼它,例如:

multi_metrics = MulticlassMetrics(rdd)
print 'fMeasure: ', multi_metrics.fMeasure(1.0,1.0)