<jupyter_start><jupyter_text>we might expect riders to behave differently on holidays; let's add an indicator of this as well:<jupyter_code>from pandas.tseries.holiday import USFederalHolidayCalendar
cal = USFederalHolidayCalendar()
holidays = cal.holidays('2012', '2016')
daily = daily.join(pd.Series(1, index=holidays, name='holiday'))
daily['holiday'].fillna(0, inplace=True)<jupyter_output><empty_output><jupyter_text>We also might suspect that the hours of daylight would affect how many people ride; let's use the standard astronomical calculation to add this information:<jupyter_code>def hours_of_daylight(date, axis=23.44, latitude=47.61):
    """Compute the hours of daylight for the given date"""
    days = (date - pd.datetime(2000, 12, 21)).days
    m = (1. - np.tan(np.radians(latitude))
         * np.tan(np.radians(axis) * np.cos(days * 2 * np.pi / 365.25)))
    return 24. * np.degrees(np.arccos(1 - np.clip(m, 0, 2))) / 180.

daily['daylight_hrs'] = list(map(hours_of_daylight, daily.index))
daily[['daylight_hrs']].plot()
plt.ylim(8, 17)<jupyter_output>/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:3: FutureWarning: The pandas.datetime class is deprecated and will be removed from pandas in a future version. Import from datetime instead.
  This is separate from the ipykernel package so we can avoid doing imports until<jupyter_text>We can also add the average temperature and total precipitation to the data. In addition to the inches of precipitation, let's add a flag that indicates whether a day is dry (has zero precipitation):<jupyter_code># temperatures are in 1/10 deg C; convert to C
weather['TMIN'] /= 10
weather['TMAX'] /= 10
weather['Temp (C)'] = 0.5 * (weather['TMIN'] + weather['TMAX'])

# precip is in 1/10 mm; convert to inches
weather['PRCP'] /= 254
weather['dry day'] = (weather['PRCP'] == 0).astype(int)

daily = daily.join(weather[['PRCP', 'Temp (C)', 'dry day']])
daily['annual'] = (daily.index - daily.index[0]).days / 365.<jupyter_output><empty_output><jupyter_text>displayurl:https://jakevdp.github.io/PythonDataScienceHandbook/05.06-linear-regression.html<jupyter_code><jupyter_output><empty_output><jupyter_text>Now our data is in order, and we can take a look at it:<jupyter_code>daily.head()<jupyter_output><empty_output><jupyter_text>With this in place, we can choose the columns to use, and fit a linear regression model to our data. We will set fit_intercept = False, because the daily flags essentially operate as their own day-specific intercepts:<jupyter_code>from sklearn.linear_model import LinearRegression
# Drop any rows with null values
daily.dropna(axis=0, how='any', inplace=True)

#column_names = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'holiday',
 #               'daylight_hrs', 'PRCP', 'dry day', 'Temp (C)', 'annual']
column_names = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'holiday',
                'daylight_hrs', 'annual']
X = daily[column_names]
y = daily['Total']

model = LinearRegression(fit_intercept=False)
model.fit(X, y)
daily['predicted'] = model.predict(X)
daily[['Total', 'predicted']].plot(alpha=0.5);

params = pd.Series(model.coef_, index=X.columns)
params<jupyter_output><empty_output><jupyter_text>These numbers are difficult to interpret without some measure of their uncertainty. We can compute these uncertainties quickly using bootstrap resamplings of the data:<jupyter_code>from sklearn.utils import resample
np.random.seed(1)
err = np.std([model.fit(*resample(X, y)).coef_
              for i in range(1000)], 0)
print(pd.DataFrame({'effect': params.round(0),
                    'error': err.round(0)}))<jupyter_output>effect  error
Mon          -1341.0  154.0
Tue          -1010.0  150.0
Wed          -1057.0  148.0
Thu          -1362.0  148.0
Fri          -2009.0  148.0
Sat          -4745.0  142.0
Sun          -4836.0  138.0
holiday      -1878.0  321.0
daylight_hrs   610.0   10.0
annual         160.0   15.0<jupyter_text>We first see that there is a relatively stable trend in the weekly baseline: there are many more riders on weekdays than on weekends and holidays. We see that for each additional hour of daylight, 129 ± 9 more people choose to ride; a temperature increase of one degree Celsius encourages 65 ± 4 people to grab their bicycle; a dry day means an average of 548 ± 33 more riders, and each inch of precipitation means 665 ± 62 more people leave their bike at home. Once all these effects are accounted for, we see a modest increase of 27 ± 18 new daily riders each year.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12176960140166447, 'fraction_numerical': 0.04139290407358739, 'mean_word_length': 4.4891826923076925, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '25161', 'license': 'MIT', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1463, 'n_words': 482, 'path': 'Bicycle_challenge_project.ipynb', 'repo_name': 'chandra-sekar-1983/bicycle_challenge', 'size': '4566'}