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

功能介绍

MultiStringIndexer 训练组件的作用是训练一个模型用于将多列字符串映射为整数,训练的时候指定多个列,每个列单独编码。
支持按照一定的次序编码。如随机、出现频次生序,出现频次降序、字符串生序、字符串降序5种方式。
设置 setStringOrderType 参数时分别对应 random frequency_asc frequency_desc alphabet_asc alphabet_desc。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[]
stringOrderType Token排序方法 Token排序方法 String “RANDOM”, “FREQUENCY_ASC”, “FREQUENCY_DESC”, “ALPHABET_ASC”, “ALPHABET_DESC” “RANDOM”

代码示例

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 = MultiStringIndexerTrainBatchOp() \
  14. .setSelectedCols(["f0"]) \
  15. .setStringOrderType("frequency_asc")
  16. model = stringindexer.linkFrom(data)
  17. model.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.MultiStringIndexerTrainBatchOp;
  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 MultiStringIndexerTrainBatchOpTest {
  9. @Test
  10. public void testMultiStringIndexerTrainBatchOp() throws Exception {
  11. List <Row> df = Arrays.asList(
  12. Row.of("football"),
  13. Row.of("football"),
  14. Row.of("football"),
  15. Row.of("basketball"),
  16. Row.of("basketball"),
  17. Row.of("tennis")
  18. );
  19. BatchOperator <?> data = new MemSourceBatchOp(df, "f0 string");
  20. BatchOperator <?> stringindexer = new MultiStringIndexerTrainBatchOp()
  21. .setSelectedCols("f0")
  22. .setStringOrderType("frequency_asc");
  23. BatchOperator model = stringindexer.linkFrom(data);
  24. model.print();
  25. }
  26. }

运行结果

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

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

| 0 | tennis | 0 |

| 0 | basketball | 1 |

| 0 | football | 2 |