# How to predict next visit date based on this data

I have a dataset shown below. Here, status is if visit has been done or not and schedule is if next_action_scheduled.

df =

visit_date |status |scheduled_visit

2018-09-13|1|0000-00-00

2018-11-13|1|0000-00-00

2019-01-13|1|0000-00-00

2019-03-13|0|0000-00-00

2019-05-13|0|0000-00-00


So consider this case like user is visiting in every 2 month.

I would like to find this pattern for all users based on the historical visits with the customer. I would like to find the next predicted visit for that customer and if we missed that visit, then I ask "Do you want to schedule a visit" Or "You missed a visit with this customer."

I'm new in Ml. I tried to use RNN for time series forecasting but I'm getting a really high error rate. Prediciton is nowhere close. What model/algorithm I should use and learn about to make a model for this? I want to create a solution like if I feed in the user_id it will check all these values and send notification within app.

Thank you

Assume x person in df table has visit_data and scheduled visit for Y person, initally calculate the difference and create that as your target variable. there after you can create a model and predict the no.of days, once you predict the no.of days add same to your visit date to get the schedule date. below is the process in code

#calculating the differnce in visit and schedule visit
df['#.of days']= df['scheduled_visit']-df['visit_date']

#now my table as below
visit_date|#serial.no|#.of days

#create Regression or ARMIA model on serial.no and #.of days

#start predicting the values for next 30 events, you will get to know the no.of days values

#finally add those values to your visit_date you will get the schedule visit date


Start with small model like ARIMA and check with results instead of deeplearning models(RNN),choose your model based the data size.

• thanks murlidhar I will start with ARIMA first and as Mark also suggested for regression. Thanks for prompt reply :) – Akshit jain Jun 21 '19 at 11:28
• Hi Muralidhar, Do you know any example which I can follow? I'm new at this. – Akshit jain Jun 25 '19 at 5:35
• don't have any example. assuming one medical representative visit a doctor on certain period that could be every 14 or 30 or 45 days. now we are trying predict the certain visit date, that should be very efffective. Mean time still check with examples. – Muralidhar A Jun 25 '19 at 6:39
• Yes I need to find a pattern in visits, so that I can recommend if he misses one! so i need to change data as days since last visit right? If you get to know of any example please refer me to it. thanks – Akshit jain Jun 25 '19 at 9:41
• Actually most of the examples I found are working on a numerical data as in with date you also have another parameter which is increasing or decreasing with the dates like sales, temperature etc. In my case I only have dates of visits for each customer and also if he completed the visit or not as a status. So I have to convert this data into a time series data first. How do I do that? – Akshit jain Jun 25 '19 at 11:13

If you plot days since the first visit vs the visit number, this ends up being a simple regression problem.

import pandas as pd

data = pd.read_csv('data.txt', '|')
data['visit_date'] = pd.to_datetime(data['visit_date'])
data['days_since_first_visit'] = (data['visit_date'] - data['visit_date'][0]).map(lambda delta: delta.days)
data = data.reset_index()

data.plot('index', 'days_since_first_visit')


I would recommend using a library like scikit-learn or statsmodels to do the regression.

• thanks for your answer. what do you mean by this: data['days_since_first_visit'] = (data['visit_date'] - data['visit_date'][0]).map(lambda delta: delta.days). – Akshit jain Jun 21 '19 at 9:55
• This creates a new column containing the delta, in days, between each visit date and first visit date – Mark Dunne Jun 21 '19 at 11:02