【读代码】关于日期的处理
return np.vstack([feat(dates) for feat in time_features_from_frequency_str(freq)])
很多的类,用字典,调用类,还有类的重命名
除以 23 表示第几个小时,一天中的第几个小时
dayofhour是读出来的
dayofweek 是 python 内置库转换来
dayofmonth 也是可以直接读出来的
dayof year 也是直接读出来
为什么都减去 0.5:中心化
奥,所以除以的数都比正常的小 1
规律:
为什么这里除以 11
class MonthOfYear(TimeFeature):
"""Month of year encoded as value between [-0.5, 0.5]"""
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
return (index.month - 1) / 11.0 - 0.5
这里的对于日期的编码还蛮有趣
class DayOfWeek(TimeFeature):
"""Hour of day encoded as value between [-0.5, 0.5]"""
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
return index.dayofweek / 6.0 - 0.5 # Python内部返回 0——6 python内部计算 day 是周几,同时 除以 6 是为了把 周一到周六调成 0——1的范围,周一默认 0,周二=1,周日=6 so__都可以调到 0——1的范围内
class DayOfMonth(TimeFeature):
"""Day of month encoded as value between [-0.5, 0.5]"""
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
return (index.day - 1) / 30.0 - 0.5 # 1——31
class DayOfYear(TimeFeature):
"""Day of year encoded as value between [-0.5, 0.5]"""
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
return (index.dayofyear - 1) / 365.0 - 0.5 # 1——366
class MonthOfYear(TimeFeature):
"""Month of year encoded as value between [-0.5, 0.5]"""
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
return (index.month - 1) / 11.0 - 0.5 # 1——12
class WeekOfYear(TimeFeature):
"""Week of year encoded as value between [-0.5, 0.5]"""
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
return (index.isocalendar().week - 1) / 52.0 - 0.5 # 返回 1——53,--》-1 到 0——52 》-0.5 中心化