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

功能介绍

用itemCF模型为实时item推荐相似的item list。

参数说明

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

代码示例

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, 1, 0.6],
  9. [4, 2, 0.3],
  10. [4, 3, 0.4],
  11. ])
  12. data = BatchOperator.fromDataframe(df_data, schemaStr='user bigint, item bigint, rating double')
  13. sdata = StreamOperator.fromDataframe(df_data, schemaStr='user bigint, item bigint, rating double')
  14. model = ItemCfTrainBatchOp()\
  15. .setUserCol("user")\
  16. .setItemCol("item")\
  17. .setRateCol("rating").linkFrom(data);
  18. predictor = ItemCfSimilarItemsRecommStreamOp(model)\
  19. .setItemCol("item")\
  20. .setReservedCols(["item"])\
  21. .setK(1)\
  22. .setRecommCol("prediction_result");
  23. predictor.linkFrom(sdata).print()
  24. StreamOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.recommendation.ItemCfTrainBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.stream.StreamOperator;
  6. import com.alibaba.alink.operator.stream.recommendation.ItemCfSimilarItemsRecommStreamOp;
  7. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  8. import org.junit.Test;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class ItemCfSimilarItemsRecommStreamOpTest {
  12. @Test
  13. public void testItemCfSimilarItemsRecommStreamOp() throws Exception {
  14. List <Row> df_data = Arrays.asList(
  15. Row.of(1, 1, 0.6),
  16. Row.of(2, 2, 0.8),
  17. Row.of(2, 3, 0.6),
  18. Row.of(4, 1, 0.6),
  19. Row.of(4, 2, 0.3),
  20. Row.of(4, 3, 0.4)
  21. );
  22. BatchOperator <?> data = new MemSourceBatchOp(df_data, "user int, item int, rating double");
  23. StreamOperator <?> sdata = new MemSourceStreamOp(df_data, "user int, item int, rating double");
  24. BatchOperator <?> model = new ItemCfTrainBatchOp()
  25. .setUserCol("user")
  26. .setItemCol("item")
  27. .setRateCol("rating").linkFrom(data);
  28. StreamOperator <?> predictor = new ItemCfSimilarItemsRecommStreamOp(model)
  29. .setItemCol("item")
  30. .setReservedCols("item")
  31. .setK(1)
  32. .setRecommCol("prediction_result");
  33. predictor.linkFrom(sdata).print();
  34. StreamOperator.execute();
  35. }
  36. }

运行结果

item prediction_result
1 {“item”:”[3]”,”similarities”:”[0.3922322702763681]”}
3 {“item”:”[2]”,”similarities”:”[0.9738412097417931]”}
2 {“item”:”[3]”,”similarities”:”[0.9738412097417931]”}
1 {“item”:”[3]”,”similarities”:”[0.3922322702763681]”}
2 {“item”:”[3]”,”similarities”:”[0.9738412097417931]”}
3 {“item”:”[2]”,”similarities”:”[0.9738412097417931]”}