Wen Yao
07/20/2023, 8:30 PMdayofweek
, is it treated as numerical value or does the model know it’s categorical?José Morales
07/21/2023, 12:25 AMWen Yao
07/21/2023, 7:25 PMJosé Morales
07/21/2023, 9:50 PMfrom lightgbm import LGBMRegressor
from mlforecast import MLForecast
from mlforecast.utils import generate_daily_series
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import make_pipeline
class CategoricalEncoder(BaseEstimator, TransformerMixin):
def __init__(self, cols_to_encode):
self.cols_to_encode = cols_to_encode
def fit(self, df, y=None):
self.dtypes_ = df[self.cols_to_encode].astype('category').dtypes
return self
def transform(self, df, y=None):
return df.astype(self.dtypes_)
series = generate_daily_series(2)
pipe = make_pipeline(CategoricalEncoder('dayofweek'), LGBMRegressor(n_estimators=2))
mlf = MLForecast(models={'reg': pipe}, freq='D', date_features=['dayofweek'])
mlf.fit(series)
# the following contains the categories used by lightgbm, which match the days of the week
mlf.models_['reg'].named_steps['lgbmregressor'].booster_.pandas_categorical