Start here · About 45 minutes
Predict bike demand
A bike rental shop wants to know how many bikes to have ready each day. With too few, customers leave. With too many, bikes sit unused. You will predict daily rentals from the weather and the day of the week.
- The data
- 360 days of simulated data: temperature, rain, weekend, and the number of bikes rented.
- Before you start
- You need basic Python: variables, functions and lists. The only math is averages.
Open the notebook
The notebook has the data and an empty cell for each step. Keep it open next to this page.
Your own computer
Download the notebook, then open it with Jupyter.
Download notebook ↓uvx --with scikit-learn --with pandas --with matplotlib --from jupyter jupyter lab predict-demand.ipynbStep 1
Keep some days for testing
Practice firstWrite Train Test Split yourselfEasyA model can memorize the days it learned from. To find out how it does on new days, put some days aside before you train anything, and use them only at the end.
A common split is 80% for training and 20% for testing. Shuffle the rows first, so the test days do not all come from one part of the year.
Need more background? Read the lesson →In the notebook
Separate the features (temperature, rain, weekend) from the target (rentals). Hold out 20% of the rows as a test set.
Step 2
Beat a simple guess
Practice firstWrite Linear Regression by Normal Equation yourselfEasyBefore you build a model, find out how well a lazy guess does. Here the lazy guess is the average number of rentals in the training data, used for every day. A model is only useful if it beats this.
Linear regression gives each feature a weight. For example, each extra degree adds some rentals and rain removes some. It picks the weights that make the squared errors on the training data as small as possible.
Need more background? Read the lesson →In the notebook
Predict the training mean for every test day. Then fit a linear regression on the training data and predict the test days.
Step 3
Measure the errors
Practice firstWrite R Squared yourselfEasyMean absolute error (MAE) is the average size of a miss, counted in bikes. It is easy to explain to the shop owner.
R² is the share of the day-to-day change that the model explains. 0 means no better than the average, and 1 means perfect. Also look at the residuals (actual minus predicted). If they grow on hot days, the model misses something.
Need more background? Read the lesson →In the notebook
Report MAE for the baseline and for the model. Plot predicted rentals against actual rentals.
Check your work
Answer these in the last cell of the notebook.
- Does your model beat the average on the test days?
- Which feature changes the prediction most?
- Why can simulated data not tell you how the model would do at a real shop?