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

功能介绍

等宽离散可以计算选定数值列的分位点,每个区间都有相同的组距,也就是数据范围/组数,通过训练可以得到一系列分为点,
然后使用这些分位点进行预测。
其中可以所有列使用同一个分组数量,也可以每一列对应一个分组数量。预测结果可以是特征值或一系列0/1离散特征。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
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.1],
  6. ["b", -2, 0.9],
  7. ["c", 100, -0.01],
  8. ["d", -99, 100.9],
  9. ["a", 1, 1.1],
  10. ["b", -2, 0.9],
  11. ["c", 100, -0.01],
  12. ["d", -99, 100.9]
  13. ])
  14. batchSource = BatchOperator.fromDataframe(df,schemaStr="f_string string, f_long long, f_double double")
  15. trainOp = EqualWidthDiscretizerTrainBatchOp(). \
  16. setSelectedCols(['f_long', 'f_double']). \
  17. setNumBuckets(5). \
  18. linkFrom(batchSource)
  19. EqualWidthDiscretizerPredictBatchOp(). \
  20. setSelectedCols(['f_long', 'f_double']). \
  21. linkFrom(trainOp,batchSource). \
  22. print()
  23. trainOp = EqualWidthDiscretizerTrainBatchOp().setSelectedCols(['f_long', 'f_double']). \
  24. setNumBucketsArray([5,3]). \
  25. linkFrom(batchSource)
  26. EqualWidthDiscretizerPredictBatchOp(). \
  27. setSelectedCols(['f_long', 'f_double']). \
  28. linkFrom(trainOp,batchSource). \
  29. print()
  30. EqualWidthDiscretizerPredictBatchOp(). \
  31. setEncode("ASSEMBLED_VECTOR"). \
  32. setSelectedCols(['f_long', 'f_double']). \
  33. setOutputCols(["assVec"]). \
  34. linkFrom(trainOp,batchSource).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.feature.EqualWidthDiscretizerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.feature.EqualWidthDiscretizerTrainBatchOp;
  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.EqualWidthDiscretizerPredictStreamOp;
  8. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  9. import com.alibaba.alink.params.feature.HasEncodeWithoutWoe.Encode;
  10. import org.junit.Test;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. public class EqualWidthDiscretizerTrainBatchOpTest {
  14. @Test
  15. public void testEqualWidthDiscretizerTrainBatchOp2() throws Exception {
  16. List <Row> df = Arrays.asList(
  17. Row.of("a", 1, 1.1),
  18. Row.of("b", -2, 0.9),
  19. Row.of("c", 100, -0.01),
  20. Row.of("d", -99, 100.9),
  21. Row.of("a", 1, 1.1),
  22. Row.of("b", -2, 0.9),
  23. Row.of("c", 100, -0.01),
  24. Row.of("d", -99, 100.9)
  25. );
  26. BatchOperator <?> batchSource = new MemSourceBatchOp(df, "f_string string, f_long int, f_double double");
  27. BatchOperator <?> trainOp = new EqualWidthDiscretizerTrainBatchOp().setSelectedCols("f_long", "f_double")
  28. .setNumBuckets(5).linkFrom(batchSource);
  29. new EqualWidthDiscretizerPredictBatchOp().setSelectedCols("f_long","f_double")
  30. .linkFrom(trainOp, batchSource).print();
  31. BatchOperator trainOp2 = new EqualWidthDiscretizerTrainBatchOp().setSelectedCols("f_long", "f_double")
  32. .setNumBucketsArray(5,3).linkFrom(batchSource);
  33. new EqualWidthDiscretizerPredictBatchOp().setSelectedCols("f_long","f_double")
  34. .linkFrom(trainOp2,batchSource).print();
  35. new EqualWidthDiscretizerPredictBatchOp().setSelectedCols("f_long","f_double")
  36. .setEncode(Encode.ASSEMBLED_VECTOR)
  37. .setOutputCols("assVec")
  38. .linkFrom(trainOp2,batchSource).print();
  39. }
  40. }

运行结果

| f_string | f_long | f_double | | —- | —- | —- |

| a | 2 | 0 |

| b | 2 | 0 |

| c | 4 | 0 |

| d | 0 | 4 |

| a | 2 | 0 |

| b | 2 | 0 |

| c | 4 | 0 |

| d | 0 | 4 |

| f_string | f_long | f_double | | —- | —- | —- |

| a | 2 | 0 |

| b | 2 | 0 |

| c | 4 | 0 |

| d | 0 | 2 |

| a | 2 | 0 |

| b | 2 | 0 |

| c | 4 | 0 |

| d | 0 | 2 |

| f_string | f_long | f_double | assVec | | —- | —- | —- | —- |

| a | 1 | 1.1000 | $8$2:1.0 5:1.0 |

| b | -2 | 0.9000 | $8$2:1.0 5:1.0 |

| c | 100 | -0.0100 | $8$5:1.0 |

| d | -99 | 100.9000 | $8$0:1.0 |

| a | 1 | 1.1000 | $8$2:1.0 5:1.0 |

| b | -2 | 0.9000 | $8$2:1.0 5:1.0 |

| c | 100 | -0.0100 | $8$5:1.0 |

| d | -99 | 100.9000 | $8$0:1.0 |