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

功能介绍

基于StringIndexer模型,将一列字符串映射为整数。该组件为批式组件。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCol 选中的列名 计算列对应的列名 String 所选列类型为 [INTEGER, LONG, STRING]
handleInvalid 未知token处理策略 未知token处理策略。”keep”表示用最大id加1代替, “skip”表示补null, “error”表示抛异常 String “KEEP”, “ERROR”, “SKIP” “KEEP”
modelFilePath 模型的文件路径 模型的文件路径 String null
outputCol 输出结果列 输出结果列列名,可选,默认null String null
reservedCols 算法保留列名 算法保留列 String[] null
numThreads 组件多线程线程个数 组件多线程线程个数 Integer 1

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["football"],
  6. ["football"],
  7. ["football"],
  8. ["basketball"],
  9. ["basketball"],
  10. ["tennis"],
  11. ])
  12. data = BatchOperator.fromDataframe(df, schemaStr='f0 string')
  13. stringindexer = StringIndexerTrainBatchOp() \
  14. .setSelectedCol("f0") \
  15. .setStringOrderType("frequency_asc")
  16. predictor = StringIndexerPredictBatchOp().setSelectedCol("f0").setOutputCol("f0_indexed")
  17. model = stringindexer.linkFrom(data)
  18. 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.dataproc.StringIndexerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.dataproc.StringIndexerTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class StringIndexerPredictBatchOpTest {
  10. @Test
  11. public void testStringIndexerPredictBatchOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of("football"),
  14. Row.of("football"),
  15. Row.of("football"),
  16. Row.of("basketball"),
  17. Row.of("basketball"),
  18. Row.of("tennis")
  19. );
  20. BatchOperator <?> data = new MemSourceBatchOp(df, "f0 string");
  21. BatchOperator <?> stringindexer = new StringIndexerTrainBatchOp()
  22. .setSelectedCol("f0")
  23. .setStringOrderType("frequency_asc");
  24. BatchOperator <?> predictor = new StringIndexerPredictBatchOp().setSelectedCol("f0").setOutputCol(
  25. "f0_indexed");
  26. BatchOperator model = stringindexer.linkFrom(data);
  27. predictor.linkFrom(model, data).print();
  28. }
  29. }

运行结果

| f0 | f0_indexed | | —- | —- |

| football | 2 |

| football | 2 |

| football | 2 |

| basketball | 1 |

| basketball | 1 |

| tennis | 0 |