I'm trying to understand something. I undertand what I want and the logic behind it, but I'm doubtful about what it is 'officially' and if it's right. I used this similar question as input, but my question doesn't assume that years are independent and this it's a classification problem.

Context: I have a dataset of purchases of a certain product of clients per month. I need the predict the sales of that product for the next month. I want to use multiple models (XGBoost, RF, etc.) with k-fold cross validation and compare them. So, each fold in k-fold will be a month such as: (Assuming that the present month is month 12 and we are looking to predict month 13):

Step 1: We train the models:

fold 1 : training [1 2 3 4], test [5]
fold 2 : training [2 3 4 5], test [6]
fold 3 : training [3 4 5 6], test [7]
fold 4 : training [4 5 6 7], test [8]
fold 5 : training [5 6 7 8], test [9]
fold 5 : training [6 7 8 9], test [10]

Step 2: We assess twice the models on out-of sample data and choose one:

hold-out month: test [11]
hold-out month: test [12]

Step 3: We implement the model

Implementing the model: predict_next_month [13]


  1. Given an algorithm, the model choosen is one of the k-fold models or some mix of those k-fold models? If it's a mix, how is the "learning" mixed? If it's one of the k-fold models, how it's chosen?
  2. I can't find any sklearn code to do this example (found this built-from-scratch code example). Should I start learning how to build models (a.k.a. write the code of the algorithm instead of using sklearn ML codes) from scratch in order to be able to customize them at my problem? (this answer shows my logic and it says "shouldn't be too hard to code up", but I don't find any good first-step to start in python, in R I found it here)
  3. If there is a trend in the time series, there is no need to remove the trend as there is no need to extrapolate (because it's a classification problem). If we would need to extrapolate (if it were a regression problem, like in this answer) we would need another alternative to tree-based models because tree-based models can't predict a value bigger or smaller than the value in their training set). Is this conclusion FULLY right?

PS: After we will predict month 14, month 15, month 16, and so on..

PS2: I'm considering training at least a full year and maximum two years, I just used four months for training in this example for simplicity.

  • $\begingroup$ Not sure I agree with your point 3, yes trees cannot extrapolate, but if there's a trend you need to extrapolate or there will always be an error. So I would tend to remove any apparent trend / seasonality (i.e. linear and sin/cos terms for example), fit the tree to the residuals and then use the prediction from the tree along with the seasonal / trend terms. $\endgroup$ Jan 12, 2022 at 3:48

1 Answer 1

  1. You don't choose a model from the folds. K fold cross validation is a means of estimating how using a model like a random forest or a boosted tree would perform on unseen data. You would perform cross validation on all models and select the one with the smallest cross validated loss/error metric.

  2. Have you seen https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html

  3. If you're doing classification, there may be a trend in the log odds of each class (essentially a trend in the probability of each class). You can try and learn it, there is no way to remove it. You will need to extrapolate that trend when using the model to make predictions. The hope is that the trend continues month to month in between trains of the model.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.