Spaces:
Sleeping
Sleeping
Update utils/timefeatures.py
Browse files- utils/timefeatures.py +73 -114
utils/timefeatures.py
CHANGED
|
@@ -1,148 +1,107 @@
|
|
| 1 |
-
#
|
| 2 |
-
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License").
|
| 5 |
-
# You may not use this file except in compliance with the License.
|
| 6 |
-
# A copy of the License is located at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# or in the "license" file accompanying this file. This file is distributed
|
| 11 |
-
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
| 12 |
-
# express or implied. See the License for the specific language governing
|
| 13 |
-
# permissions and limitations under the License.
|
| 14 |
-
|
| 15 |
-
from typing import List
|
| 16 |
|
| 17 |
import numpy as np
|
| 18 |
import pandas as pd
|
| 19 |
-
from
|
| 20 |
-
from pandas.tseries.frequencies import to_offset
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
|
|
|
| 27 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 28 |
pass
|
| 29 |
|
| 30 |
-
|
| 31 |
-
return self.__class__.__name__ + "()"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
class SecondOfMinute(TimeFeature):
|
| 35 |
"""Minute of hour encoded as value between [-0.5, 0.5]"""
|
| 36 |
-
|
| 37 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
class MinuteOfHour(TimeFeature):
|
| 42 |
"""Minute of hour encoded as value between [-0.5, 0.5]"""
|
| 43 |
-
|
| 44 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
class HourOfDay(TimeFeature):
|
| 49 |
"""Hour of day encoded as value between [-0.5, 0.5]"""
|
| 50 |
-
|
| 51 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
class DayOfWeek(TimeFeature):
|
| 56 |
-
"""Hour of day encoded as value between [-0.5, 0.5]"""
|
| 57 |
|
|
|
|
|
|
|
| 58 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
class DayOfMonth(
|
| 63 |
"""Day of month encoded as value between [-0.5, 0.5]"""
|
| 64 |
-
|
| 65 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
-
class DayOfYear(
|
| 70 |
"""Day of year encoded as value between [-0.5, 0.5]"""
|
| 71 |
-
|
| 72 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
class MonthOfYear(
|
| 77 |
"""Month of year encoded as value between [-0.5, 0.5]"""
|
| 78 |
-
|
| 79 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
class WeekOfYear(
|
| 84 |
"""Week of year encoded as value between [-0.5, 0.5]"""
|
| 85 |
-
|
| 86 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 91 |
"""
|
| 92 |
-
Returns a list of time features that will be
|
| 93 |
-
Parameters
|
| 94 |
-
----------
|
| 95 |
-
freq_str
|
| 96 |
-
Frequency string of the form [multiple][granularity] such as "12H", "5min", "1D" etc.
|
| 97 |
"""
|
| 98 |
-
|
| 99 |
features_by_offsets = {
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
HourOfDay,
|
| 110 |
-
DayOfWeek,
|
| 111 |
-
DayOfMonth,
|
| 112 |
-
DayOfYear,
|
| 113 |
-
],
|
| 114 |
-
offsets.Second: [
|
| 115 |
-
SecondOfMinute,
|
| 116 |
-
MinuteOfHour,
|
| 117 |
-
HourOfDay,
|
| 118 |
-
DayOfWeek,
|
| 119 |
-
DayOfMonth,
|
| 120 |
-
DayOfYear,
|
| 121 |
-
],
|
| 122 |
}
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/timefeatures.py (최종 수정 버전)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
+
from typing import List
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# -------------------------------------------------------------------------
|
| 8 |
+
# 이 파일의 모든 코드를 아래 내용으로 교체하면 됩니다.
|
| 9 |
+
# -------------------------------------------------------------------------
|
| 10 |
|
| 11 |
+
class BaseTimeFeature:
|
| 12 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 13 |
pass
|
| 14 |
|
| 15 |
+
class SecondOfMinute(BaseTimeFeature):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""Minute of hour encoded as value between [-0.5, 0.5]"""
|
|
|
|
| 17 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 18 |
+
# ⭐️ 수정: index.second -> index.dt.second
|
| 19 |
+
return index.dt.second / 59.0 - 0.5
|
| 20 |
|
| 21 |
+
class MinuteOfHour(BaseTimeFeature):
|
|
|
|
| 22 |
"""Minute of hour encoded as value between [-0.5, 0.5]"""
|
|
|
|
| 23 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 24 |
+
# ⭐️ 수정: index.minute -> index.dt.minute
|
| 25 |
+
return index.dt.minute / 59.0 - 0.5
|
| 26 |
|
| 27 |
+
class HourOfDay(BaseTimeFeature):
|
|
|
|
| 28 |
"""Hour of day encoded as value between [-0.5, 0.5]"""
|
|
|
|
| 29 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 30 |
+
# ⭐️ 수정: index.hour -> index.dt.hour
|
| 31 |
+
return index.dt.hour / 23.0 - 0.5
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
class DayOfWeek(BaseTimeFeature):
|
| 34 |
+
"""Day of week encoded as value between [-0.5, 0.5]"""
|
| 35 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 36 |
+
# ⭐️ 수정: index.dayofweek -> index.dt.dayofweek
|
| 37 |
+
return index.dt.dayofweek / 6.0 - 0.5
|
| 38 |
|
| 39 |
+
class DayOfMonth(BaseTimeFeature):
|
| 40 |
"""Day of month encoded as value between [-0.5, 0.5]"""
|
|
|
|
| 41 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 42 |
+
# ⭐️ 수정: index.day -> index.dt.day
|
| 43 |
+
return (index.dt.day - 1) / 30.0 - 0.5
|
| 44 |
|
| 45 |
+
class DayOfYear(BaseTimeFeature):
|
| 46 |
"""Day of year encoded as value between [-0.5, 0.5]"""
|
|
|
|
| 47 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 48 |
+
# ⭐️ 수정: index.dayofyear -> index.dt.dayofyear
|
| 49 |
+
return (index.dt.dayofyear - 1) / 365.0 - 0.5
|
| 50 |
|
| 51 |
+
class MonthOfYear(BaseTimeFeature):
|
| 52 |
"""Month of year encoded as value between [-0.5, 0.5]"""
|
|
|
|
| 53 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 54 |
+
# ⭐️ 수정: index.month -> index.dt.month
|
| 55 |
+
return (index.dt.month - 1) / 11.0 - 0.5
|
| 56 |
|
| 57 |
+
class WeekOfYear(BaseTimeFeature):
|
| 58 |
"""Week of year encoded as value between [-0.5, 0.5]"""
|
|
|
|
| 59 |
def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
|
| 60 |
+
# ⭐️ 수정: index.isocalendar().week -> index.dt.isocalendar().week
|
| 61 |
+
# .astype(float) 추가
|
| 62 |
+
return (index.dt.isocalendar().week.astype(float) - 1) / 52.0 - 0.5
|
| 63 |
+
|
| 64 |
+
def time_features_from_frequency_str(freq_str: str) -> List[BaseTimeFeature]:
|
| 65 |
"""
|
| 66 |
+
Returns a list of time features that will be used for a given frequency string.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
|
|
|
| 68 |
features_by_offsets = {
|
| 69 |
+
"Y": ["year"],
|
| 70 |
+
"M": ["month", "year"],
|
| 71 |
+
"W": ["day", "week", "month", "year"],
|
| 72 |
+
"D": ["day", "week", "month", "year"],
|
| 73 |
+
"B": ["day", "week", "month", "year"],
|
| 74 |
+
"H": ["hour", "day", "week", "month", "year"],
|
| 75 |
+
"T": ["minute", "hour", "day", "week", "month", "year"],
|
| 76 |
+
"min": ["minute", "hour", "day", "week", "month", "year"],
|
| 77 |
+
"S": ["second", "minute", "hour", "day", "week", "month", "year"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
+
offset = freq_str.split("-")[-1]
|
| 80 |
+
for name, feats in features_by_offsets.items():
|
| 81 |
+
if offset.startswith(name):
|
| 82 |
+
return [
|
| 83 |
+
cls()
|
| 84 |
+
for cls in FEATURES_MAP.values()
|
| 85 |
+
if cls.name in feats
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
FEATURES_MAP = {
|
| 89 |
+
"year": "Year",
|
| 90 |
+
"month": MonthOfYear,
|
| 91 |
+
"week": WeekOfYear,
|
| 92 |
+
"day": DayOfMonth,
|
| 93 |
+
"dayofweek": DayOfWeek,
|
| 94 |
+
"dayofyear": DayOfYear,
|
| 95 |
+
"hour": HourOfDay,
|
| 96 |
+
"minute": MinuteOfHour,
|
| 97 |
+
"second": SecondOfMinute,
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
def time_features(dates, freq="H"):
|
| 101 |
+
# ⭐️ 이 함수 내부 로직을 pandas 최신 버전에 맞게 수정했습니다.
|
| 102 |
+
if isinstance(dates, pd.DataFrame):
|
| 103 |
+
dates = pd.to_datetime(dates.iloc[:, 0])
|
| 104 |
+
|
| 105 |
+
return np.vstack(
|
| 106 |
+
[feat(dates) for feat in time_features_from_frequency_str(freq)]
|
| 107 |
+
).transpose(1, 0)
|