Kaggle competition: How Much Did It Rain? II
X = train_df.drop(['ID_code','target'],axis=1)
 y = train_df['target']<choose_model_class>
INPUT_WIDTH = 19
N_FEATURES = 22
THRESHOLD = 73
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )<init_hyperparams>
train_df = pd.read_csv(".. /input/train.csv")
 train_df[train_df.columns[1:]] = train_df[train_df.columns[1:]].astype(np.float32)
 train_df.head(20 )
params = {'num_leaves': 9,
'min_data_in_leaf': 42,
'objective': 'binary',
'max_depth': 16,
'learning_rate': 0.0123,
'boosting': 'gbdt',
'bagging_freq': 5,
'bagging_fraction': 0.8,
'feature_fraction': 0.8201,
'bagging_seed': 11,
'reg_alpha': 1.728910519108444,
'reg_lambda': 4.9847051755586085,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 4}<train_model>
train_new = train_new.fillna(0.0)
 train_new = train_new.reset_index(drop=True)
 train_new.head()
prediction = np.zeros(len(X_test))
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=20000,
valid_sets = [train_data, valid_data],verbose_eval=300,early_stopping_rounds = 200)
 prediction += model.predict(X_test, num_iteration=model.best_iteration)/5<train_model>
df_temp = pd.DataFrame(train_new.groupby('Id')['Expected'].mean())
 meaningful_ids = np.array(df_temp[df_temp['Expected'] < THRESHOLD].index)
 del df_temp
train_final = train_new[np.in1d(train_new.Id, meaningful_ids)]
del train_new, meaningful_ids
train_final.shape
train_pool = Pool(X,y)
 m = CatBoostClassifier(iterations=300,eval_metric="AUC", boosting_type = 'Ordered')
 m.fit(X,y,silent=True)
 y_pred1 = m.predict(X_test)
 m.best_score_<save_to_csv>
train_gp = train_final.groupby("Id")
 train_size = len(train_gp)
 del train_final
X_train = np.zeros(( train_size, INPUT_WIDTH, N_FEATURES), dtype=np.float32)
 y_train = np.zeros(train_size, dtype=np.float32)
 seq_len_train = np.zeros(train_size, dtype=np.float32)
 i = 0
for _, group in train_gp:
X = group.values
seq_len = X.shape[0]
X_train[i,:seq_len,:] = X[:,1:23]
y_train[i] = X[0,23]
seq_len_train[i] = seq_len
i += 1
del X
del train_gp
X_train.shape, y_train.shape
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = prediction
sub.to_csv("submission.csv", index=False )<save_to_csv>
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 = np.array(test_df.Id.unique())
 test_final = test_df.fillna(0.0)
 test_final = test_final.reset_index(drop=True)
 del test_df
sub1 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub1["target"] = y_pred1
sub1.to_csv("submission1.csv", index=False )<save_to_csv>
test_gp = test_final.groupby("Id")
 test_size = len(test_gp)
 del test_final
X_test = np.zeros(( test_size, INPUT_WIDTH, N_FEATURES), dtype=np.float32)
 seq_len_test = np.zeros(test_size, dtype=np.float32)
 i = 0
for _, group in test_gp:
X = group.values
seq_len = X.shape[0]
X_test[i,:seq_len,:] = X[:,1:23]
seq_len_test[i] = seq_len
i += 1
del X
del test_gp
X_test.shape
sub2 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub2["target"] =(prediction + y_pred1)/2
sub2.to_csv("submission2.csv", index=False )<load_from_csv>
TimeDistributed, Flatten, Bidirectional
train_df=pd.read_csv(".. /input/train.csv" )<load_from_csv>
es_callback = EarlyStopping(monitor='val_loss', min_delta=0, patience=5 )
test_df=pd.read_csv(".. /input/test.csv" )<count_values>
BATCH_SIZE = 1024
N_EPOCHS = 32
train_df.target.value_counts() /train_df.shape[0]<count_missing_values>
def get_model_simple(shape=(19,22)) :
inp = Input(shape)
 x = CuDNNLSTM(64, return_sequences=False )(inp)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model
train_df.isnull().any().sum() 
<prepare_x_and_y>
model_0.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )
target=train_df.target<drop_column>
y_pred_0 = model_0.predict(X_test)
 submission_0 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_0.reshape(-1)})
 submission_0.to_csv('submission_0.csv', index=False )
target = train_df['target']
train=train_df.drop(["ID_code","target"],axis=1)
 test = test_df.drop('ID_code',axis=1 )<create_dataframe>
def get_model_seq(shape=(19,22)) :
inp = Input(shape)
 x = CuDNNLSTM(64, return_sequences=True )(inp)
 x = TimeDistributed(Dense(10))(x)
 x = Flatten()(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model
scv=StratifiedKFold(n_splits=5,shuffle=True,random_state=42)
 oof=np.zeros(train_df.shape[0])
 predictions=np.zeros(test_df.shape[0])
 feature_importance_df=pd.DataFrame()
param = {
'num_leaves': 10,
'max_bin': 119,
'min_data_in_leaf': 11,
'learning_rate': 0.02,
'min_sum_hessian_in_leaf': 0.00245,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 4.972,
'lambda_l2': 2.276,
'min_gain_to_split': 0.65,
'max_depth': 14,
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}
for fold_,(trn_idx, val_idx)in enumerate(scv.split(train.values, target.values)) :
print("fold n°{}".format(fold_))
trn_data = lgb.Dataset(train.iloc[trn_idx], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train.iloc[val_idx], label=target.iloc[val_idx])
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 100)
 oof[val_idx] = clf.predict(train.iloc[val_idx], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = train.columns
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test, num_iteration=clf.best_iteration)/ 5
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )<save_to_csv>
model_1.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )
sub_lgb = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_lgb["target"] = predictions
sub_lgb.to_csv("submission_lgb.csv", index=False )<import_modules>
y_pred_1 = model_1.predict(X_test)
 submission_1 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_1.reshape(-1)})
 submission_1.to_csv('submission_1.csv', index=False )
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from catboost import CatBoostClassifier,Pool
from IPython.display import display
import matplotlib.patches as patch
import matplotlib.pyplot as plt
from sklearn.svm import NuSVR
from scipy.stats import norm
from sklearn import svm
import lightgbm as lgb
import xgboost as xgb
import seaborn as sns
import pandas as pd
import numpy as np
import warnings
import time
import glob
import sys
import os
import gc<set_options>
def get_model_bilstm(shape=(19,22)) :
inp = Input(shape)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(inp)
 x = TimeDistributed(Dense(10))(x)
 x = Flatten()(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model
fold_n=5
folds = StratifiedKFold(n_splits=fold_n, shuffle=True, random_state=10)
 %matplotlib inline
%precision 4
warnings.filterwarnings('ignore')
 plt.style.use('ggplot')
 np.set_printoptions(suppress=True)
 pd.set_option("display.precision", 15 )<import_modules>
model_2.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )
print('pandas: {}'.format(pd.__version__))
print('numpy: {}'.format(np.__version__))
print('Python: {}'.format(sys.version))<import_modules>
y_pred_2 = model_2.predict(X_test)
 submission_2 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_2.reshape(-1)})
 submission_2.to_csv('submission_2.csv', index=False )
from sklearn.metrics import roc_auc_score, roc_curve<load_from_csv>
def get_model_deep(shape=(19,22)) :
inp = Input(shape)
 x = Dense(16 )(inp)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = TimeDistributed(Dense(64))(x)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = TimeDistributed(Dense(1))(x)
 x = AveragePooling1D()(x)
 x = Flatten()(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model
train= pd.read_csv(".. /input/train.csv")
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
model_3.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )
sample_submission = pd.read_csv('.. /input/sample_submission.csv')
 sample_submission.head()<set_options>
y_pred_3 = model_3.predict(X_test)
 submission_3 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_3.reshape(-1)})
 submission_3.to_csv('submission_3.csv', index=False )
def reduce_mem_usage(df):
start_mem_usg = df.memory_usage().sum() / 1024**2
print("Memory usage of properties dataframe is :",start_mem_usg," MB")
 NAlist = []
for col in df.columns:
if df[col].dtype != object:
print("******************************")
 print("Column: ",col)
 print("dtype before: ",df[col].dtype)
 IsInt = False
mx = df[col].max()
mn = df[col].min()
if not np.isfinite(df[col] ).all() :
NAlist.append(col)
 df[col].fillna(mn-1,inplace=True)
 asint = df[col].fillna(0 ).astype(np.int64)
 result =(df[col] - asint)
 result = result.sum()
if result > -0.01 and result < 0.01:
IsInt = True
if IsInt:
if mn >= 0:
if mx < 255:
df[col] = df[col].astype(np.uint8)
 elif mx < 65535:
df[col] = df[col].astype(np.uint16)
 elif mx < 4294967295:
df[col] = df[col].astype(np.uint32)
 else:
df[col] = df[col].astype(np.uint64)
 else:
if mn > np.iinfo(np.int8 ).min and mx < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif mn > np.iinfo(np.int16 ).min and mx < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif mn > np.iinfo(np.int32 ).min and mx < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif mn > np.iinfo(np.int64 ).min and mx < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
df[col] = df[col].astype(np.float32)
 print("dtype after: ",df[col].dtype)
 print("******************************")
 print("___MEMORY USAGE AFTER COMPLETION:___")
 mem_usg = df.memory_usage().sum() / 1024**2
print("Memory usage is: ",mem_usg," MB")
 print("This is ",100*mem_usg/start_mem_usg,"% of the initial size")
 return df, NAlist<count_missing_values>
y_pred_avg =(y_pred_0 + y_pred_1 + y_pred_2 + y_pred_3)/ 4
submission_avg = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_avg.reshape(-1)})
 submission_avg.to_csv('submission_avg.csv', index=False )