Kaggle competition: Bike Sharing Demand
def make_outs(test_group, preds):
i = 0
x = np.array([])
 for test_gp, preds in zip(test_group, preds):
if(not i%1000):
print(i)
 gp = test_gp[1]
x = np.append(x,(preds[gp['atom_index_0'].values, gp['atom_index_1'].values] + preds[gp['atom_index_1'].values, gp['atom_index_0'].values])/2.0)
 i = i+1
return x
def group_mean_log_mae(y_true, y_pred, types, floor=1e-9):

 maes =(y_true-y_pred ).abs().groupby(types ).mean()
return np.log(maes.map(lambda x: max(x, floor)) ).mean()<normalization>
%matplotlib inline
max_size = 29
preds = preds.reshape(( -1,max_size, max_size))
out_unscaled = make_outs(val_group, preds )<feature_engineering>
train_df = pd.read_csv('.. /input/bike-sharing-demand/train.csv')
 test_df = pd.read_csv('.. /input/bike-sharing-demand/test.csv' )
val['pred_scalar_coupling_constant'] = out_unscaled
coups_to_isolate = ['1JHC', '1JHN', '2JHC', '2JHH', '2JHN', '3JHC', '3JHH', '3JHN']
for i, coup in enumerate(coups_to_isolate):
scale_min = train['scalar_coupling_constant'].loc[train.type == coup].min()
scale_max = train['scalar_coupling_constant'].loc[train.type == coup].max()
scale_mid =(scale_max + scale_min)/2
scale_norm = scale_max - scale_mid
val.loc[val.type == coup, 'pred_scalar_coupling_constant'] = val['pred_scalar_coupling_constant'].loc[val.type == coup]*scale_norm + scale_mid
val.loc[val.type == coup, 'pred_scalar_coupling_constant'] = val['pred_scalar_coupling_constant'].loc[val.type == coup]<compute_train_metric>
train_df['datetime'] = pd.to_datetime(train_df['datetime'])
 test_df['datetime'] = pd.to_datetime(test_df['datetime'] )
for coup in coups_to_isolate:
log_mae = group_mean_log_mae(val['scalar_coupling_constant'], val['pred_scalar_coupling_constant'], val['type'][val.type == coup])
 print(coup,"\t", log_mae)
 total = group_mean_log_mae(val['scalar_coupling_constant'], val['pred_scalar_coupling_constant'], val['type'])
 print("")
 print("Total:","\t", total )<load_pretrained>
train_df['year'] = train_df['datetime'].apply(lambda x: x.year)
 train_df['month'] = train_df['datetime'].apply(lambda x: x.month)
 train_df['day'] = train_df['datetime'].apply(lambda x: x.day)
 train_df['hour'] = train_df['datetime'].apply(lambda x: x.hour)
 test_df['year'] = test_df['datetime'].apply(lambda x: x.year)
 test_df['month'] = test_df['datetime'].apply(lambda x: x.month)
 test_df['day'] = test_df['datetime'].apply(lambda x: x.day)
 test_df['hour'] = test_df['datetime'].apply(lambda x: x.hour )
nodes_test = np.load(datadir + "internalgraphdata/nodes_test.npz")['arr_0']
in_edges_test = np.load(datadir + "internalgraphdata/in_edges_test.npz")['arr_0']
in_edges_test = in_edges_test.reshape(-1,in_edges_test.shape[1]*in_edges_test.shape[2],in_edges_test.shape[3] )<predict_on_test>
train_df = train_df.drop(['datetime', 'casual', 'registered'], axis=1)
 test_df = test_df.drop(['datetime'], axis=1 )
preds = mpnn.predict({'adj_input' : in_edges_test, 'nod_input': nodes_test}, verbose=1 )<save_model>
def rmsle(y, pred):
log_y = np.log1p(y)
 log_pred = np.log1p(pred)
 squared_error =(log_y - log_pred)**2
rmsle = np.sqrt(np.mean(squared_error))
return rmsle
np.save("preds_kernel.npy" , preds )<groupby>
train_df['count'] = np.log1p(train_df['count'] )
test_group = test.groupby('molecule_name' )<normalization>
X_train, X_test, y_train, y_test = train_test_split(train_df.drop(['count'], axis=1), train_df['count'], test_size=0.3 )
preds = preds.reshape(( -1,max_size, max_size))
out_unscaled = make_outs(test_group, preds )<feature_engineering>
lr_reg = LinearRegression()
lr_reg.fit(X_train, y_train)
 pred = lr_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('RMSLE:', rmsle(y_test_exp, pred_exp))
test['scalar_coupling_constant'] = out_unscaled
coups_to_isolate = ['1JHC', '1JHN', '2JHC', '2JHH', '2JHN', '3JHC', '3JHH', '3JHN']
for i, coup in enumerate(coups_to_isolate):
scale_min = train['scalar_coupling_constant'].loc[train.type == coup].min()
scale_max = train['scalar_coupling_constant'].loc[train.type == coup].max()
scale_mid =(scale_max + scale_min)/2
scale_norm = scale_max - scale_mid
test.loc[test.type == coup, 'scalar_coupling_constant'] = test['scalar_coupling_constant'].loc[test.type == coup]*scale_norm + scale_mid
test.loc[test.type == coup, 'pred_scalar_coupling_constant'] = test['scalar_coupling_constant'].loc[test.type == coup] 
<save_to_csv>
train_df = pd.get_dummies(train_df, columns=['year', 'month', 'day', 'hour', 'holiday', 'workingday', 'season', 'weather'])
 test_df = pd.get_dummies(test_df, columns=['year', 'month', 'day', 'hour', 'holiday', 'workingday', 'season', 'weather'] )
