Java 类名:com.alibaba.alink.operator.stream.recommendation.SwingRecommStreamOp
Python 类名:SwingRecommStreamOp

功能介绍

Swing 是一种被广泛使用的item召回算法,算法详细介绍可以参考SwingTrainBatchOp组件。
该组件为Swing的流处理预测组件,输入为 SwingTrainBatchOp 输出的模型和要预测的item列。

参数说明

名称中文名称描述类型是否必须?取值范围默认值
itemColItem列列名Item列列名String
recommCol推荐结果列名推荐结果列名String
initRecommCol初始推荐列列名初始推荐列列名String所选列类型为 [M_TABLE]null
k推荐TOP数量推荐TOP数量Integer10
reservedCols算法保留列名算法保留列String[]null
numThreads组件多线程线程个数组件多线程线程个数Integer1
modelStreamFilePath模型流的文件路径模型流的文件路径Stringnull
modelStreamScanInterval扫描模型路径的时间间隔描模型路径的时间间隔,单位秒Integer10
modelStreamStartTime模型流的起始时间模型流的起始时间。默认从当前时刻开始读。使用yyyy-mm-dd hh:mm:ss.fffffffff格式,详见Timestamp.valueOf(String s)Stringnull

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. ["a1", "11L", 2.2],
  6. ["a1", "12L", 2.0],
  7. ["a2", "11L", 2.0],
  8. ["a2", "12L", 2.0],
  9. ["a3", "12L", 2.0],
  10. ["a3", "13L", 2.0],
  11. ["a4", "13L", 2.0],
  12. ["a4", "14L", 2.0],
  13. ["a5", "14L", 2.0],
  14. ["a5", "15L", 2.0],
  15. ["a6", "15L", 2.0],
  16. ["a6", "16L", 2.0],
  17. ])
  18. data = BatchOperator.fromDataframe(df_data, schemaStr='user string, item string, rating double')
  19. model = SwingTrainBatchOp()\
  20. .setUserCol("user")\
  21. .setItemCol("item")\
  22. .linkFrom(data)
  23. predictor = SwingRecommBatchOp()\
  24. .setItemCol("item")\
  25. .setRecommCol("prediction_result")
  26. predictor.linkFrom(model, data).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.SwingRecommBatchOp;
  4. import com.alibaba.alink.operator.batch.recommendation.SwingTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class SwingRecommStreamOpTest {
  10. @Test
  11. public void testSwingRecommStreamOp() throws Exception {
  12. List <Row> df_data = Arrays.asList(
  13. Row.of("a1", "11L", 2.2),
  14. Row.of("a1", "12L", 2.0),
  15. Row.of("a2", "11L", 2.0),
  16. Row.of("a2", "12L", 2.0),
  17. Row.of("a3", "12L", 2.0),
  18. Row.of("a3", "13L", 2.0),
  19. Row.of("a4", "13L", 2.0),
  20. Row.of("a4", "14L", 2.0),
  21. Row.of("a5", "14L", 2.0),
  22. Row.of("a5", "15L", 2.0),
  23. Row.of("a6", "15L", 2.0),
  24. Row.of("a6", "16L", 2.0)
  25. );
  26. BatchOperator <?> data = new MemSourceBatchOp(df_data, "user string, item string, rating double");
  27. BatchOperator <?> model = new SwingTrainBatchOp()
  28. .setUserCol("user")
  29. .setItemCol("item")
  30. .linkFrom(data);
  31. BatchOperator <?> predictor = new SwingRecommBatchOp()
  32. .setItemCol("item")
  33. .setRecommCol("prediction_result");
  34. predictor.linkFrom(model, data).print();
  35. }
  36. }

运行结果

user item rating prediction_result
a6 15L 2.0000 null
a4 13L 2.0000 null
a6 16L 2.0000 null
a5 14L 2.0000 null
a3 13L 2.0000 null
a1 12L 2.0000 {“item”:”[“11L”]”,”score”:”[0.11662912368774414]”}
a2 12L 2.0000 {“item”:”[“11L”]”,”score”:”[0.11662912368774414]”}
a1 11L 2.2000 {“item”:”[“12L”]”,”score”:”[0.12805642187595367]”}
a3 12L 2.0000 {“item”:”[“11L”]”,”score”:”[0.11662912368774414]”}
a4 14L 2.0000 null
a5 15L 2.0000 null
a2 11L 2.0000 {“item”:”[“12L”]”,”score”:”[0.12805642187595367]”}