1
$\begingroup$

let's suppose we have a customer data from the year 2015 to 2019, I want to train_test_split() my data such that my data gets divided into three sets, set-1 is from 2015 to 2017 (3 years) on which i will train my model, set-2 i.e. 2018(1 year) on which i will validate my model , set 3 is 2019(1 year) on which I will test my model. I want a code to divide data into 3 sets based on time(years).

$\endgroup$
4
  • 1
    $\begingroup$ Welcome to DS StackExchange. Please elaborate more on the question. What are you asking exactly? As of now, it's not possible to help you. $\endgroup$
    – Leevo
    Jan 16, 2020 at 15:36
  • 1
    $\begingroup$ Hello, let's suppose we have a customer data from the year 2015 to 2019, I want to train_test_split() my data such that my data gets divided into three sets, set-1 is from 2015 to 2017 (3 years) on which i will train my model, set-2 i.e. 2018(1 year) on which i will validate my model , set 3 is 2019(1 year) on which I will test my model. I want a code to divide data into 3 sets based on time(years). $\endgroup$
    – karan
    Jan 16, 2020 at 16:21
  • $\begingroup$ Thank you! Please update the main question with this information $\endgroup$
    – Leevo
    Jan 16, 2020 at 16:24
  • $\begingroup$ Do you realize that this is a terrible idea from a machine learning perspective? The year is very likely a significant factor and you are removing 2 years completely from the learning process. $\endgroup$
    – Pieter21
    Jan 16, 2020 at 21:10

1 Answer 1

0
$\begingroup$

Seems to me the best (or at least quickest) way to do this would be have all the data in a Pandas dataframe, then create masks based on year and create new dataframes for each group. Ex:

train_df = data[data['year'].isin(['2015', '2016', '2017'])
validate_df = data[data['year'] == '2018']
test_df = data[data['year'] == '2019']

Hope this is what you're looking for. If not, let me know and we can work out another solution.

$\endgroup$
1
  • 1
    $\begingroup$ Yes this is the simplest and the quickest way , thanks for the quick help. $\endgroup$
    – karan
    Jan 17, 2020 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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