Java 类名:com.alibaba.alink.operator.stream.feature.CrossFeaturePredictStreamOp
Python 类名:CrossFeaturePredictStreamOp

功能介绍

将选定的特征列组合成单个向量类型的特征。

使用方式

该组件是预测组件,需要配合预测组件 CrossFeatureTrainBatchOp 使用。
使用中指定输出列名(outputCol)即可。

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| outputCol | 输出结果列列名 | 输出结果列列名,必选 | String | ✓ | | |

| modelFilePath | 模型的文件路径 | 模型的文件路径 | 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 = pd.DataFrame([
  5. ["1.0", "1.0", 1.0, 1],
  6. ["1.0", "1.0", 0.0, 1],
  7. ["1.0", "0.0", 1.0, 1],
  8. ["1.0", "0.0", 1.0, 1],
  9. ["2.0", "3.0", None, 0],
  10. ["2.0", "3.0", 1.0, 0],
  11. ["0.0", "1.0", 2.0, 0],
  12. ["0.0", "1.0", 1.0, 0]])
  13. batchData = BatchOperator.fromDataframe(df, schemaStr="f0 string, f1 string, f2 double, label bigint")
  14. streamData = StreamOperator.fromDataframe(df, schemaStr="f0 string, f1 string, f2 double, label bigint")
  15. train = CrossFeatureTrainBatchOp().setSelectedCols(['f0','f1','f2']).linkFrom(batchData)
  16. CrossFeaturePredictStreamOp(train).setOutputCol("cross").linkFrom(streamData).print()
  17. 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.feature.CrossFeaturePredictBatchOp;
  4. import com.alibaba.alink.operator.batch.feature.CrossFeatureTrainBatchOp;
  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 CrossFeaturePredictStreamOpTest {
  10. @Test
  11. public void testCrossFeaturePredictStreamOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of("1.0", "1.0", 1.0, 1),
  14. Row.of("1.0", "1.0", 0.0, 1),
  15. Row.of("1.0", "0.0", 1.0, 1),
  16. Row.of("1.0", "0.0", 1.0, 1),
  17. Row.of("2.0", "3.0", null, 0),
  18. Row.of("2.0", "3.0", 1.0, 0),
  19. Row.of("0.0", "1.0", 2.0, 0)
  20. );
  21. BatchOperator <?> batchData = new MemSourceBatchOp(df, "f0 string, f1 string, f2 double, label int");
  22. StreamOperator<?> streamData = new MemSourceStreamOp(df, "f0 string, f1 string, f2 double, label int");
  23. BatchOperator <?> train = new CrossFeatureTrainBatchOp().setSelectedCols("f0", "f1", "f2").linkFrom(batchData);
  24. new CrossFeaturePredictStreamOp(train).setOutputCol("cross").linkFrom(streamData).print();
  25. StreamOperator.execute();
  26. }
  27. }

运行结果

| f0 | f1 | f2 | label | cross | | —- | —- | —- | —- | —- |

| 2.0 | 3.0 | 1.0000 | 0 | $36$7:1.0 |

| 0.0 | 1.0 | 2.0000 | 0 | $36$32:1.0 |

| 1.0 | 1.0 | 0.0000 | 1 | $36$12:1.0 |

| 1.0 | 1.0 | 1.0000 | 1 | $36$3:1.0 |

| 1.0 | 0.0 | 1.0000 | 1 | $36$0:1.0 |

| 1.0 | 0.0 | 1.0000 | 1 | $36$0:1.0 |

| 2.0 | 3.0 | null | 0 | $36$25:1.0 |