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

功能介绍

提供ID转换为字符串的功能,与 HugeMultiStringIndexerPredictBatchOp 功能相反。
由 MultiStringIndexerTrainBatchOp 生成词典模型,将输入数据的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. [1, "football", "apple"],
  6. [2, "football", "apple"],
  7. [3, "football", "apple"],
  8. [4, "basketball", "apple"],
  9. [5, "basketball", "apple"],
  10. [6, "tennis", "pair"],
  11. [7, "tennis", "pair"],
  12. [8, "pingpang", "banana"],
  13. [9, "pingpang", "banana"],
  14. [0, "baseball", "banana"]
  15. ])
  16. data = BatchOperator.fromDataframe(df, schemaStr='id long, f0 string, f1 string')
  17. stringindexer = MultiStringIndexerTrainBatchOp()\
  18. .setSelectedCols(["f0", "f1"])\
  19. .setStringOrderType("frequency_asc")
  20. model = stringindexer.linkFrom(data)
  21. predictor = HugeMultiStringIndexerPredictBatchOp()\
  22. .setSelectedCols(["f0", "f1"])
  23. result = predictor.linkFrom(model, data)
  24. stringPredictor = HugeMultiIndexerStringPredictBatchOp()\
  25. .setSelectedCols(["f0", "f1"])\
  26. .setOutputCols(["f0_source", "f1_source"])
  27. stringPredictor.linkFrom(model, result).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 HugeMultiIndexerStringPredictBatchOpTest {
  8. @Test
  9. public void testHugeMultiStringIndexerPredict() throws Exception {
  10. List <Row> df = Arrays.asList(
  11. Row.of(1L, "football", "apple"),
  12. Row.of(2L, "football", "apple"),
  13. Row.of(3L, "football", "apple"),
  14. Row.of(4L, "basketball", "apple"),
  15. Row.of(5L, "basketball", "apple"),
  16. Row.of(6L, "tennis", "pair"),
  17. Row.of(7L, "tennis", "pair"),
  18. Row.of(8L, "pingpang", "banana"),
  19. Row.of(9L, "pingpang", "banana"),
  20. Row.of(0L, "baseball", "banana")
  21. );
  22. // baseball 1
  23. // basketball,pair,tennis,pingpang 2
  24. // footbal,banana 3
  25. // apple 5
  26. BatchOperator <?> data = new MemSourceBatchOp(df, "id long,f0 string,f1 string");
  27. BatchOperator <?> stringindexer = new MultiStringIndexerTrainBatchOp()
  28. .setSelectedCols("f0", "f1")
  29. .setStringOrderType("frequency_asc");
  30. BatchOperator model = stringindexer.linkFrom(data);
  31. model.lazyPrint(10);
  32. BatchOperator <?> predictor = new HugeMultiStringIndexerPredictBatchOp().setSelectedCols("f0", "f1");
  33. BatchOperator result = predictor.linkFrom(model, data);
  34. result.lazyPrint(10);
  35. BatchOperator <?> stringPredictor = new HugeMultiIndexerStringPredictBatchOp().setSelectedCols("f0", "f1")
  36. .setOutputCols("f0_source", "f1_source");
  37. stringPredictor.linkFrom(model, result).print();
  38. }
  39. }

运行结果

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

| 1 | apple | 2 |

| 1 | pair | 0 |

| 1 | banana | 1 |

| -1 | {“selectedCols”:”[“f0”,”f1”]”,”selectedColTypes”:”[“VARCHAR”,”VARCHAR”]”} | null |

| 0 | football | 4 |

| 0 | baseball | 0 |

| 0 | basketball | 1 |

| 0 | tennis | 2 |

| 0 | pingpang | 3 |

| id | f0 | f1 | | —- | —- | —- |

| 1 | 4 | 2 |

| 2 | 4 | 2 |

| 6 | 2 | 0 |

| 7 | 2 | 0 |

| 5 | 1 | 2 |

| 3 | 4 | 2 |

| 9 | 3 | 1 |

| 0 | 0 | 1 |

| 4 | 1 | 2 |

| 8 | 3 | 1 |

| id | f0 | f1 | f0_source | f1_source | | —- | —- | —- | —- | —- |

| 5 | 1 | 2 | basketball | apple |

| 2 | 4 | 2 | football | apple |

| 6 | 2 | 0 | tennis | pair |

| 4 | 1 | 2 | basketball | apple |

| 8 | 3 | 1 | pingpang | banana |

| 3 | 4 | 2 | football | apple |

| 7 | 2 | 0 | tennis | pair |

| 9 | 3 | 1 | pingpang | banana |

| 0 | 0 | 1 | baseball | banana |

| 1 | 4 | 2 | football | apple |