Forecasting Demand for Bike Sharing System with Python — Part 1
By: Cheer Hung
Tags: Supervised Learning, Predictions, Data Science, Data Visualization, Predictive Analytics

Prediction Machine Learning

“Prediction” refers to the output generated by an algorithm which has been trained on historical data and applied to unseen data. In this project it can be referred to the number of bikes rented out in the next quarter based on previous rental behavior by the customers.

Objective

Bike sharing programs are becoming more and more popular around the world due to environmental issues, pricing and convenience. In an attempt by the city or government to control and understand mobility flows in the city of Washington, bicycle sharing system data can be used as an approximation to explain the commuting of people around the city. Bike sharing has become a trend all around the world.

Therefore, we decide to use the Bike Sharing dataset provided by UCI. The main goal of this project is to predict the hourly number of bikes rented during the last quarter of the year.

For this purpose, two datasets with 17 variables were provided with data between the first of January of 2011 until the last day of 2012. One dataset had hourly information while the other one contained daily information regarding the bike sharing system business.

Let’s Do it!

We start importing two datasets first by using function pd.read_csv then we put the column instant as the index.

# Importing the data

day = pd.read_csv(~, index_col=”instant”, parse_dates=True)

hour = pd.read_csv(~, index_col=”instant”, parse_dates=True) # Checking the head of the hourly and daily dataset

day.head()

hour.head()

Then we use the function .head() to check the head of the dataset. We are going to focus on the hourly dataset as the target is to make hourly predictions. However, we will also look at the daily dataset to explore any possible differences.

Data Preparation

Different analyzes will be performed to understand the data we are dealing with. This analyzes include some of the following:

Checking missing values and coherence

Checking variable types

Analyzing distribution of the variables

Check Missing Values

# Checking missing values for the dataset

day.isnull().any()

hour.isnull().any()

We use function .isnull().any() to check if there is any missing value, as we can see there are no missing values in both csv. files. Then, we check the amount of the entries by using .shape.

Here is an easy calculation, 731 days in day dataset time 24 hour a day. We expect that hour dataset should have 17544 entries (1 per hour). However, we only have 17379 entries. What about the remaining 165 hours? We need to check the consistency of the dataset.

To visualise the inconsistency in the dataset. The x axis of this graph is “date” and the y axis is “Number of hours” there is an entry for, which implies that if the value is 24 then there is a record in the dataset for the whole day(24 hours 1 entry per hour) and where ever there is a valley there is some anomaly which caused the business to be inoperable for those many hours. However, if there was a regular pattern it could be interpreted as a routine maintenance of the bike but as there is no pattern which is witnessed implies that there is some event which occurred on that day in Washington which caused the business to be inoperable.

We witness a significant dip in October 2012. So, we trace the historical resources, we found out that there is a day when the business was not operating for 23 hours. Upon further research, we found out that Washington DC was hit by a hurricane on October 29, 2012. So it can be inferred that these missing hours are due to some underlying cause and hence cannot be imputed.

Convert Numerical Variables into Categories

# check the datatype

hour.info()

By looking at the above results we can see that some of the columns are interpreted as integers even though they are actually better interpreted as categories. Here, we use the pandas library pd.to_datetime to convert the dteday to the right datatype. Then use the function .astype(“category”) convert the numerical variables to categorical.

Then we use the function .info() again to verify that all the variables have been changed. All the variables have been correctly changed according to the result which is shown below.

Data Visualisation

Data visualisation is an important process for us to get the customer behaviour insights. Here, we use libraries seaborn to generate the graphs.