mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-22 23:38:08 +00:00
fix(Data): one-hot encode date features
This commit is contained in:
+10
-3
@@ -5,9 +5,14 @@ import numpy as np
|
|||||||
from pandas.core.frame import DataFrame
|
from pandas.core.frame import DataFrame
|
||||||
from utils.technical_indicators import ROC, RSI, STOK, STOD
|
from utils.technical_indicators import ROC, RSI, STOK, STOD
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
from sklearn.preprocessing import OneHotEncoder
|
||||||
|
|
||||||
#%%
|
#%%
|
||||||
|
|
||||||
|
def get_all_assets(path: str) -> list[str]:
|
||||||
|
return [f.split('.')[0] for f in os.listdir(path) if os.path.isfile(os.path.join(path,f)) and not f.startswith('.')]
|
||||||
|
|
||||||
|
|
||||||
def load_data(path: str,
|
def load_data(path: str,
|
||||||
target_asset: str,
|
target_asset: str,
|
||||||
target_asset_lags: list[int],
|
target_asset_lags: list[int],
|
||||||
@@ -42,9 +47,9 @@ def load_data(path: str,
|
|||||||
dfs.index = pd.DatetimeIndex(dfs.index)
|
dfs.index = pd.DatetimeIndex(dfs.index)
|
||||||
|
|
||||||
if add_date_features:
|
if add_date_features:
|
||||||
dfs['day_month'] = dfs.index.day
|
dfs = pd.concat([dfs, pd.get_dummies(dfs.index.day, drop_first=True, prefix="day_month").set_index(dfs.index)], axis=1)
|
||||||
dfs['day_week'] = dfs.index.dayofweek
|
dfs = pd.concat([dfs, pd.get_dummies(dfs.index.dayofweek, drop_first=True, prefix="day_week").set_index(dfs.index)] , axis=1)
|
||||||
dfs['month'] = dfs.index.month
|
dfs = pd.concat([dfs, pd.get_dummies(dfs.index.month, drop_first=True, prefix="month").set_index(dfs.index)], axis = 1)
|
||||||
|
|
||||||
if index_column == 'int':
|
if index_column == 'int':
|
||||||
dfs.reset_index(drop=True, inplace=True)
|
dfs.reset_index(drop=True, inplace=True)
|
||||||
@@ -129,6 +134,8 @@ def __augment_derived_features(df: pd.DataFrame, log_returns: bool, technical_fe
|
|||||||
return df
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
def __create_target_cum_forward_returns(df: pd.DataFrame, source_column: str, period: int) -> pd.DataFrame:
|
def __create_target_cum_forward_returns(df: pd.DataFrame, source_column: str, period: int) -> pd.DataFrame:
|
||||||
df['target'] = df[source_column].diff(period).shift(-period)
|
df['target'] = df[source_column].diff(period).shift(-period)
|
||||||
|
|||||||
Reference in New Issue
Block a user