Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<data_type_conversions>
print(os.listdir(".. /input"))
sns.set(style='white', context='notebook', palette='Set2')
 %matplotlib inline
warnings.simplefilter(action='ignore', category=FutureWarning )
all_data["Functional"] = all_data["Functional"].fillna("Typ" )<data_type_conversions>
train_df = pd.read_csv(".. /input/train_V2.csv")
 test_df = pd.read_csv(".. /input/test_V2.csv" )
all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0] )<data_type_conversions>
pd.DataFrame({'train':train_df.isna().sum() , 'test':test_df.isna().sum() } )
all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0] )<categorify>
train_df = train_df.dropna()
all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0] )<data_type_conversions>
dataset = pd.concat([train_df, test_df], sort=True)
 dataset = reduce_mem_usage(dataset )
all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0] )<data_type_conversions>
def min_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'matchType', 'winPlacePerc']
features = [col for col in df.columns if col not in cols_to_drop]
agg = df.groupby(['matchId','groupId'])[features].min()
return df.merge(agg, suffixes=['', '_min'], how='left', on=['matchId', 'groupId'])
 def max_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'matchType', 'winPlacePerc']
features = [col for col in df.columns if col not in cols_to_drop]
agg = df.groupby(['matchId', 'groupId'])[features].max()
return df.merge(agg, suffixes=['', '_max'], how='left', on=['matchId', 'groupId'])
 def sum_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'matchType', 'winPlacePerc']
features = [col for col in df.columns if col not in cols_to_drop]
agg = df.groupby(['matchId', 'groupId'])[features].sum()
return df.merge(agg, suffixes=['', '_sum'], how='left', on=['matchId', 'groupId'])
 def median_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'matchType', 'winPlacePerc']
features = [col for col in df.columns if col not in cols_to_drop]
agg = df.groupby(['matchId', 'groupId'])[features].median()
return df.merge(agg, suffixes=['', '_median'], how='left', on=['matchId', 'groupId'])
 def mean_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'matchType', 'winPlacePerc']
features = [col for col in df.columns if col not in cols_to_drop]
agg = df.groupby(['matchId', 'groupId'])[features].mean()
return df.merge(agg, suffixes=['', '_mean'], how='left', on=['matchId', 'groupId'])
 def rank_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'matchType', 'winPlacePerc']
features = [col for col in df.columns if col not in cols_to_drop]
agg = df.groupby(['matchId', 'groupId'])[features].mean()
agg = agg.groupby('matchId')[features].rank(pct=True)
 return df.merge(agg, suffixes=['', '_mean_rank'], how='left', on=['matchId', 'groupId'] )
all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None" )<sort_values>
from sklearn.model_selection import KFold, StratifiedKFold, train_test_split
from sklearn.metrics import mean_absolute_error
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head()<data_type_conversions>
def oof_model_preds(df, model, num_folds, params):
train_df = df[df['winPlacePerc'].notnull() ]
test_df = df[df['winPlacePerc'].isnull() ]
print("Starting LightGBM.Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
del df
gc.collect()
oof_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
drop_features = ['Id', 'groupId', 'matchId', 'matchType', 'winPlacePerc']
feats = [f for f in train_df.columns if f not in drop_features]
if num_folds == 1:
train_x, valid_x, train_y, valid_y = train_test_split(train_df[feats], train_df['winPlacePerc'], test_size=0.2, random_state=1001)
 model.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)],
eval_metric= 'mae', verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'])
 oof_preds = model.predict(train_df[feats])
 sub_preds = model.predict(test_df[feats])
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = model.feature_importances_
fold_importance_df["fold"] = 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('MAE : %.6f' %(mean_absolute_error(train_df['winPlacePerc'], oof_preds)))
 del train_x, train_y, valid_x, valid_y
gc.collect()
elif num_folds > 1:
folds = KFold(n_splits= num_folds, shuffle=True, random_state=1001)
 for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df[feats], train_df['winPlacePerc'])) :
train_x, train_y = train_df[feats].iloc[train_idx], train_df['winPlacePerc'].iloc[train_idx]
valid_x, valid_y = train_df[feats].iloc[valid_idx], train_df['winPlacePerc'].iloc[valid_idx]
model.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)],
eval_metric= 'mae', verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'])
 oof_preds[valid_idx] = model.predict(valid_x)
 sub_preds += model.predict(test_df[feats])/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = model.feature_importances_
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('Fold %2d MAE : %.6f' %(n_fold + 1, mean_absolute_error(valid_y, oof_preds[valid_idx])))
 del train_x, train_y, valid_x, valid_y
gc.collect()
print('Full MAE score %.6f' % mean_absolute_error(train_df['winPlacePerc'], oof_preds))
return oof_preds, sub_preds, feature_importance_df
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )<categorify>
params = {
'num_leaves': 144,
'learning_rate': 0.1,
'n_estimators': 800,
'max_depth':12,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9,
'verbose':50,
'early_stopping_rounds':100
}
lgbm_reg = lgb.LGBMRegressor(num_leaves=params['num_leaves'], learning_rate=params['learning_rate'],
n_estimators=params['n_estimators'], max_depth=params['max_depth'],
max_bin = params['max_bin'], bagging_fraction = params['bagging_fraction'],
bagging_freq = params['bagging_freq'], feature_fraction = params['feature_fraction'], 
)
 lgb_oof_preds, lgb_sub_preds, lgb_feature_importance_df = oof_model_preds(dataset, lgbm_reg, num_folds=4, params=params )
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
print('Shape all_data: {}'.format(all_data.shape))<categorify>
display_importances(lgb_feature_importance_df )
<feature_engineering><EOS> 
sub = pd.DataFrame()
sub['Id'] = test_df['Id']
sub['winPlacePerc'] = lgb_sub_preds
sub['winPlacePerc'][sub['winPlacePerc'] > 1] = 1
sub.to_csv('lgb_submission.csv',index=False )