test[['id','scalar_coupling_constant']].to_csv('submission.csv', index=False )<set_options>
train_df, test_df = train_df.align(test_df, join='left', axis=1)
 test_df = test_df.drop(['count'], axis=1 )
%matplotlib inline 
<load_from_csv>
X_train, X_test, y_train, y_test = train_test_split(train_df.drop(['count'], axis=1), train_df['count'], test_size=0.3 )
train_df = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv" )<load_from_csv>
lr_reg = LinearRegression()
lr_reg.fit(X_train, y_train)
 pred = lr_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('LinearRegression RMSLE:', rmsle(y_test_exp, pred_exp))
test_df = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv" )<count_values>
ridge_reg = Ridge(alpha=10)
 ridge_reg.fit(X_train, y_train)
 pred = ridge_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('Ridge RMSLE:', rmsle(y_test_exp, pred_exp))
train_df['label'].value_counts()<import_modules>
lasso_reg = Lasso(alpha=0.01)
 lasso_reg.fit(X_train, y_train)
 pred = lasso_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('Lasso RMSLE:', rmsle(y_test_exp, pred_exp))
import keras
import sklearn
from keras.preprocessing import image
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, BatchNormalization, Activation, Dropout, LeakyReLU, Input, Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras import optimizers
from keras.layers.merge import concatenate
from keras import utils
from keras.utils import plot_model
from sklearn.model_selection import train_test_split<choose_model_class>
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
BATCH_SIZE = 64
NP_EPOCHS = 50
NP_CLASSES = 10
VALIDATION_SPLIT = 0.20
OPTIMIZER = optimizers.RMSprop()<prepare_x_and_y>
rf_reg = RandomForestRegressor(n_estimators=500)
 rf_reg.fit(X_train, y_train)
 pred = rf_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('RandomForestRegressor RMSLE:', rmsle(y_test_exp, pred_exp))
train = train_df.drop('label', axis=1)
 target = train_df['label']<drop_column>
gbm_reg = GradientBoostingRegressor(n_estimators=500)
 gbm_reg.fit(X_train, y_train)
 pred = gbm_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('GradientBoostingRegressor RMSLE:', rmsle(y_test_exp, pred_exp))
test = test_df.drop('id', axis=1 )<feature_engineering>
xgb_reg = XGBRegressor(n_estimators=500)
 xgb_reg.fit(X_train, y_train)
 pred = xgb_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('XGBRegressor RMSLE:', rmsle(y_test_exp, pred_exp))
train /= 255<feature_engineering>
lgbm_reg = LGBMRegressor(n_estimators=500)
 lgbm_reg.fit(X_train, y_train)
 pred = lgbm_reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('LGBMRegressor RMSLE:', rmsle(y_test_exp, pred_exp))
test /= 255<categorify>
X_train = train_df.drop(['count'], axis=1)
 y_train = train_df['count']
X_test = test_df
target = utils.to_categorical(target, NP_CLASSES )<define_variables>
lgbm_reg = LGBMRegressor(n_estimators=500)
 lgbm_reg.fit(X_train, y_train)
 pred = lgbm_reg.predict(X_test)
 pred_exp = np.expm1(pred )
NP_OUTPUT_FUNC = "softmax"
NP_DESCRIMINATOR_FUNC = "sigmoid"
NP_DROPOUT_FIRST = 0.15
NP_DROPOUT_SECOND = 0.10<choose_model_class>
submission = pd.read_csv('.. /input/bike-sharing-demand/sampleSubmission.csv')
 submission
left_input = Input(shape=input_shape)
 x = left_input
NP_FILTERS = 32
for i in range(3):
x = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE, padding="same" )(x)
 x = LeakyReLU(alpha=0.15 )(x)
 x = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(x)
 x = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE )(x)
 x = LeakyReLU(alpha=0.15 )(x)
 x = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(x)
 x = Dropout(NP_DROPOUT_FIRST )(x)
 x = MaxPooling2D()(x)
 NP_FILTERS *= 2<choose_model_class>
submission.loc[:, 'count'] = pred_exp
submission
right_input = Input(shape=input_shape)
 y = right_input
NP_FILTERS = 32
for i in range(3):
y = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE, padding="same" )(y)
 y = LeakyReLU(alpha=0.15 )(y)
 y = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(y)
 y = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE )(y)
 y = LeakyReLU(alpha=0.15 )(y)
 y = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(y)
 y = Dropout(NP_DROPOUT_FIRST )(y)
 y = MaxPooling2D()(y)
 NP_FILTERS *= 2<concatenate>
submission.to_csv('submission.csv', index=False )
<choose_model_class><EOS> 
submission.to_csv('submission.csv', index=False )