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

功能介绍

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

参数说明

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

| itemCol | Item列列名 | Item列列名 | String | ✓ | | |

| recommCol | 推荐结果列名 | 推荐结果列名 | String | ✓ | | |

| initRecommCol | 初始推荐列列名 | 初始推荐列列名 | String | | 所选列类型为 [M_TABLE] | null |

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

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | null |

| numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | | | 1 |

代码示例

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. model = ItemCfTrainBatchOp()\
  14. .setUserCol("user")\
  15. .setItemCol("item")\
  16. .setRateCol("rating").linkFrom(data);
  17. predictor = ItemCfSimilarItemsRecommBatchOp()\
  18. .setItemCol("item")\
  19. .setReservedCols(["item"])\
  20. .setK(1)\
  21. .setRecommCol("prediction_result");
  22. 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.ItemCfSimilarItemsRecommBatchOp;
  4. import com.alibaba.alink.operator.batch.recommendation.ItemCfTrainBatchOp;
  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 ItemCfSimilarItemsRecommBatchOpTest {
  10. @Test
  11. public void testItemCfSimilarItemsRecommBatchOp() throws Exception {
  12. List <Row> df_data = Arrays.asList(
  13. Row.of(1, 1, 0.6),
  14. Row.of(2, 2, 0.8),
  15. Row.of(2, 3, 0.6),
  16. Row.of(4, 1, 0.6),
  17. Row.of(4, 2, 0.3),
  18. Row.of(4, 3, 0.4)
  19. );
  20. BatchOperator <?> data = new MemSourceBatchOp(df_data, "user int, item int, rating double");
  21. BatchOperator <?> model = new ItemCfTrainBatchOp()
  22. .setUserCol("user")
  23. .setItemCol("item")
  24. .setRateCol("rating").linkFrom(data);
  25. BatchOperator <?> predictor = new ItemCfSimilarItemsRecommBatchOp()
  26. .setItemCol("item")
  27. .setReservedCols("item")
  28. .setK(1)
  29. .setRecommCol("prediction_result");
  30. predictor.linkFrom(model, data).print();
  31. }
  32. }

运行结果

| item | prediction_result | | —- | —- |

| 1 | {“item”:”[3]”,”similarities”:”[0.3922322702763681]”} |

| 2 | {“item”:”[3]”,”similarities”:”[0.9738412097417931]”} |

| 3 | {“item”:”[2]”,”similarities”:”[0.9738412097417931]”} |

| 1 | {“item”:”[3]”,”similarities”:”[0.3922322702763681]”} |

| 2 | {“item”:”[3]”,”similarities”:”[0.9738412097417931]”} |

| 3 | {“item”:”[2]”,”similarities”:”[0.9738412097417931]”} |