Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<split>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning)
 %matplotlib inline
train = dfall.iloc[:len(df),:]
test = dfall.iloc[len(df):,:]<import_modules>
dailyData = pd.read_csv(".. /input/train.csv" )
from sklearn import preprocessing
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold<categorify>
dailyData["date"] = dailyData.datetime.apply(lambda x : x.split() [0])
 dailyData["hour"] = dailyData.datetime.apply(lambda x : x.split() [1].split(":")[0])
 dailyData["weekday"] = dailyData.date.apply(lambda dateString : calendar.day_name[datetime.strptime(dateString,"%Y-%m-%d" ).weekday() ])
 dailyData["month"] = dailyData.date.apply(lambda dateString : calendar.month_name[datetime.strptime(dateString,"%Y-%m-%d" ).month])
 dailyData["season"] = dailyData.season.map({1: "Spring", 2 : "Summer", 3 : "Fall", 4 :"Winter" })
 dailyData["weather"] = dailyData.weather.map({1: " Clear + Few clouds + Partly cloudy + Partly cloudy",\
2 : " Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist ", \
3 : " Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds", \
4 :" Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog " } )
folds = train.copy()
Fold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 for n,(train_index, val_index)in enumerate(Fold.split(folds, folds["Survived"])) :
folds.loc[val_index, 'fold'] = int(n)
 folds['fold'] = folds['fold'].astype(int)
 print(folds.groupby(['fold', "Survived"] ).size() )<define_variables>
categoryVariableList = ["hour","weekday","month","season","weather","holiday","workingday"]
for var in categoryVariableList:
dailyData[var] = dailyData[var].astype("category" )
p_train = folds[folds["fold"] != 0]
p_val = folds[folds["fold"] == 0]<drop_column>
dailyData = dailyData.drop(["datetime"],axis=1 )
p_train = p_train.reset_index(drop=True)
 p_val = p_val.reset_index(drop=True )<import_modules>
dailyDataWithoutOutliers = dailyData[np.abs(dailyData["count"]-dailyData["count"].mean())<=(3*dailyData["count"].std())]
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset<define_variables>
dataTrain = pd.read_csv(".. /input/train.csv")
 dataTest = pd.read_csv(".. /input/test.csv" )
FEATURES = ["Pclass","Sex","Age","SibSp","Parch","C","Q","S",np.nan]
TARGET = "Survived"<prepare_x_and_y>
data = dataTrain.append(dataTest)
 data.reset_index(inplace=True)
 data.drop('index',inplace=True,axis=1 )
train_X = np.array(p_train[FEATURES])
 train_Y = np.array(p_train[TARGET])
 val_X = np.array(p_val[FEATURES])
 val_Y = np.array(p_val[TARGET] )<normalization>
data["date"] = data.datetime.apply(lambda x : x.split() [0])
 data["hour"] = data.datetime.apply(lambda x : x.split() [1].split(":")[0] ).astype("int")
 data["year"] = data.datetime.apply(lambda x : x.split() [0].split("-")[0])
 data["weekday"] = data.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).weekday())
 data["month"] = data.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).month )
scaler = StandardScaler()
scaler.fit(train_X)
 train_X = scaler.transform(train_X)
 val_X = scaler.transform(val_X )<data_type_conversions>
dataWind0 = data[data["windspeed"]==0]
dataWindNot0 = data[data["windspeed"]!=0]
rfModel_wind = RandomForestRegressor()
windColumns = ["season","weather","humidity","month","temp","year","atemp"]
rfModel_wind.fit(dataWindNot0[windColumns], dataWindNot0["windspeed"])
 wind0Values = rfModel_wind.predict(X= dataWind0[windColumns])
 dataWind0["windspeed"] = wind0Values
data = dataWindNot0.append(dataWind0)
 data.reset_index(inplace=True)
 data.drop('index',inplace=True,axis=1 )
train_X = torch.from_numpy(train_X ).float()
train_Y = torch.from_numpy(train_Y ).long()
val_X = torch.from_numpy(val_X ).float()
val_Y = torch.from_numpy(val_Y ).long()<create_dataframe>
categoricalFeatureNames = ["season","holiday","workingday","weather","weekday","month","year","hour"]
numericalFeatureNames = ["temp","humidity","windspeed","atemp"]
dropFeatures = ['casual',"count","datetime","date","registered"]
train_dataset = TensorDataset(train_X,train_Y)
 val_dataset = TensorDataset(val_X,val_Y )<load_pretrained>
for var in categoricalFeatureNames:
data[var] = data[var].astype("category" )
train_dataloader = DataLoader(train_dataset,batch_size=128,shuffle = True)
 val_dataloader = DataLoader(val_dataset,batch_size=32,shuffle = False )<define_variables>
dataTrain = data[pd.notnull(data['count'])].sort_values(by=["datetime"])
 dataTest = data[~pd.notnull(data['count'])].sort_values(by=["datetime"])
 datetimecol = dataTest["datetime"]
yLabels = dataTrain["count"]
yLablesRegistered = dataTrain["registered"]
yLablesCasual = dataTrain["casual"]
for a in train_dataloader:
print(a)
 break<normalization>
