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

功能介绍

数据归一化预测组件
将数据归一到minValue和maxValue之间,value最终结果为 (value - min) / (max - min) * (maxValue - minValue) + minValue,最终结果的范围为[minValue, maxValue]。
minValue和maxValue由用户指定,默认为0和1
需要加载由MinMaxScalerTrainBatchOp训练的模型

参数说明

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

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

| outputCols | 输出结果列列名数组 | 输出结果列列名数组,可选,默认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. colnames = ["col1", "col2", "col3"]
  14. selectedColNames = ["col2", "col3"]
  15. inOp = BatchOperator.fromDataframe(df, schemaStr='col1 string, col2 double, col3 long')
  16. # train
  17. trainOp = MinMaxScalerTrainBatchOp()\
  18. .setSelectedCols(selectedColNames)
  19. trainOp.linkFrom(inOp)
  20. # batch predict
  21. predictOp = MinMaxScalerPredictBatchOp()
  22. predictOp.linkFrom(trainOp, inOp).print()
  23. # stream predict
  24. sinOp = StreamOperator.fromDataframe(df, schemaStr='col1 string, col2 double, col3 long')
  25. predictStreamOp = MinMaxScalerPredictStreamOp(trainOp)
  26. predictStreamOp.linkFrom(sinOp).print()
  27. 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.MinMaxScalerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.dataproc.MinMaxScalerTrainBatchOp;
  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.MinMaxScalerPredictStreamOp;
  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 MinMaxScalerPredictStreamOpTest {
  13. @Test
  14. public void testMinMaxScalerPredictStreamOp() 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. String[] selectedColNames = new String[] {"col2", "col3"};
  25. BatchOperator <?> inOp = new MemSourceBatchOp(df, "col1 string, col2 double, col3 int");
  26. BatchOperator <?> trainOp = new MinMaxScalerTrainBatchOp()
  27. .setSelectedCols(selectedColNames);
  28. trainOp.linkFrom(inOp);
  29. BatchOperator <?> predictOp = new MinMaxScalerPredictBatchOp();
  30. predictOp.linkFrom(trainOp, inOp).print();
  31. StreamOperator <?> sinOp = new MemSourceStreamOp(df, "col1 string, col2 double, col3 int");
  32. StreamOperator <?> predictStreamOp = new MinMaxScalerPredictStreamOp(trainOp);
  33. predictStreamOp.linkFrom(sinOp).print();
  34. StreamOperator.execute();
  35. }
  36. }

运行结果

| col1 | col2 | col3 | | —- | —- | —- |

| a | 0.5473 | 1.0000 |

| b | 0.4851 | 0.0808 |

| c | 0.9965 | 0.0000 |

| d | 0.0000 | 1.0000 |

| a | 0.5045 | 0.0000 |

| b | 0.4866 | 0.0808 |

| c | 1.0000 | 0.0000 |

| col1 | col2 | col3 | | —- | —- | —- |

| a | 0.5473 | 1.0000 |

| c | 1.0000 | 0.0000 |

| b | 0.4851 | 0.0808 |

| c | 0.9965 | 0.0000 |

| b | 0.4866 | 0.0808 |

| d | 0.0000 | 1.0000 |

| a | 0.5045 | 0.0000 |