Java 类名:com.alibaba.alink.operator.batch.feature.QuantileDiscretizerTrainBatchOp
Python 类名:QuantileDiscretizerTrainBatchOp

功能介绍

分位点离散可以计算选定列的分位点,然后使用这些分位点进行离散化。
生成选中列对应的q-quantile,其中可以所有列指定一个,也可以每一列对应一个

编码结果

Encode ——> INDEX

预测结果为单个token的index

Encode ——> VECTOR

预测结果为稀疏向量:

  1. 1. dropLasttrue,向量中非零元个数为0或者1
  2. 2. dropLastfalse,向量中非零元个数必定为1
Encode ——> ASSEMBLED_VECTOR

预测结果为稀疏向量,是预测选择列中,各列预测为VECTOR时,按照选择顺序ASSEMBLE的结果。

向量维度

Encode ——> Vector
  1. numBuckets: 训练参数
  2. dropLast: 预测参数
  3. handleInvalid: 预测参数

Token index

Encode ——> Vector
  1. 1. 正常数据: 唯一的非零元为数据所在的bucket,若 dropLasttrue, 最大的bucket的值会被丢掉,预测结果为全零元
  2. 2. null:
  3. 2.1 handleInvalidkeep: 唯一的非零元为:numBuckets - dropLast(true: 1, false: 0)
  4. 2.2 handleInvalidskip: null
  5. 2.3 handleInvaliderror: 报错

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[] 所选列类型为 [BIGDECIMAL, BIGINTEGER, BYTE, DOUBLE, FLOAT, INTEGER, LONG, SHORT]
leftOpen 是否左开右闭 左开右闭为true,左闭右开为false Boolean true
numBuckets quantile个数 quantile个数,对所有列有效。 Integer 2
numBucketsArray quantile个数 quantile个数,每一列对应数组中一个元素。 Integer[] null

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["a", 1, 1, 2.0, True],
  6. ["c", 1, 2, -3.0, True],
  7. ["a", 2, 2, 2.0, False],
  8. ["c", 0, 0, 0.0, False]
  9. ])
  10. batchSource = BatchOperator.fromDataframe(
  11. df, schemaStr='f_string string, f_long long, f_int int, f_double double, f_boolean boolean')
  12. streamSource = StreamOperator.fromDataframe(
  13. df, schemaStr='f_string string, f_long long, f_int int, f_double double, f_boolean boolean')
  14. trainOp = QuantileDiscretizerTrainBatchOp()\
  15. .setSelectedCols(['f_double'])\
  16. .setNumBuckets(8)\
  17. .linkFrom(batchSource)
  18. predictBatchOp = QuantileDiscretizerPredictBatchOp()\
  19. .setSelectedCols(['f_double'])
  20. predictStreamOp = QuantileDiscretizerPredictStreamOp(trainOp)\
  21. .setSelectedCols(['f_double'])
  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.feature.QuantileDiscretizerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.feature.QuantileDiscretizerTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import com.alibaba.alink.operator.stream.StreamOperator;
  7. import com.alibaba.alink.operator.stream.feature.QuantileDiscretizerPredictStreamOp;
  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 QuantileDiscretizerTrainBatchOpTest {
  13. @Test
  14. public void testQuantileDiscretizerTrainBatchOp() throws Exception {
  15. List <Row> df = Arrays.asList(
  16. Row.of("a", 1, 1, 2.0, true),
  17. Row.of("c", 1, 2, -3.0, true),
  18. Row.of("a", 2, 2, 2.0, false),
  19. Row.of("c", 0, 0, 0.0, false)
  20. );
  21. BatchOperator <?> batchSource = new MemSourceBatchOp(df,
  22. "f_string string, f_long int, f_int int, f_double double, f_boolean boolean");
  23. StreamOperator <?> streamSource = new MemSourceStreamOp(df,
  24. "f_string string, f_long int, f_int int, f_double double, f_boolean boolean");
  25. BatchOperator <?> trainOp = new QuantileDiscretizerTrainBatchOp()
  26. .setSelectedCols("f_double")
  27. .setNumBuckets(8)
  28. .linkFrom(batchSource);
  29. BatchOperator <?> predictBatchOp = new QuantileDiscretizerPredictBatchOp()
  30. .setSelectedCols("f_double");
  31. predictBatchOp.linkFrom(trainOp, batchSource).print();
  32. StreamOperator <?> predictStreamOp = new QuantileDiscretizerPredictStreamOp(trainOp)
  33. .setSelectedCols("f_double");
  34. predictStreamOp.linkFrom(streamSource).print();
  35. StreamOperator.execute();
  36. }
  37. }

运行结果

| f_string | f_long | f_int | f_double | f_boolean | | —- | —- | —- | —- | —- |

| a | 1 | 1 | 2 | true |

| c | 1 | 2 | 0 | true |

| a | 2 | 2 | 2 | false |

| c | 0 | 0 | 1 | false |