Java 类名:com.alibaba.alink.operator.batch.regression.FmRegressorPredictBatchOp
Python 类名:FmRegressorPredictBatchOp

功能介绍

FM即因子分解机(Factor Machine),它的特点是考虑了特征之间的相互作用,是一种非线性模型。该组件使用FM模型解决回归问题。

算法原理

FM模型是线性模型的升级,是在线性表达式后面加入了新的交叉项特征及对应的权值,FM模型的表达式如下所示:
FM回归预测 (FmRegressorPredictBatchOp) - 图1
这里我们使用 Adagrad 优化算法求解该模型。算法原理细节可以参考文献[1]。

算法使用

FM算法是推荐领域被验证的效果较好的推荐方案之一,在电商、广告、视频、信息流、游戏的推荐领域有广泛应用。

文献

[1] S. Rendle, “Factorization Machines,” 2010 IEEE International Conference on Data Mining, 2010, pp. 995-1000, doi: 10.1109/ICDM.2010.127.

参数说明

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

| predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | | |

| modelFilePath | 模型的文件路径 | 模型的文件路径 | String | | | null |

| predictionDetailCol | 预测详细信息列名 | 预测详细信息列名 | String | | | |

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

| vectorCol | 向量列名 | 向量列对应的列名,默认值是null | String | | 所选列类型为 [DENSE_VECTOR, SPARSE_VECTOR, STRING, VECTOR] | null |

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

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["1:1.1 3:2.0", 1.0],
  6. ["2:2.1 10:3.1", 1.0],
  7. ["1:1.2 5:3.2", 0.0],
  8. ["3:1.2 7:4.2", 0.0]
  9. ])
  10. input = BatchOperator.fromDataframe(df, schemaStr='kv string, label double')
  11. fm = FmRegressorTrainBatchOp()\
  12. .setVectorCol("kv")\
  13. .setLabelCol("label")
  14. model = input.link(fm)
  15. predictor = FmRegressorPredictBatchOp()\
  16. .setPredictionCol("pred")
  17. predictor.linkFrom(model, input).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.regression.FmRegressorPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.regression.FmRegressorTrainBatchOp;
  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 FmRegressorPredictBatchOpTest {
  10. @Test
  11. public void testFmRegressorPredictBatchOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of("1:1.1 3:2.0", 1.0),
  14. Row.of("2:2.1 10:3.1", 1.0),
  15. Row.of("1:1.2 5:3.2", 0.0),
  16. Row.of("3:1.2 7:4.2", 0.0)
  17. );
  18. BatchOperator <?> input = new MemSourceBatchOp(df, "kv string, label double");
  19. BatchOperator <?> fm = new FmRegressorTrainBatchOp()
  20. .setVectorCol("kv")
  21. .setLabelCol("label");
  22. BatchOperator model = input.link(fm);
  23. BatchOperator <?> predictor = new FmRegressorPredictBatchOp()
  24. .setPredictionCol("pred");
  25. predictor.linkFrom(model, input).print();
  26. }
  27. }

运行结果

| kv | label | pred | | —- | —- | —- |

| 1:1.1 3:2.0 | 1.0 | 0.473600 |

| 2:2.1 10:3.1 | 1.0 | 0.755115 |

| 1:1.2 5:3.2 | 0.0 | 0.005875 |

| 3:1.2 7:4.2 | 0.0 | 0.004641 |