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

功能介绍

多列字符串转换组件,输入的模型数据来自 MultiStringIndexerTrainBatchOp 组件的输出,训练的时候指定多个列,每个列单独编码。
这个组件为批式预测组件,预测时需要指定列名,列名必须与训练时列名相同。如果转换时指定了训练时不存在的列名,会报异常。
支持按照一定的次序编码。如随机、出现频次生序,出现频次降序、字符串生序、字符串降序5种方式。
设置 setStringOrderType 参数时分别对应 random frequency_asc frequency_desc alphabet_asc alphabet_desc。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[]
handleInvalid 未知token处理策略 未知token处理策略。”keep”表示用最大id加1代替, “skip”表示补null, “error”表示抛异常 String “KEEP”, “ERROR”, “SKIP” “KEEP”
modelFilePath 模型的文件路径 模型的文件路径 String null
outputCols 输出结果列列名数组 输出结果列列名数组,可选,默认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", "apple"],
  6. ["football", "banana"],
  7. ["football", "banana"],
  8. ["basketball", "orange"],
  9. ["basketball", "grape"],
  10. ["tennis", "grape"],
  11. ])
  12. data = BatchOperator.fromDataframe(df, schemaStr='f0 string,f1 string')
  13. stringindexer = MultiStringIndexerTrainBatchOp() \
  14. .setSelectedCols(["f0", "f1"]) \
  15. .setStringOrderType("frequency_asc")
  16. predictor = MultiStringIndexerPredictBatchOp().setSelectedCols(["f0", "f1"]).setOutputCols(["f0_indexed", "f1_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.MultiStringIndexerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.dataproc.MultiStringIndexerTrainBatchOp;
  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 MultiStringIndexerPredictBatchOpTest {
  10. @Test
  11. public void testMultiStringIndexerPredictBatchOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of("football", "apple"),
  14. Row.of("football", "banana"),
  15. Row.of("football", "banana"),
  16. Row.of("basketball", "orange"),
  17. Row.of("basketball", "grape"),
  18. Row.of("tennis", "grape")
  19. );
  20. BatchOperator <?> data = new MemSourceBatchOp(df, "f0 string,f1 string");
  21. BatchOperator <?> stringindexer = new MultiStringIndexerTrainBatchOp()
  22. .setSelectedCols("f0", "f1")
  23. .setStringOrderType("frequency_asc");
  24. BatchOperator <?> predictor = new MultiStringIndexerPredictBatchOp().setSelectedCols("f0", "f1").setOutputCols(
  25. "f0_indexed", "f1_indexed");
  26. BatchOperator model = stringindexer.linkFrom(data);
  27. predictor.linkFrom(model, data).print();
  28. }
  29. }

运行结果

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

| basketball | orange | 1 | 0 |

| football | apple | 2 | 1 |

| tennis | grape | 0 | 3 |

| football | banana | 2 | 2 |

| basketball | grape | 1 | 3 |

| football | banana | 2 | 2 |