Kaggle competition: Titanic - Machine Learning from Disaster
const_cols = [c for c in train_df.columns if train_df[c].nunique(dropna=False)== 1]
train_df.drop(columns=const_cols + ['trafficSource.campaignCode'], inplace=True)
 test_df.drop(columns=const_cols, inplace=True )<categorify>
data = pd.read_csv("/kaggle/input/titanic/train.csv")
 data = data.set_index("PassengerId" )
def browser_mapping(x):
browsers = ['chrome', 'safari', 'firefox', 'internet explorer', 'edge', 'opera', 'coc coc', 'maxthon', 'iron']
mobile_browsers = ['android', 'samsung', 'mini', 'iphone', 'in-app', 'playstation', 'mozilla', 'chrome',
'blackberry', 'nokia', 'browser', 'amazon', 'lunascape', 'netscape', 'konqueror', 'puffin']
if x in browsers:
return x
elif '(not set)' in x:
return x
elif sum([(k in x)for k in mobile_browsers]):
return 'mobile browser'
else:
return 'others'
def adcontents_mapping(x):
if 'google' in x:
return 'google'
elif('placement' in x)or('placememnt' in x):
return 'placement'
elif('(not set)' in x)or('nan' in x):
return x
elif 'ad' in x:
return 'ad'
else:
return 'others'
def source_mapping(x):
sources = ['google', 'youtube', '(not set)', 'nan', 'yahoo', 'facebook', 'reddit', 'bing', 'quora', 'outlook', 'linkedin',
'pinterest', 'ask', 'siliconvalley', 'lunametrics', 'amazon', 'mysearch', 'qiita', 'messenger', 'twitter',
't.co', 'vk.com', 'search', 'edu', 'mail', 'ad', 'golang', 'direct', 'dealspotr', 'sashihara', 'phandroid',
'baidu', 'mdn', 'duckduckgo', 'seroundtable', 'metrics', 'sogou', 'businessinsider', 'github', 'gophergala',
'yandex', 'msn', 'dfa', 'feedly', 'arstechnica', 'squishable', 'flipboard', 't-online.de', 'sm.cn', 'wow',
'partners']
for s in sources:
if s in x:
return s
return 'others'
def custom(df):
print('custom.. ')
 df['source.country'] = df['trafficSource.source'] + '_' + df['geoNetwork.country']
df['campaign.medium'] = df['trafficSource.campaign'] + '_' + df['trafficSource.medium']
df['browser.category'] = df['device.browser'] + '_' + df['device.deviceCategory']
df['browser.os'] = df['device.browser'] + '_' + df['device.operatingSystem']
df['device_deviceCategory_channelGrouping'] = df['device.deviceCategory'] + "_" + df['channelGrouping']
df['channelGrouping_browser'] = df['device.browser'] + "_" + df['channelGrouping']
df['channelGrouping_OS'] = df['device.operatingSystem'] + "_" + df['channelGrouping']
df['content.source'] = df['trafficSource.adContent'] + "_" + df['source.country']
df['medium.source'] = df['trafficSource.medium'] + "_" + df['source.country']
for i in ['geoNetwork.city', 'geoNetwork.continent', 'geoNetwork.country','geoNetwork.metro', 'geoNetwork.networkDomain', 'geoNetwork.region','geoNetwork.subContinent']:
for j in ['device.browser','device.deviceCategory', 'device.operatingSystem', 'trafficSource.source']:
df[i + "_" + j] = df[i] + "_" + df[j]
return df
for df in [train_df, test_df]:
df['device.browser'] = df['device.browser'].map(lambda x: browser_mapping(str(x ).lower()))
df['trafficSource.adContent'] = df['trafficSource.adContent'].map(lambda x: adcontents_mapping(str(x ).lower()))
df['trafficSource.source'] = df['trafficSource.source'].map(lambda x: source_mapping(str(x ).lower()))
train_df = custom(train_df)
 test_df = custom(test_df )<data_type_conversions>
Y = data.Survived
data = data.drop(columns= ['Survived','Name', 'Ticket', 'Cabin'])
for df in [train_df, test_df]:
df['visitStartTime'] = pd.to_datetime(df['visitStartTime'], unit='s')
 df['sess_date_dow'] = df['visitStartTime'].dt.dayofweek
df['sess_date_hours'] = df['visitStartTime'].dt.hour
df['totals.hits/views'] = df['totals.hits'] /(df['totals.pageviews'] + 1)
 df.sort_values(['fullVisitorId', 'visitStartTime'], ascending=True, inplace=True)
 df['next_session_1'] =(
 df['visitStartTime'] - df[['fullVisitorId', 'visitStartTime']].groupby('fullVisitorId')['visitStartTime'].shift(1)
 ).astype(np.int64)// 1e9 // 60 // 60
df['next_session_2'] =(
 df['visitStartTime'] - df[['fullVisitorId', 'visitStartTime']].groupby('fullVisitorId')['visitStartTime'].shift(-1)
 ).astype(np.int64)// 1e9 // 60 // 60<data_type_conversions>
data.Pclass = data.Pclass.astype(str)
 features = [ 'Pclass','Sex', 'Embarked']
dums = pd.get_dummies(data[features])
 data = data.drop(columns=features )
num_cols = ['visitNumber', 'totals.hits', 'totals.pageviews', 'totals.hits/views', 'next_session_1', 'next_session_2']
excluded_cols = ['date', 'fullVisitorId', 'sessionId', 'totals.transactionRevenue', 'visitId', 'visitStartTime']
cat_cols = [col for col in train_df.columns
if(col not in excluded_cols)and(col not in num_cols)]
all_cols = cat_cols + num_cols
for col in cat_cols:
train_df[col], indexer = pd.factorize(train_df[col].astype(str))
test_df[col] = indexer.get_indexer(test_df[col].astype(str))
for col in num_cols:
train_df[col] = train_df[col].astype(float)
 test_df[col] = test_df[col].astype(float)
 train_df["totals.transactionRevenue"].fillna(0, inplace=True )<count_unique_values>
