%matplotlib inline
import pandas as pd
import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor, export_graphviz

# read the data and set "datetime" as the index
url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/bikeshare.csv'
bikes = pd.read_csv(url, index_col='datetime', parse_dates=True)

# "count" is a method, so it's best to rename that column
bikes.rename(columns={'count':'total'}, inplace=True)

# create "hour" as its own feature
bikes['hour'] = bikes.index.hour

bikes.head()

bikes.tail()

"""
Explanation: Exercise 7
Capital Bikeshare data
Introduction

Capital Bikeshare dataset from Kaggle: data, data dictionary
Each observation represents the bikeshare rentals initiated during a given hour of a given day
End of explanation
"""


# mean rentals for each value of "workingday"
bikes.groupby('workingday').total.mean()

# mean rentals for each value of "hour"
bikes.groupby('hour').total.mean()

"""
Explanation: hour ranges from 0 (midnight) through 23 (11pm)
workingday is either 0 (weekend or holiday) or 1 (non-holiday weekday)

Exercise 7.1
Run these two groupby statements and figure out what they tell you about the data.
End of explanation
"""


# mean rentals for each value of "hour"
bikes.groupby('hour').total.mean().plot()

"""
Explanation: Exercise 7.2
Run this plotting code, and make sure you understand the output. Then, separate this plot into two separate plots conditioned on "workingday". (In other words, one plot should display the hourly trend for "workingday=0", and the other should display the hourly trend for "workingday=1".)
End of explanation
"""


# hourly rental trend for "workingday=0"
bikes[bikes.workingday==0].groupby('hour').total.mean().plot()

# hourly rental trend for "workingday=1"
bikes[bikes.workingday==1].groupby('hour').total.mean().plot()

# combine the two plots
bikes.groupby(['hour', 'workingday']).total.mean().unstack().plot()

"""
Explanation: Plot for workingday == 0 and workingday == 1
End of explanation
"""


