Java 类名:com.alibaba.alink.pipeline.dataproc.IndexToString
Python 类名:IndexToString

功能介绍

基于StringIndexer模型,将一列整数映射为字符串。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
modelName 模型名字 模型名字 String
selectedCol 选中的列名 计算列对应的列名 String
modelFilePath 模型的文件路径 模型的文件路径 String null
outputCol 输出结果列 输出结果列列名,可选,默认null String null
overwriteSink 是否覆写已有数据 是否覆写已有数据 Boolean false
reservedCols 算法保留列名 算法保留列 String[] null
numThreads 组件多线程线程个数 组件多线程线程个数 Integer 1
modelStreamFilePath 模型流的文件路径 模型流的文件路径 String null
modelStreamScanInterval 扫描模型路径的时间间隔 描模型路径的时间间隔,单位秒 Integer 10
modelStreamStartTime 模型流的起始时间 模型流的起始时间。默认从当前时刻开始读。使用yyyy-mm-dd hh:mm:ss.fffffffff格式,详见Timestamp.valueOf(String s) String null

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. ["football"],
  6. ["football"],
  7. ["football"],
  8. ["basketball"],
  9. ["basketball"],
  10. ["tennis"],
  11. ])
  12. train_data = BatchOperator.fromDataframe(df_data, schemaStr='f0 string')
  13. data = StreamOperator.fromDataframe(df_data, schemaStr='f0 string')
  14. stringIndexer = StringIndexer() \
  15. .setModelName("string_indexer_model") \
  16. .setSelectedCol("f0") \
  17. .setOutputCol("f0_indexed") \
  18. .setStringOrderType("frequency_asc").fit(train_data)
  19. batch_model = stringIndexer.transform(train_data)
  20. indexed = stringIndexer.transform(data)
  21. indexToStrings = IndexToStringPredictStreamOp(batch_model) \
  22. .setSelectedCol("f0_indexed") \
  23. .setOutputCol("f0_indxed_unindexed")
  24. indexToStrings.linkFrom(indexed).print()
  25. StreamOperator.execute()

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 com.alibaba.alink.operator.stream.StreamOperator;
  5. import com.alibaba.alink.operator.stream.dataproc.IndexToStringPredictStreamOp;
  6. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  7. import com.alibaba.alink.pipeline.dataproc.StringIndexer;
  8. import com.alibaba.alink.pipeline.dataproc.StringIndexerModel;
  9. import org.junit.Test;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class IndexToStringTest {
  13. @Test
  14. public void testIndexToString() throws Exception {
  15. List <Row> df_data = Arrays.asList(
  16. Row.of("football"),
  17. Row.of("football"),
  18. Row.of("football"),
  19. Row.of("basketball"),
  20. Row.of("basketball"),
  21. Row.of("tennis")
  22. );
  23. BatchOperator <?> train_data = new MemSourceBatchOp(df_data, "f0 string");
  24. StreamOperator <?> data = new MemSourceStreamOp(df_data, "f0 string");
  25. StringIndexerModel stringIndexer = new StringIndexer()
  26. .setModelName("string_indexer_model")
  27. .setSelectedCol("f0")
  28. .setOutputCol("f0_indexed")
  29. .setStringOrderType("frequency_asc").fit(train_data);
  30. BatchOperator batch_model = stringIndexer.transform(train_data);
  31. StreamOperator indexed = stringIndexer.transform(data);
  32. StreamOperator <?> indexToStrings = new IndexToStringPredictStreamOp(batch_model)
  33. .setSelectedCol("f0_indexed")
  34. .setOutputCol("f0_indxed_unindexed");
  35. indexToStrings.linkFrom(indexed).print();
  36. StreamOperator.execute();
  37. }
  38. }

运行结果

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

| football | 2 | football |

| football | 2 | football |

| football | 2 | football |

| basketball | 1 | basketball |

| basketball | 1 | basketball |

| tennis | 0 | tennis |