Kaggle competition: Bike Sharing Demand
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(28, 28, 1),padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64, kernel_size=3, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256,kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(256,kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(512,kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.summary()<define_variables>
train=pd.read_csv('.. /input/train.csv')
 train.info()
BATCH_SIZE = 512
EPOCHS = 60<train_model>
train['date']=train.datetime.apply(lambda x: x.split() [0])
 train['hour']=train.datetime.apply(lambda x:x.split() [1].split(':')[0])
 train['weekday'] = train.date.apply(lambda dateString : calendar.day_name[datetime.strptime(dateString,"%Y-%m-%d" ).weekday() ])
 train['month']=train.date.apply(lambda dateString: calendar.month_name[datetime.strptime(dateString,'%Y-%m-%d' ).month])
 train['season']=train.season.map({1:'Spring',2:'Summer',3:'Fall',4:'Winter'})
 train['weather']=train.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 " } )
model.compile(loss='categorical_crossentropy',optimizer=Adadelta() ,metrics=['accuracy'])
 datagen.fit(train_image)
 history = model.fit_generator(datagen.flow(train_image,train_label, batch_size=BATCH_SIZE),
epochs = EPOCHS,
shuffle=True,
validation_data =(val_image,val_label),
verbose = 1,
steps_per_epoch=train_image.shape[0] // BATCH_SIZE )<predict_on_test>
category_vars=['hour','weekday','month','season','weather','holiday','workingday']
for var in category_vars:
train[var]=train[var].astype('category')
 train.info()
test_label = model.predict(test_image)
<predict_on_test>
train=train.drop('datetime',axis=1 )
label_hot = model.predict(test_image)
 label = np.argmax(label_hot,1)
 id_ = np.arange(0,label.shape[0] )<load_from_csv>
train.isnull().sum()
sim = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 print(sim.head(10))<save_to_csv>
train.isnull().sum()
save = pd.DataFrame({'id':id_,'label':label})
 print(save.head(10))
save.to_csv('submission.csv',index=False )<set_options>
trainwo=train[np.abs(train['count']-train['count'].mean())<=3*train['count'].std() ]
sns.set()
%matplotlib inline 
<set_options>
train=pd.read_csv('.. /input/train.csv')
 test=pd.read_csv(".. /input/test.csv" )
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config )<load_from_csv>
data=train.append(test)
 data.reset_index(inplace=True)
 data.drop('index',inplace=True,axis=1)
train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 dig = pd.read_csv(".. /input/Kannada-MNIST/Dig-MNIST.csv")
 sample = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv" )<prepare_x_and_y>
data['date']=data['datetime'].apply(lambda x:x.split() [0])
 data['hour']=data['datetime'].apply(lambda x:x.split() [1].split(':')[0])
 data['year']=data['date'].apply(lambda x:x.split('-')[0])
 data['month']=data['date'].apply(lambda x:datetime.strptime(x,"%Y-%m-%d" ).month)
 data['weekday']=data['date'].apply(lambda x:datetime.strptime(x,"%Y-%m-%d" ).weekday() )
x_train = train.drop(columns=['label'])
 y_train = to_categorical(train.label)
 x_test = test.drop(columns="id")
<data_type_conversions>
wind0=data[data['windspeed']==0]
windNot0=data[data['windspeed']!=0]
rf_wind=RandomForestRegressor()
wind_cols=['season','weather','year','month','temp','atemp','humidity']
rf_wind.fit(windNot0[wind_cols],windNot0['windspeed'])
 pred=rf_wind.predict(X=wind0[wind_cols])
 wind0['windspeed']=pred
data=windNot0.append(wind0)
 data.reset_index(inplace=True)
 data.drop('index',axis=1,inplace=True)
x_train[train.label == 0].iloc[0].to_numpy().astype(np.uint8 ).reshape(28, 28 )<data_type_conversions>
categorical_features=['season','month','year','workingday','holiday','weather','hour']
numerical_features=['humidity','windspeed','temp','atemp']
drop_features=['casual','registered','datetime','date','count']
for var in categorical_features:
data[var]=data[var].astype('category' )
fig, ax = plt.subplots(ncols=10, figsize=(15,15))
for i in range(10):
kannada = x_train[train.label == i]
ax[i].set_title(i)
 ax[i].axis('off')
 ax[i].imshow(kannada.iloc[0, :].to_numpy().astype(np.uint8 ).reshape(28, 28))<feature_engineering>
train=data[pd.notnull(data['count'])].sort_values(by=['datetime'])
 test=data[~pd.notnull(data['count'])].sort_values(by='datetime')
 datetimecol=test['datetime']
y_train=train['count']
y_train_registered=train['registered']
y_train_casual=train['casual']
train=train.drop(drop_features,axis=1)
 test=test.drop(drop_features,axis=1 )
x_train = x_train /255
x_test = x_test / 255<prepare_x_and_y>
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(p+1)for p in y]))
log2=np.nan_to_num(np.array([np.log(a+1)for a in y_]))
calc=(log1-log2)**2
return np.sqrt(np.mean(calc))
x_train = x_train.values.reshape(-1,28,28,1)
 x_test = x_test.values.reshape(-1,28,28,1 )<split>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning)
 lr=LinearRegression()
