Kaggle competition: Elo Merchant Category Recommendation
<SOS> metric: RMSE Kaggle data source: elo-merchant-category-recommendation<drop_column>
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
train.drop(train[train['rideDistance'] >= 20000].index, inplace=True )<filter>
%%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' )
train[train['swimDistance'] >= 2000]<drop_column>
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]
train.drop(train[train['swimDistance'] >= 2000].index, inplace=True )<drop_column>
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}
train.drop(train[train['weaponsAcquired'] >= 50].index, inplace=True )<drop_column>
%%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))
train.drop(train[train['heals'] >= 40].index, inplace=True )<feature_engineering>
model_without_outliers = pd.DataFrame({"card_id":df_test["card_id"].values})
 model_without_outliers["target"] = predictions
train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['maxPlaceNorm'] = train['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['matchDurationNorm'] = train['matchDuration']*(( 100-train['playersJoined'])/100 + 1)
 to_show = ['Id', 'kills','killsNorm','damageDealt', 'damageDealtNorm', 'maxPlace', 'maxPlaceNorm', 'matchDuration', 'matchDurationNorm', 'playersJoined']
train[to_show][0:11]<data_type_conversions>
%%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' )
train['groupId'] = train['groupId'].astype('category')
 train['matchId'] = train['matchId'].astype('category')
 train['groupId_cat'] = train['groupId'].cat.codes
train['matchId_cat'] = train['matchId'].cat.codes
train.drop(columns=['groupId', 'matchId'], inplace=True)
 train[['groupId_cat', 'matchId_cat']].head()<drop_column>
target = df_train['outliers']
del df_train['outliers']
del df_train['target']
train.drop(columns = ['Id'], inplace=True )<import_modules>
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]
from sklearn.preprocessing import LabelEncoder<categorify>
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}
label_encoder = LabelEncoder()
mapped_education = pd.Series(label_encoder.fit_transform(train['matchType']))
mapped_education.value_counts().plot.barh()
print(dict(enumerate(label_encoder.classes_)) )<categorify>
%%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)) )
categorical_columns = train.columns[train.dtypes == 'object'].union(['matchType'])
 for column in categorical_columns:
train[column] = label_encoder.fit_transform(train[column])
 train.head(10 )<prepare_x_and_y>
df_outlier_prob = pd.DataFrame({"card_id":df_test["card_id"].values})
 df_outlier_prob["target"] = predictions
df_outlier_prob.head()
target = 'winPlacePerc'
features = list(train.columns)
 y_train = np.array(train[target])
 features.remove(target)
 x_train = train[features]
print(x_train.shape,y_train.shape )<data_type_conversions>
outlier_id = pd.DataFrame(df_outlier_prob.sort_values(by='target',ascending = False ).head(25000)['card_id'] )
test['headshot_rate'] = test['headshotKills'] / test['kills']
test['headshot_rate'] = test['headshot_rate'].fillna(0)
 test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['maxPlaceNorm'] = test['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 test['matchDurationNorm'] = test['matchDuration']*(( 100-test['playersJoined'])/100 + 1)
 test['healsandboosts'] = test['heals'] + test['boosts']
test['groupId'] = test['groupId'].astype('category')
 test['matchId'] = test['matchId'].astype('category')
 test['groupId_cat'] = test['groupId'].cat.codes
test['matchId_cat'] = test['matchId'].cat.codes
categorical_columns = test.columns[test.dtypes == 'object'].union(['matchType'])
 for column in categorical_columns:
test[column] = label_encoder.fit_transform(test[column])
 x_test = test[features].copy()
x_test.fillna(0, inplace=True)
 print(x_test.shape )<split>
best_submission = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/3.695.csv' )
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state = 1 )<choose_model_class>
most_likely_liers = best_submission.merge(outlier_id,how='right')
 most_likely_liers.head()
rf = RandomForestRegressor(n_estimators = 80, min_samples_leaf = 3, max_depth = 26, max_features = 0.5,
n_jobs = -1 )<train_model>
%%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
%%time
rf.fit(x_train, y_train )<predict_on_test>
model_without_outliers.to_csv("combining_submission.csv", index=False )