Answers.Body: <p>As a rule of thumb, you not just should but you must shuffle your training instances always. You just need to pay attention to not be fooled in regard to what a training instance is in your specific use case.</p>
  <p>In the most classic situation you'll exactly what you did (which is right) and that's baffling you: given n training instances with n features you just randomly change the order of each training instance. So this dummy example</p>
  <p><kbd><code>          </code></kbd><kbd><strong><code>Feature 1 </code></strong></kbd><kbd><strong><code>Feature 2 </code></strong></kbd><br/>
  <kbd><code>Instance 1</code></kbd><kbd><code>  F1,1    </code></kbd><kbd><code>  F1,2    </code></kbd><br>
  <kbd><code>Instance 2</code></kbd><kbd><code>  F2,1    </code></kbd><kbd><code>  F2,2    </code></kbd><br>
  <kbd><code>Instance 3</code></kbd><kbd><code>  F3,1    </code></kbd><kbd><code>  F3,2    </code></kbd><br><br></p>
  <p>might turn into this</p>
  <p><kbd><code>          </code></kbd><kbd><strong><code>Feature 1 </code></strong></kbd><kbd><strong><code>Feature 2 </code></strong></kbd><br/>
  <kbd><code>Instance 3</code></kbd><kbd><code>  F3,1    </code></kbd><kbd><code>  F3,2    </code></kbd><br>
  <kbd><code>Instance 2</code></kbd><kbd><code>  F2,1    </code></kbd><kbd><code>  F2,2    </code></kbd><br>
  <kbd><code>Instance 1</code></kbd><kbd><code>  F1,1    </code></kbd><kbd><code>  F1,2    </code></kbd><br><br></p>
  <p>There's only one situation in which you need to pay attention, which is when you're dealing with time series. A time series is a sequence of repeated measurements, and we use them to train predictive models when we care about learning the temporal dependencies between events and their features.</p>
  <p>In practice, if you measure the same event <span class="math-container">$n$</span> times gathering <span class="math-container">$m$</span> features you'll have <span class="math-container">$n*m$</span> element that compose the time serie. Note that the different time stamps in this case do not represent separate training instances, rather different time series are separate training instances. For example, I can record several weather features at different hours for a specific city, and that would give me a single time serie, by repeating the process for several cities I would gather different training instances. Once you understand the difference is pretty easy to see that you can still randomize your training instances, the only thing you can't randomize, for obvious reasons, is the order of the time stamps.</p>
  <p><kbd><code>          </code></kbd><kbd><strong><code>Time step 1      </code></strong></kbd><kbd><strong><code>Time step 2      </code></strong></kbd><br/>
  <kbd><code>          </code></kbd><kbd><strong><code>Feat 1 </code></strong></kbd><kbd><strong><code>Feat 2 </code></strong></kbd><kbd><strong><code>Feat 1 </code></strong></kbd><kbd><strong><code>Feat 2 </code></strong></kbd><br/>
  <kbd><code>Instance 1</code></kbd><kbd><code> F1,1  </code></kbd><kbd><code> F1,2  </code></kbd><kbd><code> F1,1  </code></kbd><kbd><code> F1,2  </code></kbd><br>
  <kbd><code>Instance 2</code></kbd><kbd><code> F2,1  </code></kbd><kbd><code> F2,2  </code></kbd><kbd><code> F2,1  </code></kbd><kbd><code> F2,2  </code></kbd><br>
  <kbd><code>Instance 3</code></kbd><kbd><code> F3,1  </code></kbd><kbd><code> F3,2  </code></kbd><kbd><code> F3,1  </code></kbd><kbd><code> F3,2  </code></kbd><br></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-23T07:25:28.397Z
Answers.Text: Thanks for the elaborate answer! So from the description of the problem, I understand I need to shuffle the data, however, to test the hypothesis, I left the last 3 months of data as test data. The rest was shuffled and used as training. I got bad performance on test. Some of the things I see is that vendors change, or type of food ordered. Perhaps I need to re-train every N test samples (using the last M)? Is there a good way of detecting a drift/change in data distribution? (I can try and build a model per vendor e.g. but that seems too much to handle...)
Answers.UserDisplayName: John Coles
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-19T16:01:08.867Z
Answers.LastActivityDate: 2022-07-19T16:01:08.867Z
Answers.UserDisplayName: Edoardo Guerriero
Answers.Body: <p>Yes, shuffling the dataset is in fact, an important step in the process. Shuffling help in reducing overfitting to a particular pattern and generates more 'randomness' to the data, hence helping the model get used to variations and be more generalised.</p>
  <p>Hence, you will want to shuffle to make sure that your training/test/validation sets are representative of the overall distribution of the data.</p>
  <p><a href="https://datascience.stackexchange.com/questions/24511/why-should-the-data-be-shuffled-for-machine-learning-tasks#:%7E:text=Shuffling%20data%20serves%20the%20purpose,sorted%20by%20their%20class%2Ftarget.">This</a> link will give you more in-depth explanantions if you require them.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-23T07:30:45.770Z
