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

功能介绍

标准化是对向量数据进行按正态化处理的组件
加载VectorStandardScalerTrainBatchOp中生成的模型,对向量数据做标准化预处理。

参数说明

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

| modelFilePath | 模型的文件路径 | 模型的文件路径 | String | | | null |

| outputCol | 输出结果列 | 输出结果列列名,可选,默认null | 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. ["a", "10.0, 100"],
  6. ["b", "-2.5, 9"],
  7. ["c", "100.2, 1"],
  8. ["d", "-99.9, 100"],
  9. ["a", "1.4, 1"],
  10. ["b", "-2.2, 9"],
  11. ["c", "100.9, 1"]
  12. ])
  13. data = BatchOperator.fromDataframe(df, schemaStr="col1 string, vec string")
  14. colnames = ["col1", "vec"]
  15. selectedColName = "vec"
  16. trainOp = VectorStandardScalerTrainBatchOp()\
  17. .setSelectedCol(selectedColName)
  18. model = trainOp.linkFrom(data)
  19. #batch predict
  20. batchPredictOp = VectorStandardScalerPredictBatchOp()
  21. batchPredictOp.linkFrom(model, data).print()
  22. #stream predict
  23. streamData = StreamOperator.fromDataframe(df, schemaStr="col1 string, vec string")
  24. streamPredictOp = VectorStandardScalerPredictStreamOp(trainOp)
  25. streamData.link(streamPredictOp).print()
  26. 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.vector.VectorStandardScalerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.dataproc.vector.VectorStandardScalerTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import com.alibaba.alink.operator.stream.StreamOperator;
  7. import com.alibaba.alink.operator.stream.dataproc.vector.VectorStandardScalerPredictStreamOp;
  8. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  9. import org.junit.Test;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class VectorStandardScalerPredictStreamOpTest {
  13. @Test
  14. public void testVectorStandardScalerPredictStreamOp() throws Exception {
  15. List <Row> df = Arrays.asList(
  16. Row.of("a", "10.0, 100"),
  17. Row.of("b", "-2.5, 9"),
  18. Row.of("c", "100.2, 1"),
  19. Row.of("d", "-99.9, 100"),
  20. Row.of("a", "1.4, 1"),
  21. Row.of("b", "-2.2, 9"),
  22. Row.of("c", "100.9, 1")
  23. );
  24. BatchOperator <?> data = new MemSourceBatchOp(df, "col1 string, vec string");
  25. BatchOperator <?> trainOp = new VectorStandardScalerTrainBatchOp()
  26. .setSelectedCol("vec");
  27. BatchOperator <?> model = trainOp.linkFrom(data);
  28. BatchOperator <?> batchPredictOp = new VectorStandardScalerPredictBatchOp();
  29. batchPredictOp.linkFrom(model, data).print();
  30. StreamOperator <?> streamData = new MemSourceStreamOp(df, "col1 string, vec string");
  31. StreamOperator <?> streamPredictOp = new VectorStandardScalerPredictStreamOp(trainOp);
  32. streamData.link(streamPredictOp).print();
  33. StreamOperator.execute();
  34. }
  35. }

运行结果

| col1 | vec | | —- | —- |

| a | -0.07835182408093559,1.4595814453461897 |

| c | 1.2269606224811418,-0.6520885789229323 |

| b | -0.2549018445693762,-0.4814485769617911 |

| a | -0.20280511721213143,-0.6520885789229323 |

| c | 1.237090541689495,-0.6520885789229323 |

| b | -0.25924323851581327,-0.4814485769617911 |

| d | -1.6687491397923802,1.4595814453461897 |