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

功能介绍

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

使用方式

参考文档 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"), 200.0],
  10. [0, datetime.datetime.fromisoformat("2021-11-03 00:00:00"), 300.0],
  11. [0, datetime.datetime.fromisoformat("2021-11-04 00:00:00"), 400.0],
  12. [0, datetime.datetime.fromisoformat("2021-11-06 00:00:00"), 500.0],
  13. [0, datetime.datetime.fromisoformat("2021-11-07 00:00:00"), 600.0],
  14. [0, datetime.datetime.fromisoformat("2021-11-08 00:00:00"), 700.0],
  15. [0, datetime.datetime.fromisoformat("2021-11-09 00:00:00"), 800.0],
  16. [0, datetime.datetime.fromisoformat("2021-11-10 00:00:00"), 900.0],
  17. [0, datetime.datetime.fromisoformat("2021-11-11 00:00:00"), 800.0],
  18. [0, datetime.datetime.fromisoformat("2021-11-12 00:00:00"), 700.0],
  19. [0, datetime.datetime.fromisoformat("2021-11-13 00:00:00"), 600.0],
  20. [0, datetime.datetime.fromisoformat("2021-11-14 00:00:00"), 500.0],
  21. [0, datetime.datetime.fromisoformat("2021-11-15 00:00:00"), 400.0],
  22. [0, datetime.datetime.fromisoformat("2021-11-16 00:00:00"), 300.0],
  23. [0, datetime.datetime.fromisoformat("2021-11-17 00:00:00"), 200.0],
  24. [0, datetime.datetime.fromisoformat("2021-11-18 00:00:00"), 100.0],
  25. [0, datetime.datetime.fromisoformat("2021-11-19 00:00:00"), 200.0],
  26. [0, datetime.datetime.fromisoformat("2021-11-20 00:00:00"), 300.0],
  27. [0, datetime.datetime.fromisoformat("2021-11-21 00:00:00"), 400.0],
  28. [0, datetime.datetime.fromisoformat("2021-11-22 00:00:00"), 500.0],
  29. [0, datetime.datetime.fromisoformat("2021-11-23 00:00:00"), 600.0],
  30. [0, datetime.datetime.fromisoformat("2021-11-24 00:00:00"), 700.0],
  31. [0, datetime.datetime.fromisoformat("2021-11-25 00:00:00"), 800.0],
  32. [0, datetime.datetime.fromisoformat("2021-11-26 00:00:00"), 900.0],
  33. [0, datetime.datetime.fromisoformat("2021-11-27 00:00:00"), 800.0],
  34. [0, datetime.datetime.fromisoformat("2021-11-28 00:00:00"), 700.0],
  35. [0, datetime.datetime.fromisoformat("2021-11-29 00:00:00"), 600.0],
  36. [0, datetime.datetime.fromisoformat("2021-11-30 00:00:00"), 500.0],
  37. [0, datetime.datetime.fromisoformat("2021-12-01 00:00:00"), 400.0],
  38. [0, datetime.datetime.fromisoformat("2021-12-02 00:00:00"), 300.0],
  39. [0, datetime.datetime.fromisoformat("2021-12-03 00:00:00"), 200.0]
  40. ])
  41. batch_source = dataframeToOperator(data, schemaStr='id int, ts timestamp, series double', op_type='batch')
  42. stream_source = dataframeToOperator(data, schemaStr='id int, ts timestamp, series double', op_type='stream')
  43. lstNetTrainBatchOp = LSTNetTrainBatchOp()\
  44. .setTimeCol("ts")\
  45. .setSelectedCol("series")\
  46. .setNumEpochs(10)\
  47. .setWindow(24)\
  48. .setHorizon(1)\
  49. .linkFrom(batch_source)
  50. overCountWindowStreamOp = OverCountWindowStreamOp()\
  51. .setClause("MTABLE_AGG_PRECEDING(ts, series) as mtable_agg_series")\
  52. .setTimeCol("ts")\
  53. .setPrecedingRows(24)
  54. lstNetPredictStreamOp = LSTNetPredictStreamOp(lstNetTrainBatchOp)\
  55. .setPredictNum(1)\
  56. .setPredictionCol("pred")\
  57. .setReservedCols([])\
  58. .setValueCol("mtable_agg_series")
  59. lstNetPredictStreamOp\
  60. .linkFrom(
  61. overCountWindowStreamOp\
  62. .linkFrom(stream_source)\
  63. .filter("ts = TO_TIMESTAMP('2021-12-03 00:00:00')")
  64. )\
  65. .print();
  66. 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.LSTNetPredictStreamOp;
  8. import org.junit.Test;
  9. import java.sql.Timestamp;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class LSTNetPredictStreamOpTest {
  13. @Test
  14. public void testLSTNetTrainBatchOp() 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"), 200.0),
  19. Row.of(0, Timestamp.valueOf("2021-11-03 00:00:00"), 300.0),
  20. Row.of(0, Timestamp.valueOf("2021-11-04 00:00:00"), 400.0),
  21. Row.of(0, Timestamp.valueOf("2021-11-06 00:00:00"), 500.0),
  22. Row.of(0, Timestamp.valueOf("2021-11-07 00:00:00"), 600.0),
  23. Row.of(0, Timestamp.valueOf("2021-11-08 00:00:00"), 700.0),
  24. Row.of(0, Timestamp.valueOf("2021-11-09 00:00:00"), 800.0),
  25. Row.of(0, Timestamp.valueOf("2021-11-10 00:00:00"), 900.0),
  26. Row.of(0, Timestamp.valueOf("2021-11-11 00:00:00"), 800.0),
  27. Row.of(0, Timestamp.valueOf("2021-11-12 00:00:00"), 700.0),
  28. Row.of(0, Timestamp.valueOf("2021-11-13 00:00:00"), 600.0),
  29. Row.of(0, Timestamp.valueOf("2021-11-14 00:00:00"), 500.0),
  30. Row.of(0, Timestamp.valueOf("2021-11-15 00:00:00"), 400.0),
  31. Row.of(0, Timestamp.valueOf("2021-11-16 00:00:00"), 300.0),
  32. Row.of(0, Timestamp.valueOf("2021-11-17 00:00:00"), 200.0),
  33. Row.of(0, Timestamp.valueOf("2021-11-18 00:00:00"), 100.0),
  34. Row.of(0, Timestamp.valueOf("2021-11-19 00:00:00"), 200.0),
  35. Row.of(0, Timestamp.valueOf("2021-11-20 00:00:00"), 300.0),
  36. Row.of(0, Timestamp.valueOf("2021-11-21 00:00:00"), 400.0),
  37. Row.of(0, Timestamp.valueOf("2021-11-22 00:00:00"), 500.0),
  38. Row.of(0, Timestamp.valueOf("2021-11-23 00:00:00"), 600.0),
  39. Row.of(0, Timestamp.valueOf("2021-11-24 00:00:00"), 700.0),
  40. Row.of(0, Timestamp.valueOf("2021-11-25 00:00:00"), 800.0),
  41. Row.of(0, Timestamp.valueOf("2021-11-26 00:00:00"), 900.0),
  42. Row.of(0, Timestamp.valueOf("2021-11-27 00:00:00"), 800.0),
  43. Row.of(0, Timestamp.valueOf("2021-11-28 00:00:00"), 700.0),
  44. Row.of(0, Timestamp.valueOf("2021-11-29 00:00:00"), 600.0),
  45. Row.of(0, Timestamp.valueOf("2021-11-30 00:00:00"), 500.0),
  46. Row.of(0, Timestamp.valueOf("2021-12-01 00:00:00"), 400.0),
  47. Row.of(0, Timestamp.valueOf("2021-12-02 00:00:00"), 300.0),
  48. Row.of(0, Timestamp.valueOf("2021-12-03 00:00:00"), 200.0)
  49. );
  50. MemSourceBatchOp memSourceBatchOp = new MemSourceBatchOp(
  51. data, "id int, ts timestamp, series double"
  52. );
  53. MemSourceStreamOp memSourceStreamOp = new MemSourceStreamOp(
  54. data, "id int, ts timestamp, series double"
  55. );
  56. LSTNetTrainBatchOp lstNetTrainBatchOp = new LSTNetTrainBatchOp()
  57. .setTimeCol("ts")
  58. .setSelectedCol("series")
  59. .setNumEpochs(10)
  60. .setWindow(24)
  61. .setHorizon(1)
  62. .linkFrom(memSourceBatchOp);
  63. OverCountWindowStreamOp overCountWindowStreamOp = new OverCountWindowStreamOp()
  64. .setClause("MTABLE_AGG_PRECEDING(ts, series) as mtable_agg_series")
  65. .setTimeCol("ts")
  66. .setPrecedingRows(24);
  67. LSTNetPredictStreamOp lstNetPredictStreamOp = new LSTNetPredictStreamOp(lstNetTrainBatchOp)
  68. .setPredictNum(1)
  69. .setPredictionCol("pred")
  70. .setReservedCols()
  71. .setValueCol("mtable_agg_series");
  72. lstNetPredictStreamOp
  73. .linkFrom(
  74. overCountWindowStreamOp
  75. .linkFrom(memSourceStreamOp)
  76. .filter("ts = TO_TIMESTAMP('2021-12-03 00:00:00')")
  77. )
  78. .print();
  79. StreamOperator.execute();
  80. }
  81. }

运行结果

| pred | | —- |

| {“data”:{“ts”:[“2021-12-04 00:00:00.0”],”series”:[441.76019287109375]},”schema”:”ts TIMESTAMP,series DOUBLE”} |