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

功能介绍

  • 决策树回归组件支持稠密数据格式
  • 支持带样本权重的训练

参数说明

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

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

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

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | 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 = DecisionTreeRegTrainBatchOp()\
  15. .setLabelCol('label')\
  16. .setFeatureCols(['f0', 'f1', 'f2', 'f3'])\
  17. .linkFrom(batchSource)
  18. predictBatchOp = DecisionTreeRegPredictBatchOp()\
  19. .setPredictionCol('pred')
  20. predictStreamOp = DecisionTreeRegPredictStreamOp(trainOp)\
  21. .setPredictionCol('pred')
  22. predictBatchOp.linkFrom(trainOp, batchSource).print()
  23. predictStreamOp.linkFrom(streamSource).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.regression.DecisionTreeRegPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.regression.DecisionTreeRegTrainBatchOp;
  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.DecisionTreeRegPredictStreamOp;
  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 DecisionTreeRegPredictBatchOpTest {
  13. @Test
  14. public void testDecisionTreeRegPredictBatchOp() 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 DecisionTreeRegTrainBatchOp()
  26. .setLabelCol("label")
  27. .setFeatureCols("f0", "f1", "f2", "f3")
  28. .linkFrom(batchSource);
  29. BatchOperator <?> predictBatchOp = new DecisionTreeRegPredictBatchOp()
  30. .setPredictionCol("pred");
  31. StreamOperator <?> predictStreamOp = new DecisionTreeRegPredictStreamOp(trainOp)
  32. .setPredictionCol("pred");
  33. predictBatchOp.linkFrom(trainOp, batchSource).print();
  34. predictStreamOp.linkFrom(streamSource).print();
  35. StreamOperator.execute();
  36. }
  37. }

运行结果

批预测结果

| 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 | | —- | —- | —- | —- | —- | —- |

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

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

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

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