<jupyter_start><jupyter_text>For convenience, we'll further process this dataset by shortening the column names and adding a "Total" column:<jupyter_code>data.columns = ['East', 'West']
data['Total'] = data.eval('West + East')<jupyter_output><empty_output><jupyter_text>Now let's take a look at the summary statistics for this data:<jupyter_code>data.dropna().describe()<jupyter_output><empty_output><jupyter_text>Visualizing the dataWe can gain some insight into the dataset by visualizing it.Let's start by plotting the raw data:<jupyter_code>%matplotlib inline
import seaborn 
seaborn.set()
data.plot()
plt.ylabel('Hourly Bicycle Count');<jupyter_output><empty_output><jupyter_text>The ~25,000 hourly samples are far too dense for us to make much sense of.We can gain more insight by resampling the data to a coarser grid.Let's resample by week:<jupyter_code>weekly = data.resample('W').sum()
weekly.head()
weekly.plot(style=['.-', '--', '-'])
plt.ylabel('Weekly bicycle count');<jupyter_output><empty_output><jupyter_text>This shows us some interesting seasonal trends: as you might expect, people bicycle more in the summer than in the winter, and even within a particular season the bicycle use varies from week to week (likely dependent on weather; see [In Depth: Linear Regression](05.06-Linear-Regression.ipynb) where we explore this further).Another way that comes in handy for aggregating the data is to use a rolling mean, utilizing the ``pd.rolling_mean()`` function.Here we'll do a 30 day rolling mean of our data, making sure to center the window:<jupyter_code>daily = data.resample('D').sum()
daily.head()
daily.plot(style=['.-', '--', '-'])
plt.ylabel('Weekly bicycle count');
daily.rolling(120, center=True).std().plot(style=[':', '--', '-']) # Mena + Std
plt.ylabel('mean hourly count');
daily.rolling(120, center=True).mean().plot(style=[':', '--', '-']) # Mena + Std
plt.ylabel('mean hourly count');<jupyter_output><empty_output><jupyter_text>The jaggedness of the result is due to the hard cutoff of the window.We can get a smoother version of a rolling mean using a window function–for example, a Gaussian window.The following code specifies both the width of the window (we chose 50 days) and the width of the Gaussian within the window (we chose 10 days):<jupyter_code>daily.rolling(50, center=True,win_type='gaussian').sum(std=10).plot(style=[':', '--', '-']);
daily.rolling(150, center=True,win_type='gaussian').sum(std=50).plot(style=[':', '--', '-']);<jupyter_output><empty_output><jupyter_text>Digging into the dataWhile these smoothed data views are useful to get an idea of the general trend in the data, they hide much of the interesting structure.For example, we might want to look at the average traffic as a function of the time of day.We can do this using the GroupBy functionality discussed in [Aggregation and Grouping](03.08-Aggregation-and-Grouping.ipynb):<jupyter_code>by_time = data.groupby(data.index.time).sum()
by_time.head()
plt.figure(figsize=(16,9))
hourly_ticks = 4 * 60 * 60 *  np.arange(6)
by_time.plot(xticks=hourly_ticks, style=[':', '--', '-']);<jupyter_output><empty_output><jupyter_text>The hourly traffic is a strongly bimodal distribution, with peaks around 8:00 in the morning and 5:00 in the evening.This is likely evidence of a strong component of commuter traffic crossing the bridge.This is further evidenced by the differences between the western sidewalk (generally used going toward downtown Seattle), which peaks more strongly in the morning, and the eastern sidewalk (generally used going away from downtown Seattle), which peaks more strongly in the evening.We also might be curious about how things change based on the day of the week. Again, we can do this with a simple groupby:<jupyter_code>by_weekday = data.groupby(data.index.dayofweek).sum()
by_weekday.head()
by_weekday.index = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
by_weekday.plot(style=[':', '--', '-']);<jupyter_output><empty_output><jupyter_text>This shows a strong distinction between weekday and weekend totals, with around twice as many average riders crossing the bridge on Monday through Friday than on Saturday and Sunday.With this in mind, let's do a compound GroupBy and look at the hourly trend on weekdays versus weekends.We'll start by grouping by both a flag marking the weekend, and the time of day:<jupyter_code>weekend = np.where(data.index.weekday < 5, 'Weekday', 'Weekend')
weekend[0:50]
by_time = data.groupby([weekend, data.index.time]).mean()
by_time.head()
by_time.tail()<jupyter_output><empty_output><jupyter_text>Now we'll use some of the Matplotlib tools described in [Multiple Subplots](04.08-Multiple-Subplots.ipynb) to plot two panels side by side:<jupyter_code>fig, ax = plt.subplots(1, 2, figsize=(16, 8))
by_time.loc['Weekday'].plot(ax=ax[0], title='Weekdays',
                           xticks=hourly_ticks, style=[':', '--', '-'])
by_time.loc['Weekend'].plot(ax=ax[1], title='Weekends',
                           xticks=hourly_ticks, style=[':', '--', '-']);<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup