from mlforecast import MLForecast
from mlforecast.utils import generate_daily_series

MLForecast 的 date_features 参数可以接受 pandas 日期属性,也可以接受接受 pandas DatetimeIndex 并返回数值的函数。函数名将用作特征名,因此请使用唯一且具有描述性的名称。

series = generate_daily_series(1, min_length=6, max_length=6)
def even_day(dates):
    """Day of month is even"""
    return dates.day % 2 == 0

def month_start_or_end(dates):
    """Date is month start or month end"""
    return dates.is_month_start | dates.is_month_end

def is_monday(dates):
    """Date is monday"""
    return dates.dayofweek == 0
fcst = MLForecast(
    [],
    freq='D',
    date_features=['dayofweek', 'dayofyear', even_day, month_start_or_end, is_monday]
)
fcst.preprocess(series)
unique_iddsydayofweekdayofyeareven_daymonth_start_or_endis_monday
0id_02000-01-010.27440751
1id_02000-01-021.35759562
2id_02000-01-032.30138203
3id_02000-01-043.27244214
4id_02000-01-054.21182725
5id_02000-01-065.32294736