Answers.Text: Thank you for the answer! As I wrote above to @Edoardo, when I test the model on a new chunk of data (even with shuffling) the performance is bad. I suspect it's because I have data for multiple vendors, types of food, etc. Is there a way of dealing with this issue? Some data drift detection, or continuous re-training?
Answers.UserDisplayName: John Coles
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-19T15:36:48.283Z
Answers.LastActivityDate: 2022-07-19T15:36:48.283Z
Answers.UserDisplayName: Yash Laddha
Body: <p>I'm new to ML and trying to write a solution to a food delivery duration time problem (so called lead time). I used algorithms such as random forest and gradient boosting which gave OK results but not amazing. I have daily data covering 3 years, that I split to train/test with an 80/20% split.
  To try and improve results, I repeated the split however I did it randomly, i.e. on a shuffled data. My model, does not use any relations between data points, it only predicts the lead time using that specific sample's features (e.g. type of food, who's the carrier, expected delivery duration, etc.)
  When I do this, the results improve dramatically, which makes me question I'm doing something illegal.
  I wanted to know if I can actually shuffle the data? Can I do that?</p>
  
Tags: <machine-learning><python><data-preprocessing><data-augmentation>
Title: Can I shuffle data for delivery duration forecast problem?
UserDisplayName: John Coles
ThreadText: # Can I shuffle data for delivery duration forecast problem?
  
  (Asked by: username_0 on 2022-07-19)
  
  I'm new to ML and trying to write a solution to a food delivery duration time problem (so called lead time). I used algorithms such as random forest and gradient boosting which gave OK results but not amazing. I have daily data covering 3 years, that I split to train/test with an 80/20% split.
  To try and improve results, I repeated the split however I did it randomly, i.e. on a shuffled data. My model, does not use any relations between data points, it only predicts the lead time using that specific sample's features (e.g. type of food, who's the carrier, expected delivery duration, etc.)
  When I do this, the results improve dramatically, which makes me question I'm doing something illegal.
  I wanted to know if I can actually shuffle the data? Can I do that?
  
  **Tags:** machine-learning, python, data-preprocessing, data-augmentation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-07-19. Score: 1
  
  As a rule of thumb, you not just should but you must shuffle your training instances always. You just need to pay attention to not be fooled in regard to what a training instance is in your specific use case.
  
  In the most classic situation you'll exactly what you did (which is right) and that's baffling you: given n training instances with n features you just randomly change the order of each training instance. So this dummy example
  
  `Feature 1` `Feature 2`   
  `Instance 1` `F1,1`  `F1,2`   
  `Instance 2` `F2,1`  `F2,2`   
  `Instance 3` `F3,1`  `F3,2`
  
  might turn into this
  
  `Feature 1` `Feature 2`   
  `Instance 3` `F3,1`  `F3,2`   
  `Instance 2` `F2,1`  `F2,2`   
  `Instance 1` `F1,1`  `F1,2`
  
  There's only one situation in which you need to pay attention, which is when you're dealing with time series. A time series is a sequence of repeated measurements, and we use them to train predictive models when we care about learning the temporal dependencies between events and their features.
  
  In practice, if you measure the same event $n$ times gathering $m$ features you'll have $n\*m$ element that compose the time serie. Note that the different time stamps in this case do not represent separate training instances, rather different time series are separate training instances. For example, I can record several weather features at different hours for a specific city, and that would give me a single time serie, by repeating the process for several cities I would gather different training instances. Once you understand the difference is pretty easy to see that you can still randomize your training instances, the only thing you can't randomize, for obvious reasons, is the order of the time stamps.
  
  `Time step 1` `Time step 2`   
  `Feat 1` `Feat 2` `Feat 1` `Feat 2`   
  `Instance 1` `F1,1`  `F1,2`  `F1,1`  `F1,2`   
  `Instance 2` `F2,1`  `F2,2`  `F2,1`  `F2,2`   
  `Instance 3` `F3,1`  `F3,2`  `F3,1`  `F3,2`
  
  > **Comments:**
  > **username_0**: Thanks for the elaborate answer! So from the description of the problem, I understand I need to shuffle the data, however, to test the hypothesis, I left the last 3 months of data as test data. The rest was shuffled and used as training. I got bad performance on test. Some of the things I see is that vendors change, or type of food ordered. Perhaps I need to re-train every N test samples (using the last M)? Is there a good way of detecting a drift/change in data distribution? (I can try and build a model per vendor e.g. but that seems too much to handle...)  
  
  ---
  
  ### Answer by username_2 on 2022-07-19. Score: 1
  
  Yes, shuffling the dataset is in fact, an important step in the process. Shuffling help in reducing overfitting to a particular pattern and generates more 'randomness' to the data, hence helping the model get used to variations and be more generalised.
  
  Hence, you will want to shuffle to make sure that your training/test/validation sets are representative of the overall distribution of the data.
  
  [This](https://datascience.stackexchange.com/questions/24511/why-should-the-data-be-shuffled-for-machine-learning-tasks#:%7E:text=Shuffling%20data%20serves%20the%20purpose,sorted%20by%20their%20class%2Ftarget.) link will give you more in-depth explanantions if you require them.
  
  > **Comments:**
  > **username_0**: Thank you for the answer! As I wrote above to @username_1, when I test the model on a new chunk of data (even with shuffling) the performance is bad. I suspect it's because I have data for multiple vendors, types of food, etc. Is there a way of dealing with this issue? Some data drift detection, or continuous re-training?  
  
  ---
  
  