File size: 4,165 Bytes
0f29326
e1ccef5
 
 
0f29326
e1ccef5
0f29326
 
 
e1ccef5
0f29326
e1ccef5
 
 
0f29326
e1ccef5
 
0f29326
 
e1ccef5
0f29326
e1ccef5
 
0f29326
 
e1ccef5
0f29326
e1ccef5
 
0f29326
 
e1ccef5
0f29326
 
e1ccef5
0f29326
 
e1ccef5
0f29326
e1ccef5
 
0f29326
 
e1ccef5
0f29326
e1ccef5
 
0f29326
 
e1ccef5
0f29326
e1ccef5
 
0f29326
 
e1ccef5
0f29326
e1ccef5
 
0f29326
 
 
 
 
e1ccef5
0f29326
e1ccef5
 
0f29326
 
 
 
 
 
 
 
 
e1ccef5
0f29326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da082ac
0f29326
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# utils/timefeatures.py (최종 수정 버전)

import numpy as np
import pandas as pd
from typing import List

# -------------------------------------------------------------------------
# 이 파일의 모든 코드를 아래 내용으로 교체하면 됩니다.
# -------------------------------------------------------------------------

class BaseTimeFeature:
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        pass

class SecondOfMinute(BaseTimeFeature):
    """Minute of hour encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.second -> index.dt.second
        return index.dt.second / 59.0 - 0.5

class MinuteOfHour(BaseTimeFeature):
    """Minute of hour encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.minute -> index.dt.minute
        return index.dt.minute / 59.0 - 0.5

class HourOfDay(BaseTimeFeature):
    """Hour of day encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.hour -> index.dt.hour
        return index.dt.hour / 23.0 - 0.5

class DayOfWeek(BaseTimeFeature):
    """Day of week encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.dayofweek -> index.dt.dayofweek
        return index.dt.dayofweek / 6.0 - 0.5

class DayOfMonth(BaseTimeFeature):
    """Day of month encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.day -> index.dt.day
        return (index.dt.day - 1) / 30.0 - 0.5

class DayOfYear(BaseTimeFeature):
    """Day of year encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.dayofyear -> index.dt.dayofyear
        return (index.dt.dayofyear - 1) / 365.0 - 0.5

class MonthOfYear(BaseTimeFeature):
    """Month of year encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.month -> index.dt.month
        return (index.dt.month - 1) / 11.0 - 0.5

class WeekOfYear(BaseTimeFeature):
    """Week of year encoded as value between [-0.5, 0.5]"""
    def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:
        # ⭐️ 수정: index.isocalendar().week -> index.dt.isocalendar().week
        # .astype(float) 추가
        return (index.dt.isocalendar().week.astype(float) - 1) / 52.0 - 0.5
        
def time_features_from_frequency_str(freq_str: str) -> List[BaseTimeFeature]:
    """
    Returns a list of time features that will be used for a given frequency string.
    """
    features_by_offsets = {
        "Y": ["year"],
        "M": ["month", "year"],
        "W": ["day", "week", "month", "year"],
        "D": ["day", "week", "month", "year"],
        "B": ["day", "week", "month", "year"],
        "H": ["hour", "day", "week", "month", "year"],
        "T": ["minute", "hour", "day", "week", "month", "year"],
        "min": ["minute", "hour", "day", "week", "month", "year"],
        "S": ["second", "minute", "hour", "day", "week", "month", "year"],
    }
    offset = freq_str.split("-")[-1]
    for name, feats in features_by_offsets.items():
        if offset.startswith(name):
            return [
                cls()
                for cls in FEATURES_MAP.values()
                if cls.name in feats
            ]

FEATURES_MAP = {
    "year": "Year",
    "month": MonthOfYear,
    "week": WeekOfYear,
    "day": DayOfMonth,
    "dayofweek": DayOfWeek,
    "dayofyear": DayOfYear,
    "hour": HourOfDay,
    "minute": MinuteOfHour,
    "second": SecondOfMinute,
}

def time_features(dates, freq="T"):
    # ⭐️ 이 함수 내부 로직을 pandas 최신 버전에 맞게 수정했습니다.
    if isinstance(dates, pd.DataFrame):
        dates = pd.to_datetime(dates.iloc[:, 0])

    return np.vstack(
        [feat(dates) for feat in time_features_from_frequency_str(freq)]
    ).transpose(1, 0)