%load_ext autoreload
%autoreload 2自定义日期特征
定义您自己的函数作为日期特征
from mlforecast import MLForecast
from mlforecast.utils import generate_daily_seriesMLForecast 的 date_features 参数可以接受 pandas 日期属性以及接受 pandas DatetimeIndex 并返回数值的函数。函数的名称将作为特征的名称,因此请使用唯一且具有描述性的名称。
series = generate_daily_series(1, min_length=6, max_length=6)def even_day(dates):
"""日期是偶数"""
return dates.day % 2 == 0
def month_start_or_end(dates):
"""日期是月初或月末"""
return dates.is_month_start | dates.is_month_end
def is_monday(dates):
"""日期是星期一"""
return dates.dayofweek == 0fcst = MLForecast(
[],
freq='D',
date_features=['dayofweek', 'dayofyear', even_day, month_start_or_end, is_monday]
)
fcst.preprocess(series)| unique_id | ds | y | dayofweek | dayofyear | even_day | month_start_or_end | is_monday | |
|---|---|---|---|---|---|---|---|---|
| 0 | id_0 | 2000-01-01 | 0.274407 | 5 | 1 | False | True | False |
| 1 | id_0 | 2000-01-02 | 1.357595 | 6 | 2 | True | False | False |
| 2 | id_0 | 2000-01-03 | 2.301382 | 0 | 3 | False | False | True |
| 3 | id_0 | 2000-01-04 | 3.272442 | 1 | 4 | True | False | False |
| 4 | id_0 | 2000-01-05 | 4.211827 | 2 | 5 | False | False | False |
| 5 | id_0 | 2000-01-06 | 5.322947 | 3 | 6 | True | False | False |
Give us a ⭐ on Github