Neural Network to Predict Bike Sharing Rides

Anna Scott
Analytics Vidhya
Published in
4 min readDec 15, 2019

--

In this article we will discuss building a neural network from scratch to make predictions using a real dataset. This approach will help us to develop a better understanding of neural network concepts. The dataset could be found here:

https://archive.ics.uci.edu/ml/datasets/Bike+Sharing+Dataset

We are going to create two files: Jupyter Notebook and my_answers.py that will actually contain our neural network and hyperparameters.

1.1 We start working with Jupiter Notebook Predicting_bike_sharing_data. We perform all the necessary imports:

The line %matplotlib inline puts matplotlib graphs next to the code.

Next two lines:

%load_ext autoreload
%autoreload 2

will reload all changed modules every time before executing a new line.

The purpose of %config InlineBackend.figure_format = ‘retina’ is to increase resolution of our plots.

import numpy as np is import of fundamental scientific computing with Python package.

import pandas as pd is import of Python library for data manipulation and analysis.

import matplotlib.pyplot as plt imports the module “matplotlib.pyplot”, pyplot is matplotlib’s plotting framework.

1.2 We read the dataset with rides = pd.read_csv(data_path) and look at the top 5 rows of data frame with rides.head()

1.3 Let’s look at a plot showing the number of bike riders over the first 10 days or so in the data set. It is not precisely 24 entries a day. The weekends have lower over all ridership and there are spikes when people are biking to and from work during the week.

1.4 We create binary dummy variables with with Pandas get_dummies()

1.5 We standardize each of the continuous variables by shifting and scaling the variables such that they have zero mean and a standard deviation of 1.

1.6 We split the data into training, testing, and validation sets.

2.1 Now we are going to build our network in my_answers.py file. In our class NeuralNetwork(object) we define constructor method __init__. We set the number of nodes in input, hidden and output layers, initialize weights, and set self.activation_function to sigmoid function.

2.2 We implement forward pass in forward_pass_train, calculating the outputs for each neuron as we work through the layers of our network. We use the initialised weights to propagate signals forward from the input to the output layers in a neural network. The output layer has only one node, and it is used for the regression, so the output of the node and the input of the node are the same.

2.3 We implement backpropagation in backpropagation. We use the weights to also propagate error backwards from the output back into the network to update our weights.

2.4 We update weights on gradient descent step in update_weights.

2.5 We Implement the forward pass in the run method.

2.6 We define train for training neural network.

3.1 We return to Jupiter Notebook Predicting_bike_sharing_data where we import our neural network.

3.2 We run these unit tests to check the correctness of our network implementation.

4.1 We are going to train our neural network. We set the hyperparameters in you myanswers.py file

The things to consider when we set our hyperparameters

4.2 We import them into our notebook

4.3 We train our network.

4.4 Finally we check our predictions

You can find the complete code here:

Happy coding my friends!

--

--