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

功能介绍

使用Fm推荐模型,为item推荐user list。

参数说明

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

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

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

| excludeKnown | 排除已知的关联 | 推荐结果中是否排除训练数据中已知的关联 | Boolean | | | false |

| 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 = FmRecommTrainBatchOp()\
  14. .setUserCol("user")\
  15. .setItemCol("item")\
  16. .setNumFactor(20)\
  17. .setRateCol("rating").linkFrom(data);
  18. predictor = FmRateRecommBatchOp()\
  19. .setUserCol("user")\
  20. .setItemCol("item")\
  21. .setRecommCol("prediction_result");
  22. predictor.linkFrom(model, data).print()
  23. model = FmRecommTrainBatchOp()\
  24. .setUserCol("user")\
  25. .setItemCol("item")\
  26. .setNumFactor(20)\
  27. .setRateCol("rating").linkFrom(data);
  28. predictor = FmUsersPerItemRecommBatchOp()\
  29. .setItemCol("user")\
  30. .setK(1).setReservedCols(["item"])\
  31. .setRecommCol("prediction_result");
  32. 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.FmRateRecommBatchOp;
  4. import com.alibaba.alink.operator.batch.recommendation.FmRecommTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.recommendation.FmUsersPerItemRecommBatchOp;
  6. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  7. import org.junit.Test;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. public class FmUsersPerItemRecommBatchOpTest {
  11. @Test
  12. public void testFmUsersPerItemRecommBatchOp() throws Exception {
  13. List <Row> df_data = Arrays.asList(
  14. Row.of(1, 1, 0.6),
  15. Row.of(2, 2, 0.8),
  16. Row.of(2, 3, 0.6),
  17. Row.of(4, 1, 0.6),
  18. Row.of(4, 2, 0.3),
  19. Row.of(4, 3, 0.4)
  20. );
  21. BatchOperator <?> data = new MemSourceBatchOp(df_data, "user int, item int, rating double");
  22. BatchOperator <?> model = new FmRecommTrainBatchOp()
  23. .setUserCol("user")
  24. .setItemCol("item")
  25. .setNumFactor(20)
  26. .setRateCol("rating").linkFrom(data);
  27. BatchOperator <?> predictor = new FmRateRecommBatchOp()
  28. .setUserCol("user")
  29. .setItemCol("item")
  30. .setRecommCol("prediction_result");
  31. predictor.linkFrom(model, data).print();
  32. model = new FmRecommTrainBatchOp()
  33. .setUserCol("user")
  34. .setItemCol("item")
  35. .setNumFactor(20)
  36. .setRateCol("rating").linkFrom(data);
  37. predictor = new FmUsersPerItemRecommBatchOp()
  38. .setItemCol("user")
  39. .setK(1).setReservedCols("item")
  40. .setRecommCol("prediction_result");
  41. predictor.linkFrom(model, data).print();
  42. }
  43. }

运行结果

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

| 1 | {“object”:”[1]”,”rate”:”[0.5829579830169678]”} |

| 2 | {“object”:”[2]”,”rate”:”[0.576914370059967]”} |

| 3 | {“object”:”[1]”,”rate”:”[0.5055253505706787]”} |

| 1 | {“object”:”[1]”,”rate”:”[0.5829579830169678]”} |

| 2 | {“object”:”[2]”,”rate”:”[0.576914370059967]”} |

| 3 | {“object”:”[1]”,”rate”:”[0.5055253505706787]”} |