Add FastAPI backend for energy trading system
Implements FastAPI backend with ML model support for energy trading, including price prediction models and RL-based battery trading policy. Features dashboard, trading, backtest, and settings API routes with WebSocket support for real-time updates.
This commit is contained in:
35
backend/app/ml/features/time_features.py
Normal file
35
backend/app/ml/features/time_features.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def add_time_features(df: pd.DataFrame, timestamp_col: str = "timestamp") -> pd.DataFrame:
|
||||
result = df.copy()
|
||||
|
||||
if timestamp_col not in result.columns:
|
||||
return result
|
||||
|
||||
result[timestamp_col] = pd.to_datetime(result[timestamp_col])
|
||||
|
||||
result["hour"] = result[timestamp_col].dt.hour
|
||||
result["day_of_week"] = result[timestamp_col].dt.dayofweek
|
||||
result["day_of_month"] = result[timestamp_col].dt.day
|
||||
result["month"] = result[timestamp_col].dt.month
|
||||
|
||||
result["hour_sin"] = _sin_encode(result["hour"], 24)
|
||||
result["hour_cos"] = _cos_encode(result["hour"], 24)
|
||||
result["day_sin"] = _sin_encode(result["day_of_week"], 7)
|
||||
result["day_cos"] = _cos_encode(result["day_of_week"], 7)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def _sin_encode(x, period):
|
||||
import numpy as np
|
||||
return np.sin(2 * np.pi * x / period)
|
||||
|
||||
|
||||
def _cos_encode(x, period):
|
||||
import numpy as np
|
||||
return np.cos(2 * np.pi * x / period)
|
||||
|
||||
|
||||
__all__ = ["add_time_features"]
|
||||
Reference in New Issue
Block a user