Java 类名:com.alibaba.alink.operator.stream.timeseries.DeepARPredictStreamOp
Python 类名:DeepARPredictStreamOp

功能介绍

使用 DeepAR 进行时间序列训练和预测。

使用方式

参考文档 https://www.yuque.com/pinshu/alink_guide/xbp5ky

参数说明

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

| predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | | |

| valueCol | value列,类型为MTable | value列,类型为MTable | String | ✓ | 所选列类型为 [M_TABLE] | |

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

| predictNum | 预测条数 | 预测条数 | Integer | | | 1 |

| predictionDetailCol | 预测详细信息列名 | 预测详细信息列名 | String | | | |

| 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. import time, datetime
  5. import numpy as np
  6. import pandas as pd
  7. data = pd.DataFrame([
  8. [0, datetime.datetime.fromisoformat('2021-11-01 00:00:00'), 100.0],
  9. [0, datetime.datetime.fromisoformat('2021-11-02 00:00:00'), 100.0],
  10. [0, datetime.datetime.fromisoformat('2021-11-03 00:00:00'), 100.0],
  11. [0, datetime.datetime.fromisoformat('2021-11-04 00:00:00'), 100.0],
  12. [0, datetime.datetime.fromisoformat('2021-11-05 00:00:00'), 100.0]
  13. ])
  14. batch_source = dataframeToOperator(data, schemaStr='id int, ts timestamp, series double', op_type='batch')
  15. stream_source = dataframeToOperator(data, schemaStr='id int, ts timestamp, series double', op_type='stream')
  16. deepARTrainBatchOp = DeepARTrainBatchOp()\
  17. .setTimeCol("ts")\
  18. .setSelectedCol("series")\
  19. .setNumEpochs(10)\
  20. .setWindow(2)\
  21. .setStride(1)\
  22. .linkFrom(batch_source)
  23. overCountWindowStreamOp = OverCountWindowStreamOp()\
  24. .setClause("MTABLE_AGG_PRECEDING(ts, series) as mtable_agg_series")\
  25. .setTimeCol("ts")\
  26. .setPrecedingRows(2)
  27. deepARPredictStreamOp = DeepARPredictStreamOp(deepARTrainBatchOp)\
  28. .setPredictNum(2)\
  29. .setPredictionCol("pred")\
  30. .setValueCol("mtable_agg_series")
  31. deepARPredictStreamOp\
  32. .linkFrom(
  33. overCountWindowStreamOp\
  34. .linkFrom(stream_source)\
  35. .filter("ts = TO_TIMESTAMP('2021-11-05 00:00:00')")
  36. )\
  37. .print()
  38. 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.feature.OverCountWindowStreamOp;
  6. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  7. import com.alibaba.alink.operator.stream.timeseries.DeepARPredictStreamOp;
  8. import org.junit.Test;
  9. import java.sql.Timestamp;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class DeepARPredictStreamOpTest {
  13. @Test
  14. public void testDeepARTrainBatchOp() throws Exception {
  15. BatchOperator.setParallelism(1);
  16. List <Row> data = Arrays.asList(
  17. Row.of(0, Timestamp.valueOf("2021-11-01 00:00:00"), 100.0),
  18. Row.of(0, Timestamp.valueOf("2021-11-02 00:00:00"), 100.0),
  19. Row.of(0, Timestamp.valueOf("2021-11-03 00:00:00"), 100.0),
  20. Row.of(0, Timestamp.valueOf("2021-11-04 00:00:00"), 100.0),
  21. Row.of(0, Timestamp.valueOf("2021-11-05 00:00:00"), 100.0)
  22. );
  23. MemSourceBatchOp memSourceBatchOp = new MemSourceBatchOp(data, "id int, ts timestamp, series double");
  24. MemSourceStreamOp memSourceStreamOp = new MemSourceStreamOp(data, "id int, ts timestamp, series double");
  25. DeepARTrainBatchOp deepARTrainBatchOp = new DeepARTrainBatchOp()
  26. .setTimeCol("ts")
  27. .setSelectedCol("series")
  28. .setNumEpochs(10)
  29. .setWindow(2)
  30. .setStride(1)
  31. .linkFrom(memSourceBatchOp);
  32. OverCountWindowStreamOp overCountWindowStreamOp = new OverCountWindowStreamOp()
  33. .setClause("MTABLE_AGG_PRECEDING(ts, series) as mtable_agg_series")
  34. .setTimeCol("ts")
  35. .setPrecedingRows(2);
  36. DeepARPredictStreamOp deepARPredictStreamOp = new DeepARPredictStreamOp(deepARTrainBatchOp)
  37. .setPredictNum(2)
  38. .setPredictionCol("pred")
  39. .setValueCol("mtable_agg_series");
  40. deepARPredictStreamOp
  41. .linkFrom(
  42. overCountWindowStreamOp
  43. .linkFrom(memSourceStreamOp)
  44. .filter("ts = TO_TIMESTAMP('2021-11-05 00:00:00')")
  45. )
  46. .print();
  47. StreamOperator.execute();
  48. }
  49. }

运行结果

| id | mtable_agg_series | pred |
|——+—————————————————————————————————————————————————————————————————————————————————————————————————————————————+————————————————————————————————————————————————————————————————————————————-|
| 0 | {“data”:{“ts”:[“2021-11-01 00:00:00.0”,”2021-11-02 00:00:00.0”,”2021-11-03 00:00:00.0”,”2021-11-04 00:00:00.0”,”2021-11-05 00:00:00.0”],”series”:[100.0,100.0,100.0,100.0,100.0]},”schema”:”ts TIMESTAMP,series DOUBLE”} | {“data”:{“ts”:[“2021-11-06 00:00:00.0”,”2021-11-07 00:00:00.0”],”series”:[31.424224853515625,39.10265350341797]},”schema”:”ts TIMESTAMP,series DOUBLE”} |