Kaggle competition: How Much Did It Rain? II
X_train, X_test, up_train, up_test, r_train, r_test = model_selection.train_test_split(X, up, r, test_size=0.1, random_state=99)
<train_model>
N_FEATURES = 22
THRESHOLD = 73
params = {'learning_rate': 0.25, 'max_depth': 10, 'boosting': 'gbdt', 'objective': 'binary', 'metric': 'binary_logloss', 'is_training_metric': True, 'seed': 42}
model = lgb.train(params, train_set=lgb.Dataset(X_train, label=up_train), num_boost_round=2000,
valid_sets=[lgb.Dataset(X_train, label=up_train), lgb.Dataset(X_test, label=up_test)],
verbose_eval=50, early_stopping_rounds=30 )<predict_on_test>
train_df = pd.read_csv(".. /input/train.csv" )
n_days = 0
for(market_obs_df, news_obs_df, predictions_template_df)in env.get_prediction_days() :
n_days +=1
if n_days % 50 == 0:
print(n_days,end=' ')
 market_obs_df = data_prep(market_obs_df, news_obs_df)
 X_live = market_obs_df[fcol].values
X_live = 1 -(( maxs - X_live)/ rng)
 lp = model.predict(X_live)
 confidence = 2 * lp -1
preds = pd.DataFrame({'assetCode':market_obs_df['assetCode'],'confidence':confidence})
 predictions_template_df = predictions_template_df.merge(preds,how='left' ).drop('confidenceValue',axis=1 ).fillna(0 ).rename(columns={'confidence':'confidenceValue'})
 env.predict(predictions_template_df)
<save_to_csv>
train_df[train_df.columns[1:]] = train_df[train_df.columns[1:]].astype(np.float32 )
env.write_submission_file()<load_from_csv>
good_ids = set(train_df.loc[train_df['Ref'].notna() , 'Id'])
 train_df = train_df[train_df['Id'].isin(good_ids)]
train_df.shape
train = pd.read_csv('.. /input/train.csv')
 train_label = train['target']
train_id = train['ID_code']
del train['target'], train['ID_code']
test = pd.read_csv('.. /input/test.csv')
 test_id = test['ID_code']
del test['ID_code']<normalization>
train_df.fillna(0.0, inplace=True)
 train_df.reset_index(drop=True, inplace=True)
 train_df.head()
sc = StandardScaler()
X_train_std = sc.fit_transform(train)
 X_test_std = sc.fit_transform(test )<import_modules>
train_df = train_df[train_df['Expected'] < THRESHOLD]
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_validate, cross_val_predict, cross_val_score
import xgboost as xgb
import lightgbm as lgbm
from sklearn.metrics import roc_auc_score<init_hyperparams>
train_groups = train_df.groupby("Id")
 train_size = len(train_groups )
num_boost_round = 10000
params = {"objective": "binary",
'metric' : 'binary_logloss',
"boosting_type": "gbdt",
"learning_rate": 0.1,
"num_leaves": 45,
"max_bin": 256,
"feature_fraction": 0.6,
"verbosity": 0,
"drop_rate": 0.1,
"is_unbalance": False,
"max_drop": 50,
"min_child_samples": 10,
"min_child_weight": 150,
"min_split_gain": 0,
"subsample": 0.9
}<define_variables>
X_train = np.zeros(( train_size, MAX_SEQ_LEN, N_FEATURES), dtype=np.float32)
 y_train = np.zeros(train_size, dtype=np.float32)
 i = 0
for _, group in train_groups:
X = group.values
seq_len = X.shape[0]
X_train[i,:seq_len,:] = X[:,1:23]
y_train[i] = X[0,23]
i += 1
del X
del train_groups
X_train.shape, y_train.shape
NFOLDS = 5
kfold = StratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=1112)
 x_score = []
final_cv_train = np.zeros(len(train_label))
final_cv_pred = np.zeros(len(test_id))<init_hyperparams>
test_df = pd.read_csv(".. /input/test.csv")
 test_df[test_df.columns[1:]] = test_df[test_df.columns[1:]].astype(np.float32)
 test_ids = test_df['Id'].unique()
