Java 类名:com.alibaba.alink.operator.batch.evaluation.EvalRankingBatchOp
Python 类名:EvalRankingBatchOp

功能介绍

排序评估是对推荐排序算法的预测结果进行效果评估,支持下列评估指标。

hitRate

排序评估 (EvalRankingBatchOp) - 图1#### averageReciprocalHitRank 排序评估 (EvalRankingBatchOp) - 图2#### map (Mean Average Precision) 排序评估 (EvalRankingBatchOp) - 图3#### ndcgArray (Normalized Discounted Cumulative Gain)

subsetAccuracy

排序评估 (EvalRankingBatchOp) - 图4#### hammingLoss 排序评估 (EvalRankingBatchOp) - 图5#### accuracy 排序评估 (EvalRankingBatchOp) - 图6#### microPrecision 排序评估 (EvalRankingBatchOp) - 图7#### microRecall 排序评估 (EvalRankingBatchOp) - 图8#### microF1 排序评估 (EvalRankingBatchOp) - 图9#### precision 排序评估 (EvalRankingBatchOp) - 图10#### recall 排序评估 (EvalRankingBatchOp) - 图11#### f1

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| labelCol | 标签列名 | 输入表中的标签列名 | String | ✓ | | |

| predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | | |

| labelRankingInfo | Object列列名 | Object列列名 | String | | | “object” |

| predictionRankingInfo | Object列列名 | Object列列名 | String | | | “object” |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["{\"object\":\"[1, 6, 2, 7, 8, 3, 9, 10, 4, 5]\"}", "{\"object\":\"[1, 2, 3, 4, 5]\"}"],
  6. ["{\"object\":\"[4, 1, 5, 6, 2, 7, 3, 8, 9, 10]\"}", "{\"object\":\"[1, 2, 3]\"}"],
  7. ["{\"object\":\"[1, 2, 3, 4, 5]\"}", "{\"object\":\"[]\"}"]
  8. ])
  9. inOp = BatchOperator.fromDataframe(df, schemaStr='pred string, label string')
  10. metrics = EvalRankingBatchOp().setPredictionCol('pred').setLabelCol('label').linkFrom(inOp).collectMetrics()
  11. print(metrics)

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.evaluation.EvalRankingBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.common.evaluation.RankingMetrics;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class EvalRankingBatchOpTest {
  10. @Test
  11. public void testEvalRankingBatchOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of("{\"object\":\"[1, 6, 2, 7, 8, 3, 9, 10, 4, 5]\"}", "{\"object\":\"[1, 2, 3, 4, 5]\"}"),
  14. Row.of("{\"object\":\"[4, 1, 5, 6, 2, 7, 3, 8, 9, 10]\"}", "{\"object\":\"[1, 2, 3]\"}"),
  15. Row.of("{\"object\":\"[1, 2, 3, 4, 5]\"}", "{\"object\":\"[]\"}")
  16. );
  17. BatchOperator <?> inOp = new MemSourceBatchOp(df, "pred string, label string");
  18. RankingMetrics metrics = new EvalRankingBatchOp().setPredictionCol("pred").setLabelCol("label").linkFrom(inOp)
  19. .collectMetrics();
  20. System.out.println(metrics.toString());
  21. }
  22. }

运行结果

  1. -------------------------------- Metrics: --------------------------------
  2. microPrecision:0.32
  3. averageReciprocalHitRank:0.5
  4. precision:0.2667
  5. accuracy:0.2667
  6. f1:0.3761
  7. hitRate:0.6667
  8. microRecall:1
  9. microF1:0.4848
  10. subsetAccuracy:0
  11. recall:0.6667
  12. map:0.355
  13. hammingLoss:0.5667