Java 类名:com.alibaba.alink.operator.batch.dataproc.HugeStringIndexerPredictBatchOp
Python 类名:HugeStringIndexerPredictBatchOp

功能介绍

提供字符串ID化处理功能,与 StringIndexerPredictBatchOp 功能相同,是其升级版本,模型为分布式存储,提升了运行效率。支持多列同时转换。
由 StringIndexerTrainBatchOp 生成词典模型,将输入数据的字符串转化成词典模型中的ID
对于词典模型中不存在的字符串,提供了三种处理策略,”keep”表示用最大id加1代替, “skip”表示补null, “error”表示抛异常

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[]
handleInvalid 未知token处理策略 未知token处理策略。”keep”表示用最大id加1代替, “skip”表示补null, “error”表示抛异常 String “KEEP”, “ERROR”, “SKIP” “KEEP”
outputCols 输出结果列列名数组 输出结果列列名数组,可选,默认null String[] null
reservedCols 算法保留列名 算法保留列 String[] null

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["football", "apple"],
  6. ["football", "apple"],
  7. ["football", "apple"],
  8. ["basketball", "apple"],
  9. ["basketball", "apple"],
  10. ["tennis", "pair"],
  11. ["tennis", "pair"],
  12. ["pingpang", "banana"],
  13. ["pingpang", "banana"],
  14. ["baseball", "banana"]
  15. ])
  16. data = BatchOperator.fromDataframe(df, schemaStr='f0 string, f1 string')
  17. stringindexer = StringIndexerTrainBatchOp()\
  18. .setSelectedCol("f0")\
  19. .setSelectedCols(["f1"])\
  20. .setStringOrderType("alphabet_asc")
  21. model = stringindexer.linkFrom(data)
  22. predictor = HugeStringIndexerPredictBatchOp()\
  23. .setSelectedCols(["f0", "f1"])\
  24. .setOutputCols(["f0_indexed", "f1_indexed"])
  25. predictor.linkFrom(model, data).print()

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 org.junit.Test;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. public class HugeStringIndexerPredictBatchOpTest {
  8. @Test
  9. public void testStringIndexerPredictBatchOp() throws Exception {
  10. List <Row> df = Arrays.asList(
  11. Row.of("football", "apple"),
  12. Row.of("football", "apple"),
  13. Row.of("football", "apple"),
  14. Row.of("basketball", "apple"),
  15. Row.of("basketball", "apple"),
  16. Row.of("tennis", "pair"),
  17. Row.of("tennis", "pair"),
  18. Row.of("pingpang", "banana"),
  19. Row.of("pingpang", "banana"),
  20. Row.of("baseball", "banana")
  21. );
  22. BatchOperator <?> data = new MemSourceBatchOp(df, "f0 string,f1 string");
  23. BatchOperator <?> stringindexer = new StringIndexerTrainBatchOp()
  24. .setSelectedCol("f0")
  25. .setSelectedCols("f1")
  26. .setStringOrderType("frequency_asc");
  27. BatchOperator <?> predictor = new HugeStringIndexerPredictBatchOp().setSelectedCols("f0", "f1")
  28. .setOutputCols("f0_indexed", "f1_indexed");
  29. BatchOperator model = stringindexer.linkFrom(data);
  30. model.lazyPrint(10);
  31. BatchOperator result = predictor.linkFrom(model, data);
  32. result.print();
  33. }
  34. }

运行结果

| token | token_index | | —- | —- |

| banana | 5 |

| football | 6 |

| basketball | 1 |

| pingpang | 2 |

| tennis | 3 |

| pair | 4 |

| baseball | 0 |

| apple | 7 |

| f0 | f1 | f0_indexed | f1_indexed | | —- | —- | —- | —- |

| basketball | apple | 1 | 7 |

| pingpang | banana | 2 | 5 |

| football | apple | 6 | 7 |

| tennis | pair | 3 | 4 |

| tennis | pair | 3 | 4 |

| basketball | apple | 1 | 7 |

| football | apple | 6 | 7 |

| football | apple | 6 | 7 |

| pingpang | banana | 2 | 5 |

| baseball | banana | 0 | 5 |