test_df = test_df.fillna(0.0)
 test_df = test_df.reset_index(drop=True )
for s in range(16):
cv_train = np.zeros(len(train_label))
cv_pred = np.zeros(len(test_id))
params['seed'] = s
kf = kfold.split(X_train_std, train_label)
 best_trees = []
fold_scores = []
for i,(train_fold, validate)in enumerate(kf):
X_train, X_validate, label_train, label_validate = X_train_std[train_fold, :], X_train_std[validate, :], train_label[train_fold], train_label[validate]
dtrain = lgbm.Dataset(X_train, label_train)
 dvalid = lgbm.Dataset(X_validate, label_validate, reference=dtrain)
 bst = lgbm.train(params, dtrain, num_boost_round, valid_sets=dvalid, verbose_eval=100, early_stopping_rounds=100)
 best_trees.append(bst.best_iteration)
 cv_pred += bst.predict(X_test_std, num_iteration=bst.best_iteration)
 cv_train[validate] += bst.predict(X_validate)
 score = roc_auc_score(label_validate, cv_train[validate])
 print(score)
 fold_scores.append(score)
 cv_pred /= NFOLDS
final_cv_train += cv_train
final_cv_pred += cv_pred
print("cv score:")
 print(roc_auc_score(train_label, cv_train))
print("current score:", roc_auc_score(train_label, final_cv_train /(s + 1.)), s+1)
 print(fold_scores)
 print(best_trees, np.mean(best_trees))
x_score.append(roc_auc_score(train_label, cv_train))
print(x_score )<save_to_csv>
test_groups = test_df.groupby("Id")
 test_size = len(test_groups)
 X_test = np.zeros(( test_size, MAX_SEQ_LEN, N_FEATURES), dtype=np.float32)
 i = 0
for _, group in test_groups:
X = group.values
seq_len = X.shape[0]
X_test[i,:seq_len,:] = X[:,1:23]
i += 1
del X
del test_groups
X_test.shape
final_cv_pred_16 = final_cv_pred / 16.0
pd.DataFrame({'ID_code': test_id, 'target': final_cv_pred_16} ).to_csv('lgbm_pred_avg.csv', index=False )<save_to_csv>
Input,
Dense,
LSTM,
AveragePooling1D,
TimeDistributed,
Flatten,
Bidirectional,
Dropout 
)
for i in range(0,200000):
if final_cv_pred_16[i]>=.5:
final_cv_pred_16[i]=1
else:
final_cv_pred_16[i]=0
pd.DataFrame({'ID_code':test_id, 'target': final_cv_pred_16} ).to_csv('lgbm_pred_bin.csv', index=False )<set_options>
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=5)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, min_delta=0.01 )
sns.set_style('whitegrid')
<load_from_csv>
BATCH_SIZE = 1024
N_EPOCHS = 30
train_df = pd.read_csv('.. /input/train.csv')
 print('Rows: ',train_df.shape[0],'Columns: ',train_df.shape[1])
 train_df.info()<count_values>
def get_model_deep(shape=(19, 22)) :
inp = Input(shape)
 x = Dense(16 )(inp)
 x = Bidirectional(LSTM(64, return_sequences=True))(x)
 x = TimeDistributed(Dense(64))(x)
 x = Bidirectional(LSTM(128, return_sequences=True))(x)
 x = TimeDistributed(Dense(1))(x)
 x = AveragePooling1D()(x)
 x = Flatten()(x)
 x = Dropout(0.5 )(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model
train_df['target'].value_counts()<load_from_csv>
model = get_model_deep(( 19,22))
model.compile(optimizer='adam', loss='mae',)
 model.summary()
test_df = pd.read_csv('.. /input/test.csv' )<drop_column>
model.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[early_stopping, reduce_lr] )
X_test = test_df.drop('ID_code',axis=1 )<prepare_x_and_y>
y_pred = model.predict(X_test, batch_size=BATCH_SIZE)
 submission = pd.DataFrame({'Id': test_ids, 'Expected': y_pred.reshape(-1)})
 submission.to_csv('submission.csv', index=False )