mirror of
https://github.com/wassname/pytorch-ts.git
synced 2026-07-30 12:30:17 +08:00
added tune feature
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
from pts.feature.time_feature import (
|
||||
MinuteOfHour,
|
||||
HourOfDay,
|
||||
DayOfWeek,
|
||||
DayOfMonth,
|
||||
DayOfYear,
|
||||
MonthOfYear,
|
||||
WeekOfYear,
|
||||
)
|
||||
@@ -0,0 +1,98 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class TimeFeature(ABC):
|
||||
def __init__(self, normalized: bool = True):
|
||||
self.normalized = normalized
|
||||
|
||||
@abstractmethod
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
pass
|
||||
|
||||
|
||||
class MinuteOfHour(TimeFeature):
|
||||
"""
|
||||
Minute of hour encoded as value between [-0.5, 0.5]
|
||||
"""
|
||||
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
if self.normalized:
|
||||
return index.minute / 59.0 - 0.5
|
||||
else:
|
||||
return index.minute.map(float)
|
||||
|
||||
|
||||
class HourOfDay(TimeFeature):
|
||||
"""
|
||||
Hour of day encoded as value between [-0.5, 0.5]
|
||||
"""
|
||||
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
if self.normalized:
|
||||
return index.hour / 23.0 - 0.5
|
||||
else:
|
||||
return index.hour.map(float)
|
||||
|
||||
|
||||
class DayOfWeek(TimeFeature):
|
||||
"""
|
||||
Hour of day encoded as value between [-0.5, 0.5]
|
||||
"""
|
||||
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
if self.normalized:
|
||||
return index.dayofweek / 6.0 - 0.5
|
||||
else:
|
||||
return index.dayofweek.map(float)
|
||||
|
||||
|
||||
class DayOfMonth(TimeFeature):
|
||||
"""
|
||||
Day of month encoded as value between [-0.5, 0.5]
|
||||
"""
|
||||
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
if self.normalized:
|
||||
return index.day / 30.0 - 0.5
|
||||
else:
|
||||
return index.day.map(float)
|
||||
|
||||
|
||||
class DayOfYear(TimeFeature):
|
||||
"""
|
||||
Day of year encoded as value between [-0.5, 0.5]
|
||||
"""
|
||||
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
if self.normalized:
|
||||
return index.dayofyear / 364.0 - 0.5
|
||||
else:
|
||||
return index.dayofyear.map(float)
|
||||
|
||||
|
||||
class MonthOfYear(TimeFeature):
|
||||
"""
|
||||
Month of year encoded as value between [-0.5, 0.5]
|
||||
"""
|
||||
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
if self.normalized:
|
||||
return index.month / 11.0 - 0.5
|
||||
else:
|
||||
return index.month.map(float)
|
||||
|
||||
|
||||
class WeekOfYear(TimeFeature):
|
||||
"""
|
||||
Week of year encoded as value between [-0.5, 0.5]
|
||||
"""
|
||||
|
||||
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
||||
if self.normalized:
|
||||
return index.weekofyear / 51.0 - 0.5
|
||||
else:
|
||||
return index.weekofyear.map(float)
|
||||
|
||||
Reference in New Issue
Block a user