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

功能介绍

提供字符串ID化处理功能
由StringIndexerTrainBatchOp生成词典模型,将输入数据的ID类型转化成词典模型中对应的字符串。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[] 所选列类型为 [LONG]
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. predictor = HugeStringIndexerPredictBatchOp().setSelectedCols(["f0", "f1"])\
  22. .setOutputCols(["f0_indexed", "f1_indexed"])
  23. model = stringindexer.linkFrom(data)
  24. result = predictor.linkFrom(model, data)
  25. indexerString = HugeIndexerStringPredictBatchOp().setSelectedCols(["f0_indexed", "f1_indexed"])\
  26. .setOutputCols(["f0_source", "f1_source"])
  27. indexerString.linkFrom(model, result).print()

Java 代码

  1. package com.alibaba.alink.operator.batch.dataproc;
  2. import org.apache.flink.types.Row;
  3. import com.alibaba.alink.operator.batch.BatchOperator;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class HugeIndexerStringPredictBatchOpTest {
  9. @Test
  10. public void testStringIndexerPredictBatchOp() throws Exception {
  11. List <Row> df = Arrays.asList(
  12. Row.of("football", "apple"),
  13. Row.of("football", "apple"),
  14. Row.of("football", "apple"),
  15. Row.of("basketball", "apple"),
  16. Row.of("basketball", "apple"),
  17. Row.of("tennis", "pair"),
  18. Row.of("tennis", "pair"),
  19. Row.of("pingpang", "banana"),
  20. Row.of("pingpang", "banana"),
  21. Row.of("baseball", "banana")
  22. );
  23. BatchOperator <?> data = new MemSourceBatchOp(df, "f0 string,f1 string");
  24. BatchOperator <?> stringindexer = new StringIndexerTrainBatchOp()
  25. .setSelectedCol("f0")
  26. .setSelectedCols("f1")
  27. .setStringOrderType("alphabet_asc");
  28. BatchOperator <?> predictor = new HugeStringIndexerPredictBatchOp().setSelectedCols("f0", "f1")
  29. .setOutputCols("f0_indexed", "f1_indexed");
  30. BatchOperator model = stringindexer.linkFrom(data);
  31. model.lazyPrint(10);
  32. BatchOperator result = predictor.linkFrom(model, data);
  33. result.lazyPrint(10);
  34. BatchOperator <?> indexerString = new HugeIndexerStringPredictBatchOp().setSelectedCols("f0_indexed", "f1_indexed")
  35. .setOutputCols("f0_source", "f1_source");
  36. indexerString.linkFrom(model, result).print();
  37. }
  38. }

运行结果

| f0 | f1 | f0_indexed | f1_indexed | f0_source | f1_source | | —- | —- | —- | —- | —- | —- |

| basketball | apple | 3 | 0 | basketball | apple |

| football | apple | 4 | 0 | football | apple |

| basketball | apple | 3 | 0 | basketball | apple |

| pingpang | banana | 6 | 1 | pingpang | banana |

| football | apple | 4 | 0 | football | apple |

| tennis | pair | 7 | 5 | tennis | pair |

| tennis | pair | 7 | 5 | tennis | pair |

| pingpang | banana | 6 | 1 | pingpang | banana |

| baseball | banana | 2 | 1 | baseball | banana |

| football | apple | 4 | 0 | football | apple |