Kaggle competition: Elo Merchant Category Recommendation
<SOS> metric: RMSE Kaggle data source: elo-merchant-category-recommendation<feature_engineering>
import numpy as np
import pandas as pd
import time
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold, KFold
from sklearn.metrics import mean_squared_error
from sklearn.metrics import log_loss
merge[merge["kills"] > 35]["kills"] = 35
merge[merge["DBNOs"] > 20]["DBNOs"] = 20
merge[merge["assists"] > 10]["assists"] = 10
merge[merge["walkDistance"] > 10000]["walkDistance"] = 10000
merge[merge["boosts"] > 15]["boosts"] = 15
merge[merge["heals"] > 30]["heals"] = 30
merge[merge["weaponsAcquired"] > 50]["weaponsAcquired"] = 50
merge[merge["damageDealt"] > 2000]["damageDealt"] = 2000
merge[merge["longestKill"] > 500]["longestKill"] = 500
merge[merge["killStreaks"] > 10]["killStreaks"] = 10
merge[merge["rideDistance"] > 15000]["rideDistance"] = 15000
merge[merge["headshotKills"] > 15]["headshotKills"] = 15<count_unique_values>
%%time
df_train = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/train_clean.csv')
 df_test = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/test_clean.csv' )
def rstr(df, pred=None):
obs = df.shape[0]
types = df.dtypes
counts = df.apply(lambda x: x.count())
 uniques = df.apply(lambda x: [x.unique() ])
 nulls = df.apply(lambda x: x.isnull().sum())
 distincts = df.apply(lambda x: x.unique().shape[0])
 missing_ration =(df.isnull().sum() /obs)*100
skewness = df.skew()
kurtosis = df.kurt()
print('Data shape: ', df.shape)
 if pred is None:
cols = ['types', 'counts', 'distincts', 'nulls', 'missing ration', 'uniques', 'skewness', 'kurtosis', 'corr']
str = pd.concat([types, counts, distincts, nulls, missing_ration, uniques, skewness, kurtosis], axis=1)
 else:
corr = df.corr() [pred]
str =pd.concat([types, counts, distincts, nulls, missing_ration, uniques, skewness, kurtosis, corr], axis=1, sort=False)
 corr_col = 'corr ' + pred
cols = ['types', 'counts', 'distincts', 'nulls', 'missing ration', 'uniques', 'skewness', 'kurtosis', corr_col]
str.columns = cols
dtypes = str.types.value_counts()
print('___________________________
Data types:
',str.types.value_counts())
 print('___________________________')
 return str<categorify>
df_train = df_train[df_train['outliers'] == 0]
target = df_train['target']
del df_train['target']
features = [c for c in df_train.columns if c not in ['card_id', 'first_active_month','outliers']]
categorical_feats = [c for c in features if 'feature_' in c]
matchType = pd.get_dummies(merge.matchType )<concatenate>
param = {'objective':'regression',
'num_leaves': 31,
'min_data_in_leaf': 25,
'max_depth': 7,
'learning_rate': 0.01,
'lambda_l1':0.13,
"boosting": "gbdt",
"feature_fraction":0.85,
'bagging_freq':8,
"bagging_fraction": 0.9 ,
"metric": 'rmse',
"verbosity": -1,
"random_state": 2333}
merge = pd.concat([merge,matchType], axis=1 )<groupby>
%%time
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=2333)
 oof = np.zeros(len(df_train))
