<jupyter_start><jupyter_text>Bicycle Traffic * https://jakevdp.github.io/PythonDataScienceHandbook/05.06-linear-regression.html<jupyter_code>%matplotlib inline
%load_ext autoreload
%autoreload 2
import os
import sys
p = os.path.join(os.path.dirname('__file__'), '..')
sys.path.append(p)
from common import *
sns.set()
DATA_DIR = '../data/'<jupyter_output>The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload<jupyter_text>Data<jupyter_code>counts = pd.read_csv(DATA_DIR+'FremontBridge.csv', index_col='Date', parse_dates=True)
weather = pd.read_csv(DATA_DIR+'BicycleWeather.csv', index_col='DATE', parse_dates=True)<jupyter_output><empty_output><jupyter_text>Feature Engineering<jupyter_code># Pull the daily counts of bicycle traffic
daily = counts.resample('d').sum()
daily['Total'] = daily.sum(axis=1)
daily = daily[['Total']] # remove other columns
# Account for day of the week patterns
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for i in range(7):
    daily[days[i]] = (daily.index.dayofweek == i).astype(float)
# Account for Holidays
from pandas.tseries.holiday import USFederalHolidayCalendar

cal = USFederalHolidayCalendar()
holidays = cal.holidays('2012', '2016')

# Fill the index with (1) indicating a holiday
holidays = pd.Series(1, index=holidays, name='holiday')

# Left Join the Series/Frames along the index column
daily = daily.join(holidays, how='left')
daily['holiday'].fillna(0, inplace=True)
# Compute hours of daylight
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)
# Add temperature
# 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'])
# Add precipitation
# 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']])
# Add num years passed
# Model trends overtime (e.g. population increase)
daily['annual'] = (daily.index - daily.index[0]).days / 365.
daily.head()
# Drop any rows with null values
daily.dropna(axis=0, how='any', inplace=True)

# Input/Target
column_names = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'holiday',
                'daylight_hrs', 'PRCP', 'dry day', 'Temp (C)', 'annual']
X = daily[column_names]
y = daily['Total']<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from sklearn.linear_model import LinearRegression
model = LinearRegression(fit_intercept=False)
model.fit(X, y)
daily['predicted'] = model.predict(X)
daily[['Total', 'predicted']].plot(alpha=0.5);<jupyter_output><empty_output><jupyter_text>Evaluate<jupyter_code># Inspect Coefficients
# See how each feature contributes to the output!
params = pd.Series(model.coef_, index=X.columns)
params
# Inspect Uncertainty / Std of Coefficients
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            505.0   86.0
Tue            610.0   83.0
Wed            593.0   83.0
Thu            482.0   85.0
Fri            178.0   81.0
Sat          -1103.0   80.0
Sun          -1134.0   83.0
holiday      -1187.0  163.0
daylight_hrs   129.0    9.0
PRCP          -665.0   62.0
dry day        548.0   33.0
Temp (C)        65.0    4.0
annual          27.0   18.0
{'fraction_non_alphanumeric': 0.15710979686294677, 'fraction_numerical': 0.044741578812033944, 'mean_word_length': 4.111695137976347, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '538805', 'license': 'MIT', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1397, 'n_words': 334, 'path': 'kaggle/BicycleTraffic.ipynb', 'repo_name': 'jaingaurav3/ML_sample', 'size': '3889'}