text_plain_output_1.png image_output_1.png
from sklearn.model_selection import StratifiedKFold, KFold, TimeSeriesSplit

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

nrows = None

def train_eval_kfolds(X, y, n_splits=5, random_state=None):
    assert hasattr(X, 'iloc')
    assert hasattr(y, 'iloc')
    skf = TimeSeriesSplit(n_splits=n_splits)
    for train_index, eval_index in skf.split(X, y):
        X_train, X_eval = (X.iloc[train_index], X.iloc[eval_index])
        y_train, y_eval = (y.iloc[train_index], y.iloc[eval_index])
        yield (X_train, X_eval, y_train, y_eval)

train_df = pd.read_csv('../input/tabular-playground-series-mar-2022/train.csv', index_col='row_id', nrows=nrows)

features = [c for c in train_df.columns if c not in ['congestion']]
X = train_df[features]
y = train_df['congestion']
features

class Pipeline:
    object_cols = None
    def transform(self, X):
        X2 = X.copy()
        date_series = pd.to_datetime(X['time'])
        date_accessor = date_series.dt
        for attr in ['date', 'day', 'day_of_week', 'day_of_year', 'days_in_month', 'freq', 'hour', 'is_leap_year', 'is_month_end', 'is_month_start', 'is_quarter_end', 'is_quarter_start', 'is_year_end', 'is_year_start', 'microsecond', 'minute', 'month', 'nanosecond', 'quarter', 'second', 'time', 'weekday', 'year']:
            X2[attr] = getattr(date_accessor, attr)
        X2['week'] = date_accessor.isocalendar().week.astype(int)
        X2['original_time'] = X['time']
        X2['time'] = X2['time'].astype(str)
        self.object_cols = list(X2.dtypes[X2.dtypes == object].index)
        X2[self.object_cols] = X2[self.object_cols].astype(str)
        return X2

myPipeline = Pipeline()
X_transformed = myPipeline.transform(X)

test_df = pd.read_csv('../input/tabular-playground-series-mar-2022/test.csv', index_col='row_id')

test_df_transformed = myPipeline.transform(test_df)
test_df_transformed