Package: time-robust-forest
time-robust-forestA Proof of concept model that explores timestamp information to train a random forest with better Out of Distribution generalization power.Installationpipinstall-Utime-robust-forestHow to use itThere are a classifier and a regressor undertime_robust_forest.models. They follow the sklearn interface, which means you can quickly fit and use a model:fromtime_robust_forest.modelsimportTimeForestClassifierfeatures=["x_1","x_2"]time_column="periods"target="y"model=TimeForestClassifier(time_column=time_column)model.fit(training_data[features+[time_column]],training_data[target])predictions=model.predict_proba(test_data[features])[:,1]There are only a few arguments that differ from a traditional Random Forest. two argumentstime_column: the column from the input dataframe containing the time
periods the model will iterate over to find the best splits (default: "period")min_sample_periods: the number of examples in every period the model needs
to keep while it splits.period_criterion: how the performance in every period is going to be
aggregated. Options: {"avg": average, "max": maximum, the worst case}.
(default: "avg")To use the environment-wise optimization:fromtime_robust_forest.hyper_optimportenv_wise_hyper_optparams_grid={"n_estimators":[30,60,120],"max_depth":[5,10],"min_impurity_decrease":[1e-1,1e-3,0],"min_sample_periods":[5,10,30],"period_criterion":["max","avg"]}model=TimeForestClassifier(time_column=time_column)opt_param=env_wise_hyper_opt(training_data[features+[time_column]],training_data[TARGET],model,time_column,params_grid,cv=5,scorer=make_scorer(roc_auc_score,needs_proba=True))Make sure you have a good choice for the time columnDon't simply use a timestamp column from the dataset, make it discrete before and guarantee there is a reasonable amount of data points in every period. Example: use year if you have 3+ years of data. Notice the choice to make it discrete becomes a modeling choice you can optimize.Random segmentsSelecting randomly from multiple time columnsThe user can use a list instead of a string as thetime_columnargument. The model will select randomly from it when building every estimator from the definedn_estimators.fromtime_robust_forest.modelsimportTimeForestClassifierfeatures=["x_1","x_2"]time_columns=["periods","periods_2"]target="y"model=TimeForestClassifier(time_column=time_columns)model.fit(training_data[features+time_columns],training_data[target])predictions=model.predict_proba(test_data[features])[:,1]Generating random segments from a timestamp columnThe user can define a maximum number of segments (random_segments) and the model will split the data using the time stamp information. In the following example, the model segments the data in 1, 2, 3... 10 parts. For every estimator, it picks randomly one of the ten columns representing thetime_columnand use it. In this case, thetime_columnshould be the time stamp information.fromtime_robust_forest.modelsimportTimeForestClassifierfeatures=["x_1","x_2"]time_column="time_stamp"target="y"model=TimeForestClassifier(time_column=time_column,random_segments=10)model.fit(training_data[features+[time_column]],training_data[target])predictions=model.predict_proba(test_data[features])[:,1]LicenseThis project is licensed under the terms of theBSD-3license. SeeLICENSEfor more details.Useful linksIntroducing the Time Robust Tree blog postCitation@misc{time-robust-forest,
  author = {Moneda, Luis},
  title = {Time Robust Forest model},
  year = {2021},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/lgmoneda/time-robust-forest}}
}