Java 类名:com.alibaba.alink.operator.batch.recommendation.LeaveKObjectOutBatchOp
Python 类名:LeaveKObjectOutBatchOp

功能介绍

将推荐结果分成两个表。

参数说明

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

| groupCol | 分组列 | 分组单列名,必选 | String | ✓ | | |

| objectCol | Object列列名 | Object列列名 | String | ✓ | | |

| outputCol | 输出结果列列名 | 输出结果列列名,必选 | String | ✓ | | |

| fraction | 拆分到测试集最大数据比例 | 拆分到测试集最大数据比例 | Double | | [0.0, 1.0] | 1.0 |

| k | 推荐TOP数量 | 推荐TOP数量 | Integer | | | 10 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. [1, 1, 0.6],
  6. [2, 2, 0.8],
  7. [2, 3, 0.6],
  8. [4, 0, 0.6],
  9. [6, 4, 0.3],
  10. [4, 7, 0.4],
  11. [2, 6, 0.6],
  12. [4, 5, 0.6],
  13. [4, 6, 0.3],
  14. [4, 3, 0.4]
  15. ])
  16. data = BatchOperator.fromDataframe(df_data, schemaStr='user bigint, item bigint, rating double')
  17. spliter = LeaveKObjectOutBatchOp()\
  18. .setK(2)\
  19. .setGroupCol("user")\
  20. .setObjectCol("item")\
  21. .setOutputCol("label")
  22. spliter.linkFrom(data).print()
  23. spliter.getSideOutput(0).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.recommendation.LeaveKObjectOutBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class LeaveKObjectOutBatchOpTest {
  9. @Test
  10. public void testLeaveKObjectOutBatchOp() throws Exception {
  11. List <Row> df_data = Arrays.asList(
  12. Row.of(1, 1, 0.6),
  13. Row.of(2, 2, 0.8),
  14. Row.of(2, 3, 0.6),
  15. Row.of(4, 0, 0.6),
  16. Row.of(6, 4, 0.3),
  17. Row.of(4, 7, 0.4),
  18. Row.of(2, 6, 0.6),
  19. Row.of(4, 5, 0.6),
  20. Row.of(4, 6, 0.3),
  21. Row.of(4, 3, 0.4)
  22. );
  23. BatchOperator <?> data = new MemSourceBatchOp(df_data, "user int, item int, rating double");
  24. BatchOperator <?> spliter = new LeaveKObjectOutBatchOp()
  25. .setK(2)
  26. .setGroupCol("user")
  27. .setObjectCol("item")
  28. .setOutputCol("label");
  29. spliter.linkFrom(data).print();
  30. spliter.getSideOutput(0).print();
  31. }
  32. }

运行结果

| user | label | | —- | —- |

| 1 | {“item”:”[1]”} |

| 6 | {“item”:”[4]”} |

| 4 | {“item”:”[7,3]”} |

| 2 | {“item”:”[2,6]”} |

| user | item | rating | | —- | —- | —- |

| 4 | 5 | 0.6000 |

| 4 | 3 | 0.4000 |

| 4 | 0 | 0.6000 |

| 2 | 3 | 0.6000 |