application_vnd.jupyter.stderr_output_1.png
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

dfs = {}
for name in ['train', 'test']:
    df = pd.read_csv('/kaggle/input/bike-sharing-demand/%s.csv' % name)
    df['_data'] = name
    dfs[name] = df

df = dfs['train'].append(dfs['test'])
df.columns = map(str.lower, df.columns)

for col in ['casual', 'registered', 'count']:
    df['%s_log' % col] = np.log(df[col] + 1)

dt = pd.DatetimeIndex(df['datetime'])
df.set_index(dt, inplace=True)
df['date'] = dt.date
df['day'] = dt.day
df['month'] = dt.month
df['year'] = dt.year
df['hour'] = dt.hour
df['dow'] = dt.dayofweek
df['woy'] = dt.weekofyear

def get_rmsle(y_pred, y_actual):
    diff = np.log(y_pred + 1) - np.log(y_actual + 1)
    mean_error = np.square(diff).mean()
    return np.sqrt(mean_error)
def get_data():
    data = df[df['_data'] == 'train'].copy()
    return data
def custom_train_test_split(data, cutoff_day=15):
    train = data[data['day'] <= cutoff_day]
    test = data[data['day'] > cutoff_day]
    return (train, test)
def prep_data(data, input_cols):
    X = data[input_cols]
    y_r = data['registered_log']
    y_c = data['casual_log']
    return (X, y_r, y_c)
def predict_on_validation_set(model, input_cols):
    data = get_data()
    train, test = custom_train_test_split(data)
    X_train, y_train_r, y_train_c = prep_data(train, input_cols)
    X_test, y_test_r, y_test_c = prep_data(test, input_cols)
    model_r = model.fit(X_train, y_train_r)
    y_pred_r = np.exp(model_r.predict(X_test)) - 1
    model_c = model.fit(X_train, y_train_c)
    y_pred_c = np.exp(model_c.predict(X_test)) - 1
    y_pred_comb = np.round(y_pred_r + y_pred_c)
    y_pred_comb[y_pred_comb < 0] = 0
    y_test_comb = np.exp(y_test_r) + np.exp(y_test_c) - 2
    score = get_rmsle(y_pred_comb, y_test_comb)
    return (y_pred_comb, y_test_comb, score)
df_test = df[df['_data'] == 'test'].copy()
def predict_on_test_set(model, x_cols):
    df_train = df[df['_data'] == 'train'].copy()
    X_train = df_train[x_cols]
    y_train_cas = df_train['casual_log']
    y_train_reg = df_train['registered_log']
    X_test = df_test[x_cols]
    casual_model = model.fit(X_train, y_train_cas)
    y_pred_cas = casual_model.predict(X_test)
    y_pred_cas = np.exp(y_pred_cas) - 1
    registered_model = model.fit(X_train, y_train_reg)
    y_pred_reg = registered_model.predict(X_test)
    y_pred_reg = np.exp(y_pred_reg) - 1
    return y_pred_cas + y_pred_reg

params = {'n_estimators': 1000, 'max_depth': 15, 'random_state': 0, 'min_samples_split': 5, 'n_jobs': -1}
rf_model = RandomForestRegressor(**params)
rf_cols = ['weather', 'temp', 'atemp', 'windspeed', 'workingday', 'season', 'holiday', 'sticky', 'hour', 'dow', 'woy', 'peak']
rf_p, rf_t, rf_score = predict_on_validation_set(rf_model, rf_cols)

params = {'n_estimators': 150, 'max_depth': 5, 'random_state': 0, 'min_samples_leaf': 10, 'learning_rate': 0.1, 'subsample': 0.7, 'loss': 'ls'}
gbm_model = GradientBoostingRegressor(**params)
gbm_cols = ['weather', 'temp', 'atemp', 'humidity', 'windspeed', 'holiday', 'workingday', 'season', 'hour', 'dow', 'year', 'ideal', 'count_season']
gbm_p, gbm_t, gbm_score = predict_on_validation_set(gbm_model, gbm_cols)
print(gbm_score)
y_p = np.round(0.2 * rf_p + 0.8 * gbm_p)
print(get_rmsle(y_p, rf_t))