Java 类名:com.alibaba.alink.operator.stream.dataproc.StringIndexerPredictStreamOp
Python 类名:StringIndexerPredictStreamOp

功能介绍

基于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 |

| 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. data = BatchOperator.fromDataframe(df_data, schemaStr='f0 string')
  13. stream_data = StreamOperator.fromDataframe(df_data, schemaStr='f0 string')
  14. stringindexer = StringIndexerTrainBatchOp() \
  15. .setSelectedCol("f0") \
  16. .setStringOrderType("frequency_asc")
  17. model = stringindexer.linkFrom(data)
  18. predictor = StringIndexerPredictStreamOp(model)\
  19. .setSelectedCol("f0")\
  20. .setOutputCol("f0_indexed")
  21. predictor.linkFrom(stream_data).print()
  22. 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.dataproc.StringIndexerTrainBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.stream.StreamOperator;
  6. import com.alibaba.alink.operator.stream.dataproc.StringIndexerPredictStreamOp;
  7. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  8. import org.junit.Test;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class StringIndexerPredictStreamOpTest {
  12. @Test
  13. public void testStringIndexerPredictStreamOp() throws Exception {
  14. List <Row> df_data = Arrays.asList(
  15. Row.of("football"),
  16. Row.of("football"),
  17. Row.of("football"),
  18. Row.of("basketball"),
  19. Row.of("basketball"),
  20. Row.of("tennis")
  21. );
  22. BatchOperator <?> data = new MemSourceBatchOp(df_data, "f0 string");
  23. StreamOperator <?> stream_data = new MemSourceStreamOp(df_data, "f0 string");
  24. BatchOperator <?> stringindexer = new StringIndexerTrainBatchOp()
  25. .setSelectedCol("f0")
  26. .setStringOrderType("frequency_asc");
  27. BatchOperator model = stringindexer.linkFrom(data);
  28. StreamOperator <?> predictor = new StringIndexerPredictStreamOp(model)
  29. .setSelectedCol("f0")
  30. .setOutputCol("f0_indexed");
  31. predictor.linkFrom(stream_data).print();
  32. StreamOperator.execute();
  33. }
  34. }

运行结果

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

| basketball | 1 |

| football | 2 |

| tennis | 0 |

| basketball | 1 |

| football | 2 |

| football | 2 |