predictions = np.zeros(len(df_test))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(df_train,df_train['outliers'].values)) :
print("fold {}".format(fold_))
trn_data = lgb.Dataset(df_train.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(df_train.iloc[val_idx][features], label=target.iloc[val_idx])
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval= 100, early_stopping_rounds = 200)
 oof[val_idx] = clf.predict(df_train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
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(df_test[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(mean_squared_error(oof, target)**0.5))
merge["group_size"] = merge.groupby(["matchId"])['groupId'].transform('count')
 merge["group_per"] = merge["maxPlace"]/merge["group_size"]<feature_engineering>
model_without_outliers = pd.DataFrame({"card_id":df_test["card_id"].values})
 model_without_outliers["target"] = predictions
merge["distanceSum"] = 0.85*merge['walkDistance'] + 0.14*merge['rideDistance'] + 0.01*merge['swimDistance']
merge["distanceRank"] = merge.groupby(["matchId"])["distanceSum"].rank(pct=True)
 merge.drop("distanceSum", axis=1, inplace=True )<feature_engineering>
%%time
df_train = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/train_clean.csv')
 df_test = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/test_clean.csv' )
merge["killPlace"] = merge["killPlace"]-merge["group_per"]
merge["killPlace"] = -1*merge["killPlace"]/merge["group_size"]<feature_engineering>
target = df_train['outliers']
del df_train['outliers']
del df_train['target']
merge["DBNOsNassists"] = merge["DBNOs"]+merge["assists"]
merge["items"] = merge["boosts"]+merge["heals"]
merge["itemsRank"] = merge.groupby(["matchId"])["items"].rank(pct=True)
 merge["damageDealtRank"] = merge.groupby(["matchId"])["damageDealt"].rank(pct=True)
 merge["damageDealtPerkills"] = merge["damageDealt"]/(merge["kills"]+1)
 merge["killStreaksPerkills"] = merge["killStreaks"] /(merge["kills"]+1)
 merge["headshotPerKills"] = merge["headshotKills"]/(merge["kills"]+1)
 merge["weaponsAcqRank"] = merge.groupby(["matchId"])["weaponsAcquired"].rank(pct=True)
 merge["longestKillRank"] = merge.groupby(["matchId"])["longestKill"].rank(pct=True)
 merge["DBNOsNassistsRank"] = merge.groupby(["matchId"])["DBNOsNassists"].rank(pct=True)
 merge["revivesRank"] = merge.groupby(["matchId"])["revives"].rank(pct=True)
 merge.drop(["boosts", "heals", "assists", "damageDealt", "killStreaks", "headshotKills", "weaponsAcquired", "longestKill", "DBNOs", "revives"],
axis=1, inplace=True )<categorify>
features = [c for c in df_train.columns if c not in ['card_id', 'first_active_month']]
categorical_feats = [c for c in features if 'feature_' in c]
merge["itemsRank"] = merge.groupby(["groupId"])["items"].transform(np.mean)
 merge["damageDealtRank"] = merge.groupby(["groupId"])["damageDealtRank"].transform(np.mean)
 merge["damageDealtPerkills"] = merge.groupby(["groupId"])["damageDealtPerkills"].transform(np.mean)
 merge["killStreaksPerkills"] = merge.groupby(["groupId"])["killStreaksPerkills"].transform(np.mean)
 merge["headshotPerKills"] = merge.groupby(["groupId"])["headshotPerKills"].transform(np.mean)
 merge["weaponsAcqRank"] = merge.groupby(["groupId"])["weaponsAcqRank"].transform(np.mean)
 merge["longestKillRank"] = merge.groupby(["groupId"])["longestKillRank"].transform(np.mean)
 merge["DBNOsNassistsRank"] = merge.groupby(["groupId"])["DBNOsNassistsRank"].transform(np.mean)
 merge["revivesRank"] = merge.groupby(["groupId"])["revivesRank"].transform(np.mean)
 merge["distanceRank"] = merge.groupby(["groupId"])["distanceRank"].transform(np.mean)
 merge["killStreaksPerkillsRank"] = merge.groupby(["matchId"])["killStreaksPerkills"].rank(pct=True)
 merge["headshotPerKillsRank"] = merge.groupby(["matchiId"])["headshotPerKills"].rank(pct=True )<feature_engineering>
param = {'num_leaves': 31,
'min_data_in_leaf': 30,
'objective':'binary',
'max_depth': 6,
'learning_rate': 0.01,
"boosting": "rf",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9 ,
"bagging_seed": 11,
"metric": 'binary_logloss',
"lambda_l1": 0.1,
"verbosity": -1,
"random_state": 2333}
<drop_column>
%%time
folds = KFold(n_splits=5, shuffle=True, random_state=15)
 oof = np.zeros(len(df_train))
predictions = np.zeros(len(df_test))
feature_importance_df = pd.DataFrame()
start = time.time()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(df_train.values, target.values)) :
print("fold n°{}".format(fold_))
trn_data = lgb.Dataset(df_train.iloc[trn_idx][features], label=target.iloc[trn_idx], categorical_feature=categorical_feats)
 val_data = lgb.Dataset(df_train.iloc[val_idx][features], label=target.iloc[val_idx], categorical_feature=categorical_feats)
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 200)
 oof[val_idx] = clf.predict(df_train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
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(df_test[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(log_loss(target, oof)) )
merge_id = merge['Id']
merge.drop(['Id','groupId','matchId','numGroups','matchType'], axis=1, inplace=True )<split>
df_outlier_prob = pd.DataFrame({"card_id":df_test["card_id"].values})
 df_outlier_prob["target"] = predictions
df_outlier_prob.head()
train = merge.loc[train_index,:]
test = merge.loc[test_index,:].reset_index()<drop_column>
outlier_id = pd.DataFrame(df_outlier_prob.sort_values(by='target',ascending = False ).head(25000)['card_id'] )
train.drop(['index'], axis=1, inplace=True)
 test.drop(['index', 'level_0'], axis=1, inplace=True )<concatenate>
best_submission = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/3.695.csv' )
train = pd.concat([train, target],axis=1 )<drop_column>
most_likely_liers = best_submission.merge(outlier_id,how='right')
 most_likely_liers.head()
target = train['winPlacePerc']
target = target.fillna(target.mean())
 train.drop('winPlacePerc', axis=1, inplace=True )<set_options>
%%time
for card_id in most_likely_liers['card_id']:
model_without_outliers.loc[model_without_outliers['card_id']==card_id,'target']\
= most_likely_liers.loc[most_likely_liers['card_id']==card_id,'target'].values
<split><EOS> 
model_without_outliers.to_csv("combining_submission.csv", index=False )