#!/usr/bin/env python # coding: utf-8 # # Exercise 04 # # Estimate a regression using the Capital Bikeshare data # # # ## Forecast use of a city bikeshare system # # We'll be working with a dataset from Capital Bikeshare that was used in a Kaggle competition ([data dictionary](https://www.kaggle.com/c/bike-sharing-demand/data)). # # Get started on this competition through Kaggle Scripts # # Bike sharing systems are a means of renting bicycles where the process of obtaining membership, rental, and bike return is automated via a network of kiosk locations throughout a city. Using these systems, people are able rent a bike from a one location and return it to a different place on an as-needed basis. Currently, there are over 500 bike-sharing programs around the world. # # The data generated by these systems makes them attractive for researchers because the duration of travel, departure location, arrival location, and time elapsed is explicitly recorded. Bike sharing systems therefore function as a sensor network, which can be used for studying mobility in a city. In this competition, participants are asked to combine historical usage patterns with weather data in order to forecast bike rental demand in the Capital Bikeshare program in Washington, D.C. # In[1]: import pandas as pd import numpy as np get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt # read the data and set the datetime as the index import zipfile with zipfile.ZipFile('../datasets/bikeshare.csv.zip', 'r') as z: f = z.open('bikeshare.csv') bikes = pd.read_csv(f, index_col='datetime', parse_dates=True) # "count" is a method, so it's best to name that column something else bikes.rename(columns={'count':'total'}, inplace=True) bikes.head() # * datetime - hourly date + timestamp # * season - # * 1 = spring # * 2 = summer # * 3 = fall # * 4 = winter # * holiday - whether the day is considered a holiday # * workingday - whether the day is neither a weekend nor holiday # * weather - # * 1: Clear, Few clouds, Partly cloudy, Partly cloudy # * 2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist # * 3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds # * 4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog # * temp - temperature in Celsius # * atemp - "feels like" temperature in Celsius # * humidity - relative humidity # * windspeed - wind speed # * casual - number of non-registered user rentals initiated # * registered - number of registered user rentals initiated # * total - number of total rentals # In[2]: bikes.shape # # Exercise 4.1 # # What is the relation between the temperature and total? # # For a one percent increase in temperature how much the bikes shares increases? # # Using sklearn estimate a linear regression and predict the total bikes share when the temperature is 31 degrees # In[3]: # Pandas scatter plot bikes.plot(kind='scatter', x='temp', y='total', alpha=0.2) # In[ ]: # # Exercise 04.2 # # Evaluate the model using the MSE # In[ ]: # # Exercise 04.3 # # Does the scale of the features matter? # # Let's say that temperature was measured in Fahrenheit, rather than Celsius. How would that affect the model? # In[ ]: # # # Exercise 04.4 # # Run a regression model using as features the temperature and temperature$^2$ using the OLS equations # In[ ]: # # Exercise 04.5 # # # Estimate a regression using more features ['temp', 'season', 'weather', 'humidity']. # # How is the performance compared to using only the temperature? # In[ ]: # # Exercise 04.6 # # Split the data in train and test # # Which of the following models is the best in the testing set? # * ['temp', 'season', 'weather', 'humidity'] # * ['temp', 'season', 'weather'] # * ['temp', 'season', 'humidity'] # # In[ ]: