# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from catboost import CatBoostRegressor
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import TimeSeriesSplit,train_test_split
from sklearn.metrics import mean_squared_error 
from math import sqrt
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.ensemble import BaggingRegressor

train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")

train.head()

train_df = train.copy()
test_df = test.copy()

train_df.info()

train_df.date_time.dtype

train_df['date_time'] = pd.to_datetime(train_df['date_time'])

test_df.info()

test_df['date_time'] = pd.to_datetime(test_df['date_time'])

# Conclusions time split of data validation set to be made accordingly

train_df.head()


def LabelEncode(dataf,datap):
    le = LabelEncoder()
    le.fit(dataf)
    v = le.transform(datap)
    return v


def findweekday(a):
    return a.weekday()


sns.kdeplot(train_df.traffic_volume)

# # Train preprocessing

train_df['weather_type'] = LabelEncode(train_df['weather_type'].append([test_df['weather_type']]),train_df['weather_type'])
train_df['is_holiday'] = LabelEncode(train_df['is_holiday'].append([test_df['is_holiday']]),train_df['is_holiday'])
train_df['weather_description'] = LabelEncode(train_df['weather_description'].append([test_df['weather_description']]),train_df['weather_description'])
train_df['month'] = train_df['date_time'].dt.month
train_df['day'] = train_df['date_time'].apply(findweekday)
train_df['hour'] = train_df['date_time'].dt.hour
train_df['year'] = train_df['date_time'].dt.year

train_df.head()

test_df['weather_type'] = LabelEncode(test_df['weather_type'].append([train['weather_type']]),test_df['weather_type'])
test_df['is_holiday'] = LabelEncode(test_df['is_holiday'].append([train['is_holiday']]),test_df['is_holiday'])
test_df['weather_description'] = LabelEncode(train['weather_description'].append([test_df['weather_description']]),test_df['weather_description'])
test_df['month'] = test_df['date_time'].dt.month
test_df['day'] = test_df['date_time'].apply(findweekday)
test_df['hour'] = test_df['date_time'].dt.hour
test_df['year'] = test_df['date_time'].dt.year

test_df.head()

new_train = train_df.drop(['date_time'],axis=1)

new_test = test_df.drop(['date_time'],axis=1)

# +
#Time based split
# -

tscv = TimeSeriesSplit(n_splits=2)

y = new_train.traffic_volume

x = new_train.drop(['traffic_volume'],axis=1)

x.year.value_counts()

new_test.year.value_counts()

x_train_new = new_train.loc[new_train.year!=2017]

x_test_new = new_train.loc[new_train.year==2017]

x_train_new.year.value_counts()

x_train,x_test,y_train,y_test = train_test_split(x,y,shuffle=False)

# +
#applying staking to the problem
model1 = RandomForestRegressor()
model2 = XGBRegressor()
model3 = LGBMRegressor()
model4 = CatBoostRegressor()
model5 = BaggingRegressor()

model1.fit(x_train,y_train)
model2.fit(x_train,y_train)
model3.fit(x_train,y_train)
model4.fit(x_train,y_train)
model5.fit(x_train,y_train)

pred1 = model1.predict(x_test)
pred2 = model2.predict(x_test)
pred3 = model3.predict(x_test)
pred4 = model4.predict(x_test)
pred5 = model5.predict(x_test)

predt1 = model1.predict(new_test)
predt2 = model2.predict(new_test)
predt3 = model3.predict(new_test)
predt4 = model4.predict(new_test)
predt5 = model5.predict(new_test)

stacked_pred = np.column_stack((pred1,pred2,pred3,pred4,pred5))
stacked_test_pred = np.column_stack((predt1,predt2,predt3,predt4,predt5))

meta_model = RandomForestRegressor()
meta_model.fit(stacked_pred,y_test)
final_pred = meta_model.predict(stacked_test_pred)
# -

stacked_test_pred

y_test.values

final_pred

bagged_pred = np.zeros(new_test.shape[0])

seed = 310
bags = 4

for n in range(0,4):
    rf = RandomForestRegressor(n_estimators=280,n_jobs=-1,random_state=seed+n,max_features=9,min_samples_leaf=5,min_samples_split=20)
    rf.fit(x,y)
    preds = rf.predict(new_test)
    bagged_pred+=preds
bagged_pred/=bags

score(y_test,bagged_pred)

rf.get_params()

rf.feature_importances_

rf.fit(x,y)

y_pred2 = rf.predict(new_test)

kk = pd.DataFrame({'date_time':test['date_time'],'traffic_volume':final_pred})

kk.to_csv("sub7_stack.csv",index=False)

cb = CatBoostRegressor()

x_train.head()

cat_features = [0,11,12,13,14,15,16]

cb.fit(x_train,y_train,cat_features=cat_features,eval_set=(x_test,y_test),plot=True)


def score(y_actual,y_predicted):
    rms = sqrt(mean_squared_error(y_actual, y_predicted))
    rms = rms/10000
    return max(0,100-rms)


y_pred = cb.predict(x_test)

score(y_test,y_pred)

rf = RandomForestRegressor()

rf.fit(x_train,y_train)

y_pred = rf.predict(x_test)

score(y_test,y_pred)

rf.fit(x,y)

y_pred2 = rf.predict(new_test)

kk = pd.DataFrame({'date_time':test['date_time'],'traffic_volume':y_pred2})

kk.to_csv("sub1.csv",index=False)

xg = XGBRegressor()

xg.fit(x_train,y_train)

v = xg.predict(x_test)

score(y_test,v)

xg.fit(x,y)

y_pred2 = xg.predict(new_test)

kk = pd.DataFrame({'date_time':test['date_time'],'traffic_volume':y_pred2})

kk.to_csv("sub2.csv",index=False)

cb = CatBoostRegressor(iterations=5000,learning_rate=0.1,depth=7)

cb.fit(x_train,y_train,eval_set=(x_test,y_test),plot=True,cat_features=cat_features,early_stopping_rounds=300,use_best_model=True,verbose_eval=100)



v = cb.predict(x_test)

score(y_test,v)

lb = LGBMRegressor()

lb.fit(x_train,y_train)

v = lb.predict(x_test)

score(y_test,v)

lb.fit(x,y)

y_pred2 = lb.predict(new_test)

kk = pd.DataFrame({'date_time':test['date_time'],'traffic_volume':y_pred2})

kk.to_csv("sub3.csv",index=False)

cb = CatBoostRegressor(iterations=3000)

cb.fit(x,y)

y_pred2 = cb.predict(new_test)

kk = pd.DataFrame({'date_time':test['date_time'],'traffic_volume':y_pred2})

kk.to_csv("sub4.csv",index=False)



StarcoderdataJupyterScriptsDedupFiltered