<jupyter_start><jupyter_text>training data split<jupyter_code>dayloss_72 =df_dayloss[df_dayloss['station_id'] == 72]
from sklearn.cross_validation import train_test_split
import matplotlib.pyplot as plt
%matplotlib inline
# training data split
X = dayloss_72[['wek_index','holiday_idx','docks']]
y = dayloss_72['bike_loss']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 123)
#y_train, y_test = cross_validation(y, test_size = 0.25, random_state = 123)
X_train = X_train.sort_index()
X_test = X_test.sort_index()
y_train = y_train.sort_index()
y_test = y_test.sort_index()
X_train[1:5]
plt.plot(dayloss_72.index, dayloss_72.bike_loss)<jupyter_output><empty_output><jupyter_text>Lasso<jupyter_code>from sklearn.linear_model import Lasso
from sklearn.model_selection import KFold
import numpy as np
import math
def RMSPE(y, y_pred):
    return math.sqrt( ((1- y_pred / y)**2).mean() )
def test_lasso(alphas):
    scores_lasso = []
    kf = KFold(n_splits=10, random_state = 123)
    
    for alpha in alphas:
        lasso = Lasso(alpha)
        for train, val in kf.split(X_train):
            score_cv = []
            y_pred = lasso.fit(X_train.values[train],y_train.values[train]).predict(X_train.values[val])
            score_cv.append(RMSPE(y_train.values[val], y_pred))
            score = np.mean(score_cv)
        scores_lasso.append(score)
    
    plt.figure(figsize=(14,8))
    cv_lasso = pd.Series(scores_lasso, index = alphas)
    cv_lasso.plot(title = "Validation - Lasso")
    plt.xlabel("alpha")
    plt.ylabel("RMSPE")
    print(cv_lasso.argmin())
alphas = [1e-4, 1e-3,1e-2, 0.1, 1, 5, 10, 20]
test_lasso(alphas)
alphas = [10, 12, 14, 18, 20]
test_lasso(alphas)
alphas = [10, 10.5, 11, 11.25, 11.5, 11.75, 12, 12.25, 12.5, 13]
test_lasso(alphas)
alphas = [10, 10.10,10.15, 10.20, 10.25, 10.30,10.40,10.55,10.7]
test_lasso(alphas)
lasso = Lasso(alpha=10.2)
scores_lasso = []
kf = KFold(n_splits=10, random_state = 123)
    
for alpha in alphas:
    lasso = Lasso(alpha)
    for train, val in kf.split(X_train):
        score_cv = []
        y_pred = lasso.fit(X_train.values[train],y_train.values[train]).predict(X_train.values[val])
        score_cv.append(RMSPE(y_train.values[val], y_pred))
        score = np.mean(score_cv)
    scores_lasso.append(score)
# prediction of bike loss of station 72
y_pred
# High temp & weather condition added 
import requests
import time
import pandas as pd
from  __builtin__ import any as b_any


def weather_Data(df_climate):
    dates = []
    maxtempF = []
    cltype = []
    
    dk = df_climate.drop_duplicates(['start_day'])

    for x in dk['start_day']:
        dates.append(x)
        api_str = 'http://api.worldweatheronline.com/premium/v1/past-weather.ashx?key=af846e459d4f4a93b4c221033172104&q=NY&date=' + x + '&format=json'
        # print api_str
        time.sleep(0.02)
        r = requests.get(api_str)
        print "request over"

        k = r.json()

        maxtempF.append(k['data']['weather'][0]['maxtempF'])

        size = len(k['data']['weather'][0]['hourly'])
        climate = []
        for l in range(size):
            climate.append(str(k['data']['weather'][0]['hourly'][l]['weatherDesc'][0].values()).split(" "))

            # print climate
        if b_any("snow" in x for x in climate):
            cltype.append("snow")
        elif b_any("rain" in x for x in climate):
            cltype.append("rain")
        elif b_any("sunny" in x for x in climate):
            cltype.append("sunny")
        else:
            cltype.append("clear")

    new_dict = dict(zip(dates, maxtempF))
    new_dict2 = dict(zip(dates, cltype))
    df_climate['High_temp'] = df_climate['start_day'].map(new_dict)
    df_climate['Climate_type'] = df_climate['start_day'].map(new_dict2)
    return df_climate
# Remove docks col and 
del df_dayloss['docks']
df_dayloss[1:5]<jupyter_output><empty_output><jupyter_text>change categorical variables to dummy variables:    1. wek_index: Weekday:0; Weekend:1    2. holiday_index: Non-holiday:0; Holiday:1    3. High_temp: continuous    4. Climate_type: Snow: (1,0,0); Rain:(0,1,0); Sunny:(0,0,1); Clear:(0,0,0)<jupyter_code># Assign weekday = 0 and weekend = 1
df_dayloss['wek_index']=df_dayloss['wek_index'].replace([1,2,3,4],[0,0,0,0])
df_dayloss['wek_index']=df_dayloss['wek_index'].replace([5,6],[1,1])   # weekend
# Get Dummy Variables for 'Climate Type'
dummy_weather=pd.get_dummies(df_dayloss['Climate_type'])
df_dayloss = pd.concat([df_dayloss, dummy_weather], axis=1)   
# Make variables for regression in continuous columns
cols = df_dayloss.columns.tolist()
cols.insert(len(cols)-1, cols.pop(cols.index('Climate_type')))
df_dayloss = df_dayloss.reindex(columns= cols)
df_dayloss[1:5]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14689265536723164, 'fraction_numerical': 0.04289600334798075, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '320112', 'license': 'MIT', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1834, 'n_words': 339, 'path': '660-final_4-23-1.ipynb', 'repo_name': 'kkandhas/CityBike-Demand-Prediction-and-Analysis', 'size': '4779'}