data = pd.concat([data, dums], axis = 1)
 data.head()
def get_folds(df=None, n_splits=5, seed=42):

 unique_vis = np.array(sorted(df['fullVisitorId'].unique()))
folds = GroupKFold(n_splits=n_splits)
 fold_ids = []
ids = np.arange(df.shape[0])
 np.random.seed(seed)
 for trn_vis, val_vis in folds.split(X=unique_vis, y=unique_vis, groups=unique_vis):
fold_ids.append(
 [
ids[df['fullVisitorId'].isin(unique_vis[trn_vis])],
ids[df['fullVisitorId'].isin(unique_vis[val_vis])]
] 
)
 return fold_ids<init_hyperparams>
data.fillna(data.mean() , inplace=True)
 data.isnull().values.any()
params = {'learning_rate': 0.01,
'objective': 'regression',
'metric': 'rmse',
'num_leaves': 49,
'verbose': 1,
'bagging_fraction': 0.94,
'feature_fraction': 0.67,
'random_state': 42,
'max_depth': 14,
'random_seed': 42,
'bagging_frequency': 5,
'lambda_l2': 0.2,
'lambda_l1': 0.55,
'min_child_samples': 130
}<train_model>
model_features=['Age','SibSp','Parch','Fare','Pclass_1','Pclass_2','Pclass_3','Sex_female','Sex_male','Embarked_C','Embarked_Q','Embarked_S']
X = data[model_features]
X_train, X_dev, y_train, y_dev = train_test_split(X,Y, test_size = 0.15)
 train_accs=[]
dev_accs = []
for nb_est in range(1,300):
model = RandomForestClassifier(n_estimators=nb_est, max_depth=5, random_state=1)
 model.fit(X_train,y_train)
 pred_train = model.predict(X_train)
 acc_train = accuracy_score(y_train, pred_train)
 pred_dev = model.predict(X_dev)
 acc_dev = accuracy_score(y_dev, pred_dev)
 train_accs.append(acc_train)
 dev_accs.append(acc_dev )
train_y = train_df["totals.transactionRevenue"]
folds = get_folds(train_df, n_splits=5)
 importances = pd.DataFrame()
oof_reg_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 for fold_,(trn_, val_)in enumerate(folds):
trn_x, trn_y = train_df[all_cols].iloc[trn_], train_y.iloc[trn_]
val_x, val_y = train_df[all_cols].iloc[val_], train_y.iloc[val_]
reg = lgb.LGBMRegressor(**params, n_estimators=2000)
 reg.fit(trn_x, np.log1p(trn_y), eval_set=[(val_x, np.log1p(val_y)) ], early_stopping_rounds=100,
verbose=100, eval_metric='rmse')
 imp_df = pd.DataFrame()
imp_df['feature'] = all_cols
imp_df['gain_reg'] = reg.booster_.feature_importance(importance_type='gain')
 imp_df['fold'] = fold_ + 1
importances = pd.concat([importances, imp_df], axis=0, sort=False)
 oof_reg_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 oof_reg_preds[oof_reg_preds < 0] = 0
lgb_preds = reg.predict(test_df[all_cols], num_iteration=reg.best_iteration_)
 lgb_preds[lgb_preds < 0] = 0
sub_preds += np.expm1(lgb_preds)/ len(folds)
 print("LGBM session-level error: ", mean_squared_error(np.log1p(train_y.values), oof_reg_preds)**.5)
 oof_pred_df = pd.DataFrame({"fullVisitorId": train_df["fullVisitorId"].values})
 oof_pred_df["transactionRevenue"] = train_df["totals.transactionRevenue"].values
oof_pred_df["PredictedRevenue"] = np.expm1(oof_reg_preds)
 oof_pred_df = oof_pred_df.groupby("fullVisitorId")["transactionRevenue", "PredictedRevenue"].sum().reset_index()
oof_err = np.sqrt(mean_squared_error(np.log1p(oof_pred_df["transactionRevenue"].values),
np.log1p(oof_pred_df["PredictedRevenue"].values)))
 print("LGBM user-level error: ", oof_err )<prepare_output>
model_final = RandomForestClassifier(n_estimators=145, max_depth=5, random_state=1)
 model_final.fit(X,Y )
train_df['predictions'] = np.expm1(oof_reg_preds)
 test_df['predictions'] = sub_preds<categorify>
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 test = test.set_index("PassengerId")
 test = test.drop(columns= ['Name', 'Ticket', 'Cabin'])
 test.Pclass = test.Pclass.astype(str)
 dums2 = pd.get_dummies(test[features])
 test = test.drop(columns=features)
 test = pd.concat([test, dums2], axis = 1)
 test.fillna(test.mean() , inplace=True)
 test.head()
def find_mode(x):
mode, mode_cnt = 0, 0
d = dict()
for val in x.values:
current_cnt = d.get(val, 0)+ 1
d[val] = current_cnt
if current_cnt > mode_cnt:
mode = val
mode_cnt = current_cnt
return mode
def parse_predictions(df):
top_N = 20
res = dict()
res['pred_first'] = df.predictions.values[0]
res['pred_last'] = df.predictions.values[-1]
for i, pred in enumerate(df.predictions.sort_values(ascending=False ).head(top_N)) :
res['pred_' + str(i)] = pred
return res<create_dataframe>
test = test[model_features]
pred = model_final.predict(test)
 output = pd.DataFrame({'PassengerId':test.index, 'Survived': pred})
 output.to_csv('my_submission.csv', index=False )