Java 类名:com.alibaba.alink.pipeline.classification.Cart
Python 类名:Cart

功能介绍

  • 支持带样本权重的训练

    参数说明

    | 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- | | featureCols | 特征列名 | 特征列名,必选 | String[] | ✓ | | | | labelCol | 标签列名 | 输入表中的标签列名 | String | ✓ | | | | predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | | | | categoricalCols | 离散特征列名 | 离散特征列名 | String[] | | | | | createTreeMode | 创建树的模式。 | series表示每个单机创建单颗树,parallel表示并行创建单颗树。 | String | | | “series” | | maxBins | 连续特征进行分箱的最大个数 | 连续特征进行分箱的最大个数。 | Integer | | | 128 | | maxDepth | 树的深度限制 | 树的深度限制 | Integer | | | 2147483647 | | maxLeaves | 叶节点的最多个数 | 叶节点的最多个数 | Integer | | | 2147483647 | | maxMemoryInMB | 树模型中用来加和统计量的最大内存使用数 | 树模型中用来加和统计量的最大内存使用数 | Integer | | | 64 | | minInfoGain | 分裂的最小增益 | 分裂的最小增益 | Double | | | 0.0 | | minSampleRatioPerChild | 子节点占父节点的最小样本比例 | 子节点占父节点的最小样本比例 | Double | | | 0.0 | | minSamplesPerLeaf | 叶节点的最小样本个数 | 叶节点的最小样本个数 | Integer | | | 2 | | modelFilePath | 模型的文件路径 | 模型的文件路径 | String | | | null | | overwriteSink | 是否覆写已有数据 | 是否覆写已有数据 | Boolean | | | false | | predictionDetailCol | 预测详细信息列名 | 预测详细信息列名 | String | | | | | reservedCols | 算法保留列名 | 算法保留列 | String[] | | | null | | weightCol | 权重列名 | 权重列对应的列名 | String | | 所选列类型为 [BIGDECIMAL, BIGINTEGER, BYTE, DOUBLE, FLOAT, INTEGER, LONG, SHORT] | null | | numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | | | 1 | | modelStreamFilePath | 模型流的文件路径 | 模型流的文件路径 | String | | | null | | modelStreamScanInterval | 扫描模型路径的时间间隔 | 描模型路径的时间间隔,单位秒 | Integer | | | 10 | | modelStreamStartTime | 模型流的起始时间 | 模型流的起始时间。默认从当前时刻开始读。使用yyyy-mm-dd hh:mm:ss.fffffffff格式,详见Timestamp.valueOf(String s) | String | | | null |

代码示例

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

运行结果

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

| 1.0000 | A | 0 | 0 | 0 | 0 | {“0”:1.0,”1”:0.0} |

| 2.0000 | B | 1 | 1 | 0 | 0 | {“0”:1.0,”1”:0.0} |

| 3.0000 | C | 2 | 2 | 1 | 1 | {“0”:0.0,”1”:1.0} |

| 4.0000 | D | 3 | 3 | 1 | 1 | {“0”:0.0,”1”:1.0} |