dataTrain = dataTrain.drop(dropFeatures,axis=1)
 dataTest = dataTest.drop(dropFeatures,axis=1 )
class Net(nn.Module):
def __init__(self):
super(Net,self ).__init__()
self.fc1 = nn.Linear(len(FEATURES),512)
 self.fc2 = nn.Linear(512,256)
 self.fc3 = nn.Linear(256,2)
 def forward(self,x):
x= F.relu(self.fc1(x))
x= F.relu(self.fc2(x))
x = self.fc3(x)
 return x<choose_model_class>
def rmsle(y, y_,convertExp=True):
if convertExp:
y = np.exp(y),
y_ = np.exp(y_)
 log1 = np.nan_to_num(np.array([np.log(v + 1)for v in y]))
log2 = np.nan_to_num(np.array([np.log(v + 1)for v in y_]))
calc =(log1 - log2)** 2
return np.sqrt(np.mean(calc))
model=Net()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters() , lr=0.01 )<train_model>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning)
 lModel = LinearRegression()
yLabelsLog = np.log1p(yLabels)
 lModel.fit(X = dataTrain,y = yLabelsLog)
 preds = lModel.predict(X= dataTrain)
 print("RMSLE Value For Linear Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
total_loss = 0
model.train()
for train_x,train_y in train_dataloader:
print(train_x)
 print(train_y)
 train_x, train_y = Variable(train_x),Variable(train_y)
 optimizer.zero_grad()
output = model(train_x)
 loss = criterion(output,train_y)
 loss.backward()
optimizer.step()
total_loss += loss.item()
break<train_model>
ridge_m_ = Ridge()
ridge_params_ = { 'max_iter':[3000],'alpha':[0.1, 1, 2, 3, 4, 10, 30,100,200,300,400,800,900,1000]}
rmsle_scorer = metrics.make_scorer(rmsle, greater_is_better=False)
 grid_ridge_m = GridSearchCV(ridge_m_,
ridge_params_,
scoring = rmsle_scorer,
cv=5)
 yLabelsLog = np.log1p(yLabels)
 grid_ridge_m.fit(dataTrain, yLabelsLog)
 preds = grid_ridge_m.predict(X= dataTrain)
 print(grid_ridge_m.best_params_)
 print("RMSLE Value For Ridge Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(12,5)
 df = pd.DataFrame(grid_ridge_m.grid_scores_)
 df["alpha"] = df["parameters"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_validation_score"].apply(lambda x:-x)
 sn.pointplot(data=df,x="alpha",y="rmsle",ax=ax )
def training(train_dataloader,model):
total_loss = 0
model.train()
for train_x,train_y in train_dataloader:
train_x, train_y = Variable(train_x),Variable(train_y)
 optimizer.zero_grad()
output = model(train_x)
 loss = criterion(output,train_y)
 loss.backward()
optimizer.step()
total_loss += loss.item()
return model,total_loss 
<compute_train_metric>
lasso_m_ = Lasso()
alpha = 1/np.array([0.1, 1, 2, 3, 4, 10, 30,100,200,300,400,800,900,1000])
 lasso_params_ = { 'max_iter':[3000],'alpha':alpha}
grid_lasso_m = GridSearchCV(lasso_m_,lasso_params_,scoring = rmsle_scorer,cv=5)
 yLabelsLog = np.log1p(yLabels)
 grid_lasso_m.fit(dataTrain, yLabelsLog)
 preds = grid_lasso_m.predict(X= dataTrain)
 print(grid_lasso_m.best_params_)
 print("RMSLE Value For Lasso Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(12,5)
 df = pd.DataFrame(grid_lasso_m.grid_scores_)
 df["alpha"] = df["parameters"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_validation_score"].apply(lambda x:-x)
 sn.pointplot(data=df,x="alpha",y="rmsle",ax=ax )
total_valloss = 0
model.eval()
with torch.no_grad() :
for val_x,val_y in val_dataloader:
output = model(val_x)
 valloss = criterion(output,val_y)
 total_valloss += valloss.item()<compute_test_metric>
rfModel = RandomForestRegressor(n_estimators=100)
 yLabelsLog = np.log1p(yLabels)
 rfModel.fit(dataTrain,yLabelsLog)
 preds = rfModel.predict(X= dataTrain)
 print("RMSLE Value For Random Forest: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
def valeval(val_dataloader,model):
total_valloss = 0
model.eval()
with torch.no_grad() :
for val_x,val_y in val_dataloader:
output = model(val_x)
 valloss = criterion(output,val_y)
 total_valloss += valloss.item()
return total_valloss<train_on_grid>
gbm = GradientBoostingRegressor(n_estimators=4000,alpha=0.01);
yLabelsLog = np.log1p(yLabels)
 gbm.fit(dataTrain,yLabelsLog)
 preds = gbm.predict(X= dataTrain)
 print("RMSLE Value For Gradient Boost: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
<train_model><EOS> 
submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_gbm_separate_without_fe.csv', index=False )