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

功能介绍

  • gbdt(Gradient Boosting Decision Trees)回归,是经典的基于boosting的有监督学习模型,可以用来解决回归问题
  • 支持连续特征和离散特征
  • 支持数据采样和特征采样

参数说明

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

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

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

| 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.0, "A", 0, 0, 0],
  6. [2.0, "B", 1, 1, 0],
  7. [3.0, "C", 2, 2, 1],
  8. [4.0, "D", 3, 3, 1]
  9. ])
  10. batchSource = BatchOperator.fromDataframe(
  11. df, schemaStr='f0 double, f1 string, f2 int, f3 int, label int')
  12. streamSource = StreamOperator.fromDataframe(
  13. df, schemaStr='f0 double, f1 string, f2 int, f3 int, label int')
  14. trainOp = GbdtRegTrainBatchOp()\
  15. .setLearningRate(1.0)\
  16. .setNumTrees(3)\
  17. .setMinSamplesPerLeaf(1)\
  18. .setLabelCol('label')\
  19. .setFeatureCols(['f0', 'f1', 'f2', 'f3'])\
  20. .linkFrom(batchSource)
  21. predictBatchOp = GbdtRegPredictBatchOp()\
  22. .setPredictionCol('pred')
  23. predictStreamOp = GbdtRegPredictStreamOp(trainOp)\
  24. .setPredictionCol('pred')
  25. predictBatchOp.linkFrom(trainOp, batchSource).print()
  26. predictStreamOp.linkFrom(streamSource).print()
  27. 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.regression.GbdtRegPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.regression.GbdtRegTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import com.alibaba.alink.operator.stream.StreamOperator;
  7. import com.alibaba.alink.operator.stream.regression.GbdtRegPredictStreamOp;
  8. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  9. import org.junit.Test;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class GbdtRegPredictBatchOpTest {
  13. @Test
  14. public void testGbdtRegPredictBatchOp() throws Exception {
  15. List <Row> df = Arrays.asList(
  16. Row.of(1.0, "A", 0, 0, 0),
  17. Row.of(2.0, "B", 1, 1, 0),
  18. Row.of(3.0, "C", 2, 2, 1),
  19. Row.of(4.0, "D", 3, 3, 1)
  20. );
  21. BatchOperator <?> batchSource = new MemSourceBatchOp(
  22. df, "f0 double, f1 string, f2 int, f3 int, label int");
  23. StreamOperator <?> streamSource = new MemSourceStreamOp(
  24. df, "f0 double, f1 string, f2 int, f3 int, label int");
  25. BatchOperator <?> trainOp = new GbdtRegTrainBatchOp()
  26. .setLearningRate(1.0)
  27. .setNumTrees(3)
  28. .setMinSamplesPerLeaf(1)
  29. .setLabelCol("label")
  30. .setFeatureCols("f0", "f1", "f2", "f3")
  31. .linkFrom(batchSource);
  32. BatchOperator <?> predictBatchOp = new GbdtRegPredictBatchOp()
  33. .setPredictionCol("pred");
  34. StreamOperator <?> predictStreamOp = new GbdtRegPredictStreamOp(trainOp)
  35. .setPredictionCol("pred");
  36. predictBatchOp.linkFrom(trainOp, batchSource).print();
  37. predictStreamOp.linkFrom(streamSource).print();
  38. StreamOperator.execute();
  39. }
  40. }

运行结果

批预测结果

| f0 | f1 | f2 | f3 | label | pred | | —- | —- | —- | —- | —- | —- |

| 1.0000 | A | 0 | 0 | 0 | 0.0000 |

| 2.0000 | B | 1 | 1 | 0 | 0.0000 |

| 3.0000 | C | 2 | 2 | 1 | 1.0000 |

| 4.0000 | D | 3 | 3 | 1 | 1.0000 |

流预测结果

| f0 | f1 | f2 | f3 | label | pred | | —- | —- | —- | —- | —- | —- |

| 1.0000 | A | 0 | 0 | 0 | 0.0000 |

| 4.0000 | D | 3 | 3 | 1 | 1.0000 |

| 3.0000 | C | 2 | 2 | 1 | 1.0000 |

| 2.0000 | B | 1 | 1 | 0 | 0.0000 |