y_train_log=np.log1p(y_train)
 lr.fit(train,y_train_log)
 pred=lr.predict(train)
 print("RMSLE Value For Linear Regression: ",rmsle(y_train_log,pred,True))
X_train, X_test, Y_train, Y_test = train_test_split(x_train, y_train, test_size=0.2, random_state=42 )<create_dataframe>
ridge=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=GridSearchCV(ridge,ridge_params_,scoring=rmsle_scorer,cv=5)
 y_train_log=np.log1p(y_train)
 grid_ridge.fit(train,y_train_log)
 preds=grid_ridge.predict(train)
 print(grid_ridge.best_params_)
 print("RMSLE Value For Ridge Regression: ", rmsle(y_train_log,preds,True))
fig,ax=plt.subplots()
df=pd.DataFrame(grid_ridge.cv_results_)
 df
df['alpha']=df['param_alpha']
df['rmsle']=df['mean_test_score'].apply(lambda x:-x)
 sn.pointplot(data=df,x='alpha',y='rmsle',ax=ax )
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1 )<train_model>
lasso=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=GridSearchCV(lasso,lasso_params,scoring=rmsle_scorer,cv=5)
 y_train_log=np.log1p(y_train)
 grid_lasso.fit(train,y_train_log)
 pred=grid_lasso.predict(train)
 print(grid_lasso.best_params_)
 print("RMSLE Value For Lasso Regression: ", rmsle(y_train_log,pred,True))
fig,ax=plt.subplots()
df=pd.DataFrame(grid_lasso.cv_results_)
 df['alpha']=df['param_alpha']
df['rmsle']=-df['mean_test_score']
sn.pointplot(data=df,x='alpha',y='rmsle',ax=ax )
datagen.fit(X_train )<define_variables>
rf=RandomForestRegressor(n_estimators=100)
 y_train_log=np.log1p(y_train)
 rf.fit(train,y_train_log)
 pred=rf.predict(train)
 print("RMSLE Value For Random Forest: ", rmsle(y_train_log,pred,True))
EPOCHS=50
BATCH_SIZE=64<choose_model_class>
gbr=GradientBoostingRegressor(n_estimators=4000,alpha=0.01)
 y_train_log=np.log1p(y_train)
 gbr.fit(train,y_train_log)
 pred=gbr.predict(train)
 print("RMSLE Value For Gradient Boost: ", rmsle(y_train_log,pred,True))
callbacks = [ReduceLROnPlateau(monitor='val_accuracy',
patience=5,
verbose=1,
factor=0.5,
min_lr=0.00001)]<choose_model_class>
submission=pd.DataFrame({
'datetime':datetimecol,
'count':[max(0,x)for x in np.exp(pred_test)]
})
 submission.to_csv('bike_predictions_gbm.csv',index=False )