In this blog post series, we will use a neural network for predicting restaurant reservations. This first post will describe how we can use a neural network for predicting the number of days between the reservation and the actual visit given a number of visitors.
I joined this Kaggle competition. This competition provides you with the data. In my notebook, I just focused on the visit_datetime (the time the actual visit took place) and the reserve_datetime (the time the reservation was made) columns.
It is straightforward to examine the difference between visit_datetime and reserve_datetime using Pandas! You can do so using the following line of code:
Are you interested in learning more Python? Order our new "Mastering Pandas" course now on Data Blogger Courses for only
- Learn to visualize data using Pandas
- Learn how to load and store data effectively
- Learn advanced data operations
It is then possible to make a histogram out of it. I skip outliers of more than 30 visitors, since there are only a few datapoints available for reservations of large groups.
We will start with a simple script using Pandas for plotting the distribution of the “lag” times (where lag is defined as the number of days of visit_datetime – reserve_datetime).
This results into the following histogram:
This histogram makes sense! It is a long-tail distribution which tells us that most of the visits happen a one up to a few days after the reservation. It is very unlikely that a visit happens more than a month after a reservation was made. Is there an underlying cause for the long-term reservations (in which the lag is more than 30 days)? Let’s find out!
The dataset also provides us the number of visitors for which a reservations is made. We can now zoom in on the relationship between the “lag” of the reservation and the number of visitors:
This results into the following plot:
Sorry for the quick and dirty plot! However, it does signal some kind of relation between the number of visitors and the lag. The more visitors, the longer the time between the reservation and the actual visit. Why is the plot so wobbly for a large number of visitors? That is because there is less and less data available if the number of visitors increases. From this plot we can conclude that there probably is a relation between the number of visitors and the lag. Now it is time to figure out what this relation is!
Figure out the relation between the number of visitors and the lag. That’s all for now!
Why Neural Networks?
Neural networks are general function approximators. Now, I don’t know the exact underlying function. The best I can do is come up with an as-close-as-possible approximation for the underlying function. The disadvantage is that it is hard to know why it works if we get it to work, but at least we can give it a try.
The first thing to notice is that our values are not normalized. The number of visitors is a number and gets larger and larger. To normalize it, we simply divide it by 100, since all numbers are below 1. The same holds for the lag. Most of the lags are lower than 30. Therefore, I will divide the lag size by 30.
Notice that there are many more approaches for normalizing the data! This is just a quick normalization on the data, but feel free to use your own normalization method. My normalization process is closely related to the MinMaxScalar normalization which can be found in sklearn (scikit-learn).
With just a few lines of Python code we can create a Multi-Layer Perceptron (MLP):
And this results into the following plot:
Great! See how close the blue line is to the observed data! Our Neural Network did a great job :-).
A Neural Network is perfect for approximating unknown functions. In this blog post, we used Python to approximate the reservation lag given a number of visitors. Feel free to post any questions/comments below!
Another post about another Kaggle competition on house price prediction can be found here.
Help building the Data Blogger CommunityHelp to grow our community to spread AI and Data Science education around the globe.
Every penny counts.