在时间序列预测中处理真实世界的数据

来自真实世界的时间序列数据通常不完善,存在缺失、重复或不对齐的时间步。预测和异常检测 ML 函数包括以下预处理功能,可帮助您使用真实世界的数据来训练可做出有用预测的模型:

  • 您可以指定事件频率来替换模型自动推断的频率。
  • The model can infer data at missing time steps and aggregate multiple values within a time step. You can specify how aggregation should be done for each feature or type of feature, or let the ML function do it for you automatically.

即使您的训练数据存在常见的一致性问题,这些功能也能让您训练出有用的模型。通常,数据一致性越好,预测模型就越准确,但数量相对较少的此类调整不会明显影响模型的准确性。

指定事件频率

Model training infers the frequency of the time steps in your training data using heuristics that, on rare occasions, choose the wrong frequency. To avoid this risk, or to correct an incorrect inference, you can optionally specify the desired frequency when initiating training using the CONFIG_OBJECT parameter frequency. This parameter specifies a time period in a form similar to '1 day' or '2 weeks'.

  • 时间间隔指定必须放在单引号内,因为它是一个字符串。
  • 支持的时间间隔包括秒、分钟、小时、天、周、月、季度和年。
  • 使用完整的时间间隔名称,而不是缩写。可以使用复数。(“Second”或“seconds”,而不是“sec”)。

以下示例显示如何使用一天的频率训练预测模型。

CREATE SNOWFLAKE.ML.FORECAST model1(
  INPUT_DATA => TABLE(v1),
  TIMESTAMP_COLNAME => 'date',
  TARGET_COLNAME => 'sales',
  CONFIG_OBJECT => {'frequency': '1 day'}
);

如果您未指定事件频率,训练过程将推断最接近的匹配事件频率。

填充缺失时间步的值

没有目标值的时间戳使用以下方法填充:

不会填充缺失的特征值,而是替换为 NULL 值。模型训练会忽略这些值。

在一个时间步中处理多个值

当一个时间步中存在多个事件时,预处理可以采用各种方式汇合它们的值。例如,如果事件的频率是每小时一次,则可以对每小时节奏之外发生的值取平均值,以得出最接近的规范每小时时间戳的值。

下表总结了可用的聚合行为。

Kind of valueAvailable behaviorsDefault behavior
Numeric
  • MEAN:值的平均值
  • MEDIAN:中间值
  • MODE:最常见的值
  • MIN:最低值
  • MAX:最高值
  • SUM:值总计
  • FIRST:最早值
  • LAST:最新值
MEAN
Categorical (string or Boolean)
  • MODE:最常见的值
  • FIRST:最早值
  • LAST:最新值
MODE

Tip

将 SUM 方法用于计数数据,例如已售出的商品数量。MEAN 适用于大多数其他数值。

所有行为都忽略 NULL 值并应用于要插值或聚合的时间段。例如,按每小时节奏对值执行 SUM 是以标准时间戳为中心的一小时内的值之和。

您可以通过两种方式替换列的默认行为:

  • 按值类型(目标、数字或分类)
  • 按确切的列名称

如果您以两种方式替换行为,则列名替换优先。

按值的类型替换

Set the following options in the function’s CONFIG_OBJECT parameter to override specific types of values: categorical, numeric, and target. The behaviors are as previously defined.

选项可能的值
aggregation_categoricalMODE、FIRST、LAST
aggregation_numericMEAN、MEDIAN、MODE、MIN、MAX、SUM、FIRST、LAST
aggregation_targetMEAN、MEDIAN、MODE、MIN、MAX、SUM、FIRST、LAST

Note

If aggregation_target is not specified, target aggregation uses the behavior, if any, specified by aggregate_numeric. Otherwise, the default, MEAN, is used.

以下示例显示如何设置分类和数字特征的聚合行为。

CREATE SNOWFLAKE.ML.FORECAST model1(
  INPUT_DATA => TABLE(v1),
  TIMESTAMP_COLNAME => 'date',
  TARGET_COLNAME => 'sales',
  CONFIG_OBJECT => {
    'frequency': '1 day',
    'aggregation_categorical': 'MODE',
    'aggregation_numeric': 'MEDIAN'}
);

Tip

即使使用默认值,也请考虑指定所有这些值。这样一来,您不需要知道默认行为是什么就可以理解该语句要做什么,并且如果您以后想要更改该行为,则不需要查找参数名称。

按列名替换

The aggregation_column option in CONFIG_OBJECT is an object that maps behaviors to column names. These behaviors override any behaviors specified using the parameters described above.

Note

The aggregation behavior for the target value cannot be specified by column name. Use the aggregation_target option instead.

For example, the following SQL statement specifies aggregation behaviors for two different columns using the aggregation_column option.

CREATE SNOWFLAKE.ML.FORECAST model1(
  INPUT_DATA => TABLE(v1),
  TIMESTAMP_COLNAME => 'date',
  TARGET_COLNAME => 'sales',
  CONFIG_OBJECT => {
    'frequency': '1 day',
    'aggregation_target': 'MEDIAN',
    'aggregation_column': {
        'temperature': 'MEDIAN',
        'employee_id': 'FIRST'
    }
  }
);