Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<set_options>
debug = False;
debug_rows = 10000;
gc.enable()
pd.options.display.float_format = '{:,.2f}'.format
pd.set_option('display.max_columns', 100)
 %matplotlib inline
pd.set_option('display.max_columns', None )<load_from_csv>
if(debug):
train = pd.read_csv('.. /input/train_V2.csv', nrows = debug_rows)
 test = pd.read_csv('.. /input/test_V2.csv')
 else:
train = pd.read_csv('.. /input/train_V2.csv', nrows = debug_rows)
 test = pd.read_csv('.. /input/test_V2.csv' )
mydata = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 mydata.drop(mydata[(mydata['OverallQual']<5)&(mydata['SalePrice']>200000)].index, inplace=True)
 mydata.drop(mydata[(mydata['GrLivArea']>4000)&(mydata['SalePrice']<300000)].index, inplace=True)
 mydata.reset_index(drop=True, inplace=True)
 mydata['MSSubClass'] = mydata['MSSubClass'].apply(str)
 mydata['YrSold'] = mydata['YrSold'].astype(str)
 mydata['MoSold'] = mydata['MoSold'].astype(str)
<categorify>
train.isnull().sum()
<categorify>
train.isnull().sum()
def fill_missings(res):
res['Alley'] = res['Alley'].fillna('missing')
 res['PoolQC'] = res['PoolQC'].fillna(res['PoolQC'].mode() [0])
 res['MasVnrType'] = res['MasVnrType'].fillna('None')
 res['BsmtQual'] = res['BsmtQual'].fillna(res['BsmtQual'].mode() [0])
 res['BsmtCond'] = res['BsmtCond'].fillna(res['BsmtCond'].mode() [0])
 res['FireplaceQu'] = res['FireplaceQu'].fillna(res['FireplaceQu'].mode() [0])
 res['GarageType'] = res['GarageType'].fillna('missing')
 res['GarageFinish'] = res['GarageFinish'].fillna(res['GarageFinish'].mode() [0])
 res['GarageQual'] = res['GarageQual'].fillna(res['GarageQual'].mode() [0])
 res['GarageCond'] = res['GarageCond'].fillna('missing')
 res['Fence'] = res['Fence'].fillna('missing')
 res['Street'] = res['Street'].fillna('missing')
 res['LotShape'] = res['LotShape'].fillna('missing')
 res['LandContour'] = res['LandContour'].fillna('missing')
 res['BsmtExposure'] = res['BsmtExposure'].fillna(res['BsmtExposure'].mode() [0])
 res['BsmtFinType1'] = res['BsmtFinType1'].fillna('missing')
 res['BsmtFinType2'] = res['BsmtFinType2'].fillna('missing')
 res['CentralAir'] = res['CentralAir'].fillna('missing')
 res['Electrical'] = res['Electrical'].fillna(res['Electrical'].mode() [0])
 res['MiscFeature'] = res['MiscFeature'].fillna('missing')
 res['MSZoning'] = res['MSZoning'].fillna(res['MSZoning'].mode() [0])
 res['Utilities'] = res['Utilities'].fillna('missing')
 res['Exterior1st'] = res['Exterior1st'].fillna(res['Exterior1st'].mode() [0])
 res['Exterior2nd'] = res['Exterior2nd'].fillna(res['Exterior2nd'].mode() [0])
 res['KitchenQual'] = res['KitchenQual'].fillna(res['KitchenQual'].mode() [0])
 res["Functional"] = res["Functional"].fillna("Typ")
 res['SaleType'] = res['SaleType'].fillna(res['SaleType'].mode() [0])
 res['SaleCondition'] = res['SaleCondition'].fillna('missing')
 flist = ['LotFrontage','LotArea','MasVnrArea','BsmtFinSF1','BsmtFinSF2','BsmtUnfSF',
'TotalBsmtSF','1stFlrSF','2ndFlrSF','LowQualFinSF','GrLivArea',
'BsmtFullBath','BsmtHalfBath','FullBath','HalfBath','BedroomAbvGr','KitchenAbvGr',
'TotRmsAbvGrd','Fireplaces','GarageCars','GarageArea','WoodDeckSF','OpenPorchSF',
'EnclosedPorch','3SsnPorch','ScreenPorch','PoolArea','MiscVal']
for fl in flist:
res[fl] = res[fl].fillna(0)
 res['TotalBsmtSF'] = res['TotalBsmtSF'].apply(lambda x: np.exp(6)if x <= 0.0 else x)
 res['2ndFlrSF'] = res['2ndFlrSF'].apply(lambda x: np.exp(6.5)if x <= 0.0 else x)
 res['GarageArea'] = res['GarageArea'].apply(lambda x: np.exp(6)if x <= 0.0 else x)
 res['GarageCars'] = res['GarageCars'].apply(lambda x: 0 if x <= 0.0 else x)
 res['LotFrontage'] = res['LotFrontage'].apply(lambda x: np.exp(4.2)if x <= 0.0 else x)
 res['MasVnrArea'] = res['MasVnrArea'].apply(lambda x: np.exp(4)if x <= 0.0 else x)
 res['BsmtFinSF1'] = res['BsmtFinSF1'].apply(lambda x: np.exp(6.5)if x <= 0.0 else x)
 return res<categorify>
train.dropna(axis=0, how='all')
 print(train.isnull().any().any() )
mydata = fill_missings(mydata)
 mydata['TotalSF'] = mydata['TotalBsmtSF'] + mydata['1stFlrSF'] + mydata['2ndFlrSF']
def QualToInt(x):
if(x=='Ex'):
r = 0
elif(x=='Gd'):
r = 1
elif(x=='TA'):
r = 2
elif(x=='Fa'):
r = 3
elif(x=='missing'):
r = 4
else:
r = 5
return r
mydata['ExterQual'] = mydata['ExterQual'].apply(QualToInt)
 mydata['ExterCond'] = mydata['ExterCond'].apply(QualToInt)
 mydata['KitchenQual'] = mydata['KitchenQual'].apply(QualToInt)
 mydata['HeatingQC'] = mydata['HeatingQC'].apply(QualToInt)
 mydata['BsmtQual'] = mydata['BsmtQual'].apply(QualToInt)
 mydata['BsmtCond'] = mydata['BsmtCond'].apply(QualToInt)
 mydata['FireplaceQu'] = mydata['FireplaceQu'].apply(QualToInt)
 mydata['GarageQual'] = mydata['GarageQual'].apply(QualToInt)
 mydata['PoolQC'] = mydata['PoolQC'].apply(QualToInt)
 def SlopeToInt(x):
if(x=='Gtl'):
r = 0
elif(x=='Mod'):
r = 1
elif(x=='Sev'):
r = 2
else:
r = 3
return r
mydata['LandSlope'] = mydata['LandSlope'].apply(SlopeToInt)
 mydata['CentralAir'] = mydata['CentralAir'].apply(lambda x: 0 if x == 'N' else 1)
 mydata['Street'] = mydata['Street'].apply(lambda x: 0 if x == 'Pave' else 1)
 mydata['PavedDrive'] = mydata['PavedDrive'].apply(lambda x: 0 if x == 'Y' else 1)
 def GFinishToInt(x):
if(x=='Fin'):
r = 0
elif(x=='RFn'):
r = 1
elif(x=='Unf'):
r = 2
else:
r = 3
return r
mydata['GarageFinish'] = mydata['GarageFinish'].apply(GFinishToInt)
 def BsmtExposureToInt(x):
if(x=='Gd'):
r = 0
elif(x=='Av'):
r = 1
elif(x=='Mn'):
r = 2
elif(x=='No'):
r = 3
else:
r = 4
return r
mydata['BsmtExposure'] = mydata['BsmtExposure'].apply(BsmtExposureToInt)
 def FunctionalToInt(x):
if(x=='Typ'):
r = 0
elif(x=='Min1'):
r = 1
elif(x=='Min2'):
r = 1
else:
r = 2
return r
mydata['Functional_int'] = mydata['Functional'].apply(FunctionalToInt)
 def HouseStyleToInt(x):
if(x=='1.5Unf'):
r = 0
elif(x=='SFoyer'):
r = 1
elif(x=='1.5Fin'):
r = 2
elif(x=='2.5Unf'):
r = 3
elif(x=='SLvl'):
r = 4
elif(x=='1Story'):
r = 5
elif(x=='2Story'):
r = 6
elif(x==' 2.5Fin'):
r = 7
else:
r = 8
return r
mydata['HouseStyle_int'] = mydata['HouseStyle'].apply(HouseStyleToInt)
 mydata['HouseStyle_1st'] = 1*(mydata['HouseStyle'] == '1Story')
 mydata['HouseStyle_2st'] = 1*(mydata['HouseStyle'] == '2Story')
 mydata['HouseStyle_15st'] = 1*(mydata['HouseStyle'] == '1.5Fin')
 def FoundationToInt(x):
if(x=='PConc'):
r = 3
elif(x=='CBlock'):
r = 2
elif(x=='BrkTil'):
r = 1
else:
r = 0
return r
mydata['Foundation_int'] = mydata['Foundation'].apply(FoundationToInt)
 def MasVnrTypeToInt(x):
if(x=='Stone'):
r = 3
elif(x=='BrkFace'):
r = 2
elif(x=='BrkCmn'):
r = 1
else:
r = 0
return r
mydata['MasVnrType_int'] = mydata['MasVnrType'].apply(MasVnrTypeToInt)
 def BsmtFinType1ToInt(x):
if(x=='GLQ'):
r = 6
elif(x=='ALQ'):
r = 5
elif(x=='BLQ'):
r = 4
elif(x=='Rec'):
r = 3
elif(x=='LwQ'):
r = 2
elif(x=='Unf'):
r = 1
else:
r = 0
return r
mydata['BsmtFinType1_int'] = mydata['BsmtFinType1'].apply(BsmtFinType1ToInt)
 mydata['BsmtFinType1_Unf'] = 1*(mydata['BsmtFinType1'] == 'Unf')
 mydata['HasWoodDeck'] =(mydata['WoodDeckSF'] == 0)* 1
mydata['HasOpenPorch'] =(mydata['OpenPorchSF'] == 0)* 1
mydata['HasEnclosedPorch'] =(mydata['EnclosedPorch'] == 0)* 1
mydata['Has3SsnPorch'] =(mydata['3SsnPorch'] == 0)* 1
mydata['HasScreenPorch'] =(mydata['ScreenPorch'] == 0)* 1
mydata['YearsSinceRemodel'] = mydata['YrSold'].astype(int)- mydata['YearRemodAdd'].astype(int)
 mydata['Total_Home_Quality'] = mydata['OverallQual'] + mydata['OverallCond'] 
<drop_column>
assists[assists>10].count()
def addlogs(res, ls):
m = res.shape[1]
for l in ls:
res = res.assign(newcol=pd.Series(np.log(1.01+res[l])).values)
 res.columns.values[m] = l + '_log'
m += 1
return res
loglist = ['LotFrontage','LotArea','MasVnrArea','BsmtFinSF1','BsmtFinSF2','BsmtUnfSF',
'TotalBsmtSF','1stFlrSF','2ndFlrSF','LowQualFinSF','GrLivArea',
'BsmtFullBath','BsmtHalfBath','FullBath','HalfBath','BedroomAbvGr','KitchenAbvGr',
'TotRmsAbvGrd','Fireplaces','GarageCars','GarageArea','WoodDeckSF','OpenPorchSF',
'EnclosedPorch','3SsnPorch','ScreenPorch','PoolArea','MiscVal','YearRemodAdd','TotalSF']
mydata = addlogs(mydata, loglist )<train_model>
eda[eda>15].count()
<categorify>
eda[eda>10].count()
def getdummies(res, ls):
def encode(encode_df):
encode_df = np.array(encode_df)
 enc = OneHotEncoder()
le = LabelEncoder()
le.fit(encode_df)
 res1 = le.transform(encode_df ).reshape(-1, 1)
 enc.fit(res1)
 return pd.DataFrame(enc.transform(res1 ).toarray()), le, enc
decoder = []
outres = pd.DataFrame({'A' : []})
 for l in ls:
cat, le, enc = encode(res[l])
 cat.columns = [l+str(x)for x in cat.columns]
outres.reset_index(drop=True, inplace=True)
 outres = pd.concat([outres, cat], axis = 1)
 decoder.append([le,enc])
 return(outres, decoder)
 catpredlist = ['MSSubClass','MSZoning','LotShape','LandContour','LotConfig',
'Neighborhood','Condition1','Condition2','BldgType',
'RoofStyle','RoofMatl','Exterior1st','Exterior2nd',
'BsmtFinType2','Heating','HouseStyle','Foundation','MasVnrType','BsmtFinType1',
'Electrical','Functional','GarageType','Alley','Utilities',
'GarageCond','Fence','MiscFeature','SaleType','SaleCondition','LandSlope','CentralAir',
'GarageFinish','BsmtExposure','Street']
res = getdummies(mydata[catpredlist],catpredlist)
 df = res[0]
decoder = res[1]
floatpredlist = ['LotFrontage_log',
'LotArea_log',
'MasVnrArea_log','BsmtFinSF1_log','BsmtFinSF2_log','BsmtUnfSF_log',
'TotalBsmtSF_log','1stFlrSF_log','2ndFlrSF_log','LowQualFinSF_log','GrLivArea_log',
'BsmtFullBath_log','BsmtHalfBath_log','FullBath_log','HalfBath_log','BedroomAbvGr_log','KitchenAbvGr_log',
'TotRmsAbvGrd_log','Fireplaces_log','GarageCars_log','GarageArea_log',
'PoolArea_log','MiscVal_log',
'YearRemodAdd','TotalSF_log','OverallQual','OverallCond','ExterQual','ExterCond','KitchenQual',
'HeatingQC','BsmtQual','BsmtCond','FireplaceQu','GarageQual','PoolQC','PavedDrive',
'HasWoodDeck', 'HasOpenPorch','HasEnclosedPorch', 'Has3SsnPorch', 'HasScreenPorch']
df = pd.concat([df,mydata[floatpredlist]],axis=1)
<train_model>
train[train['walkDistance']>10000]
<concatenate>
train[train['headshotKills']>10]
def addSquared(res, ls):
m = res.shape[1]
for l in ls:
res = res.assign(newcol=pd.Series(res[l]*res[l] ).values)
 res.columns.values[m] = l + '_sq'
m += 1
return res
sqpredlist = ['YearRemodAdd', 'LotFrontage_log',
'TotalBsmtSF_log', '1stFlrSF_log', '2ndFlrSF_log', 'GrLivArea_log',
'GarageCars_log', 'GarageArea_log',
'OverallQual','ExterQual','BsmtQual','GarageQual','FireplaceQu','KitchenQual']
df = addSquared(df, sqpredlist)
<categorify>
train[(train['rideDistance']==0)&(train['roadKills']>0)]
<prepare_x_and_y>
train[train['teamKills']>3]
X = np.array(df)
 X = np.delete(X, 0, axis=1)
 y = np.log(1+np.array(mydata['SalePrice']))<split>
df = train
df = df.drop(df[(df['walkDistance']<10.0)&(df['damageDealt']>0)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['kills']>10)].index)
 df = df.drop(df[(df['walkDistance']<100.0)&(df['weaponsAcquired']>30)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['heals']>100)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['headshotKills']>5)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['headshotKills']>5)].index)
 df['unrated_kill'] = 0
df.loc[df['killPoints']==0, 'unrated_kill']=1
df['poor_kills'] = 0
df.loc[(df['killPoints']>10)&(df['killPoints']<800), 'poor_kills'] = 1
df.loc[(df['killPoints']>10)&(df['killPoints']<800), 'killPoints'] = 0
df['killPerDamage'] = df['kills']/df['damageDealt']
df = df.fillna(0)
 df = df.drop(df[df['killStreaks']>=10].index)
 df['unrated_rank'] = 0
df.loc[df['rankPoints']==-1, 'unrated_rank']=1
df = df.drop(df[(df['rideDistance']==0.0)&(df['roadKills']>0)].index)
 df = df.drop(df[(df['weaponsAcquired']>30)&(df['walkDistance']<100)].index)
 df['unrated_win'] = 0
df.loc[(df['winPoints']==-1)|(df['winPoints'] == 0), 'unrated_win']=1
df['poor_wins'] = 0
df.loc[(df['winPoints']>250)&(df['winPoints']<1200), 'poor_wins'] = 1
df.loc[(df['winPoints']>250)&(df['winPoints']<1200), 'killPoints'] = 0
print('removed:' + str(train['Id'].count() - df['Id'].count()))
df.head()
nF = 20
kf = KFold(n_splits=nF, random_state=241, shuffle=True)
 test_errors_l2 = []
train_errors_l2 = []
test_errors_l1 = []
train_errors_l1 = []
test_errors_GBR = []
train_errors_GBR = []
test_errors_ENet = []
test_errors_LGB = []
test_errors_stack = []
test_errors_ens = []
train_errors_ens = []
models = []
pred_all = []
ifold = 1
for train_index, test_index in kf.split(X):
print('fold: ',ifold)
 ifold = ifold + 1
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
l2Regr = Ridge(alpha=9.0, fit_intercept = True)
 l2Regr.fit(X_train, y_train)
 pred_train_l2 = l2Regr.predict(X_train)
 pred_test_l2 = l2Regr.predict(X_test)
 l1Regr = make_pipeline(RobustScaler() , Lasso(alpha = 0.0003, random_state=1, max_iter=50000))
l1Regr.fit(X_train, y_train)
 pred_train_l1 = l1Regr.predict(X_train)
 pred_test_l1 = l1Regr.predict(X_test)
 myGBR = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.02,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=50,
loss='huber', random_state = 5)
 myGBR.fit(X_train,y_train)
 pred_train_GBR = myGBR.predict(X_train)
 pred_test_GBR = myGBR.predict(X_test)
 ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=4.0, l1_ratio=0.005, random_state=3))
ENet.fit(X_train, y_train)
 pred_train_ENet = ENet.predict(X_train)
 pred_test_ENet = ENet.predict(X_test)
 myLGB = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=600,
max_bin = 50, bagging_fraction = 0.6,
bagging_freq = 5, feature_fraction = 0.25,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf = 6, min_sum_hessian_in_leaf = 11)
 myLGB.fit(X_train, y_train)
 pred_train_LGB = myLGB.predict(X_train)
 pred_test_LGB = myLGB.predict(X_test)
 stackedset = pd.DataFrame({'A' : []})
 stackedset = pd.concat([stackedset,pd.DataFrame(pred_test_l2)],axis=1)
 stackedset = pd.concat([stackedset,pd.DataFrame(pred_test_l1)],axis=1)
 stackedset = pd.concat([stackedset,pd.DataFrame(pred_test_GBR)],axis=1)
 stackedset = pd.concat([stackedset,pd.DataFrame(pred_test_ENet)],axis=1)
 stackedset = pd.concat([stackedset,pd.DataFrame(pred_test_LGB)],axis=1)
 prod =(pred_test_l2*pred_test_l1*pred_test_GBR*pred_test_ENet*pred_test_LGB)**(1.0/5.0)
 stackedset = pd.concat([stackedset,pd.DataFrame(prod)],axis=1)
 Xstack = np.array(stackedset)
 Xstack = np.delete(Xstack, 0, axis=1)
 l1_staked = Lasso(alpha = 0.0001,fit_intercept = True)
 l1_staked.fit(Xstack, y_test)
 pred_test_stack = l1_staked.predict(Xstack)
 models.append([l2Regr,l1Regr,myGBR,ENet,myLGB,l1_staked])
 test_errors_l2.append(np.square(pred_test_l2 - y_test ).mean() ** 0.5)
 test_errors_l1.append(np.square(pred_test_l1 - y_test ).mean() ** 0.5)
 test_errors_GBR.append(np.square(pred_test_GBR - y_test ).mean() ** 0.5)
 test_errors_ENet.append(np.square(pred_test_ENet - y_test ).mean() ** 0.5)
 test_errors_LGB.append(np.square(pred_test_LGB - y_test ).mean() ** 0.5)
 test_errors_stack.append(np.square(pred_test_stack - y_test ).mean() ** 0.5)
<compute_test_metric>
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=1000000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 if debug == True:
df = pd.read_csv('.. /input/test_V2.csv')
 else:
df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
print("remove some columns")
 target = 'winPlacePerc'
if(is_train):
print("removing cheaters")
 df = df.drop(df[(df['walkDistance']<10.0)&(df['damageDealt']>0)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['kills']>10)].index)
 df = df.drop(df[(df['walkDistance']<100.0)&(df['weaponsAcquired']>30)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['heals']>100)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['headshotKills']>5)].index)
 df = df.drop(df[(df['walkDistance']<10.0)&(df['headshotKills']>5)].index)
 df = df.drop(df[df['killStreaks']>=10].index)
 df = df.drop(df[(df['rideDistance']==0.0)&(df['roadKills']>0)].index)
 df = df.drop(df[(df['weaponsAcquired']>30)&(df['walkDistance']<100)].index)
 df['unrated_kill'] = 0
df.loc[df['killPoints']==0, 'unrated_kill']=1
df['poor_kills'] = 0
df.loc[(df['killPoints']>10)&(df['killPoints']<800), 'poor_kills'] = 1
df.loc[(df['killPoints']>10)&(df['killPoints']<800), 'killPoints'] = 0
df['unrated_rank'] = 0
df.loc[df['rankPoints']==-1, 'unrated_rank']=1
df['unrated_win'] = 0
df.loc[(df['winPoints']==-1)|(df['winPoints'] == 0), 'unrated_win']=1
df['poor_wins'] = 0
df.loc[(df['winPoints']>250)&(df['winPoints']<1200), 'poor_wins'] = 1
df.loc[(df['winPoints']>250)&(df['winPoints']<1200), 'killPoints'] = 0
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
print("Removing Na's From DF")
 df.fillna(0, inplace=True)
 features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 print("Adding Features")
 df['headshotrate'] = df['kills']/df['headshotKills']
df['killStreakrate'] = df['killStreaks']/df['kills']
df['healthitems'] = df['heals'] + df['boosts']
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['distance_over_weapons'] = df['totalDistance'] / df['weaponsAcquired']
df['walkDistance_over_heals'] = df['walkDistance'] / df['heals']
df['walkDistance_over_kills'] = df['walkDistance'] / df['kills']
df['killsPerWalkDistance'] = df['kills'] / df['walkDistance']
df["skill"] = df["headshotKills"] + df["roadKills"]
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
print("Removing Na's From DF")
 df.fillna(0, inplace=True)
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
print(np.mean(test_errors_l2))
print(np.mean(test_errors_l1))
print(np.mean(test_errors_GBR))
print(np.mean(test_errors_ENet))
print(np.mean(test_errors_LGB))
print(np.mean(test_errors_stack))<load_from_csv>
x_train, y_train, train_columns, _ = feature_engineering(True, debug=debug)
 x_test, _, _ , test_idx = feature_engineering(False, debug=debug)
scoredata = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 scoredata['MSSubClass'] = scoredata['MSSubClass'].apply(str)
 scoredata['YrSold'] = scoredata['YrSold'].astype(str)
 scoredata['MoSold'] = scoredata['MoSold'].astype(str)
<data_type_conversions>
gc.collect() ;
train_index = round(int(x_train.shape[0]*0.8))
dev_X = x_train[:train_index]
val_X = x_train[train_index:]
dev_y = y_train[:train_index]
val_y = y_train[train_index:]
gc.collect() ;
def run_lgb(train_X, train_y, val_X, val_y, x_test):
params = {"objective" : "regression", "metric" : "mae", 'n_estimators':20000, 'early_stopping_rounds':200,
"num_leaves" : 31, "learning_rate" : 0.05, "bagging_fraction" : 0.7,
"bagging_seed" : 0, "num_threads" : 4,"colsample_bytree" : 0.7
}
lgtrain = lgb.Dataset(train_X, label=train_y)
 lgval = lgb.Dataset(val_X, label=val_y)
 model = lgb.train(params, lgtrain, valid_sets=[lgtrain, lgval], early_stopping_rounds=200, verbose_eval=1000)
 pred_test_y = model.predict(x_test, num_iteration=model.best_iteration)
 return pred_test_y, model
pred_test, model = run_lgb(dev_X, dev_y, val_X, val_y, x_test)
 pred_test
scoredata = fill_missings(scoredata)
 scoredata['ExterQual'] = scoredata['ExterQual'].apply(QualToInt)
 scoredata['ExterCond'] = scoredata['ExterCond'].apply(QualToInt)
 scoredata['KitchenQual'] = scoredata['KitchenQual'].apply(QualToInt)
 scoredata['HeatingQC'] = scoredata['HeatingQC'].apply(QualToInt)
 scoredata['BsmtQual'] = scoredata['BsmtQual'].apply(QualToInt)
 scoredata['BsmtCond'] = scoredata['BsmtCond'].apply(QualToInt)
 scoredata['FireplaceQu'] = scoredata['FireplaceQu'].apply(QualToInt)
 scoredata['GarageQual'] = scoredata['GarageQual'].apply(QualToInt)
 scoredata['PoolQC'] = scoredata['PoolQC'].apply(QualToInt)
 scoredata['LandSlope'] = scoredata['LandSlope'].apply(SlopeToInt)
 scoredata['CentralAir'] = scoredata['CentralAir'].apply(lambda x: 0 if x == 'N' else 1)
 scoredata['Street'] = scoredata['Street'].apply(lambda x: 0 if x == 'Grvl' else 1)
 scoredata['GarageFinish'] = scoredata['GarageFinish'].apply(GFinishToInt)
 scoredata['BsmtExposure'] = scoredata['BsmtExposure'].apply(BsmtExposureToInt)
 scoredata['TotalSF'] = scoredata['TotalBsmtSF'] + scoredata['1stFlrSF'] + scoredata['2ndFlrSF']
scoredata['TotalSF'] = scoredata['TotalSF'].fillna(0)
 scoredata['Functional_int'] = scoredata['Functional'].apply(FunctionalToInt)
 scoredata['HouseStyle_int'] = scoredata['HouseStyle'].apply(HouseStyleToInt)
 scoredata['HouseStyle_1st'] = 1*(scoredata['HouseStyle'] == '1Story')
 scoredata['HouseStyle_2st'] = 1*(scoredata['HouseStyle'] == '2Story')
 scoredata['HouseStyle_15st'] = 1*(scoredata['HouseStyle'] == '1.5Fin')
 scoredata['Foundation_int'] = scoredata['Foundation'].apply(FoundationToInt)
 scoredata['MasVnrType_int'] = scoredata['MasVnrType'].apply(MasVnrTypeToInt)
 scoredata['BsmtFinType1_int'] = scoredata['BsmtFinType1'].apply(BsmtFinType1ToInt)
 scoredata['BsmtFinType1_Unf'] = 1*(scoredata['BsmtFinType1'] == 'Unf')
 scoredata['PavedDrive'] = scoredata['PavedDrive'].apply(lambda x: 0 if x == 'Y' else 1)
 scoredata['HasWoodDeck'] =(scoredata['WoodDeckSF'] == 0)* 1
scoredata['HasOpenPorch'] =(scoredata['OpenPorchSF'] == 0)* 1
scoredata['HasEnclosedPorch'] =(scoredata['EnclosedPorch'] == 0)* 1
scoredata['Has3SsnPorch'] =(scoredata['3SsnPorch'] == 0)* 1
scoredata['HasScreenPorch'] =(scoredata['ScreenPorch'] == 0)* 1
scoredata['Total_Home_Quality'] = scoredata['OverallQual'] + scoredata['OverallCond'] 
<feature_engineering>
print(pred_test.shape[0])
 pred_test
df_sub = pd.read_csv(".. /input/sample_submission_V2.csv")
 df_sub['winPlacePerc'] = pred_test
df_sub.head()
scoredata['MSSubClass'] = scoredata['MSSubClass'].apply(lambda x: '20' if x == '150' else x)
 scoredata['MSZoning'] = scoredata['MSZoning'].apply(lambda x: 'RL' if x == 'missing' else x)
 scoredata['Utilities'] = scoredata['Utilities'].apply(lambda x: 'AllPub' if x == 'missing' else x)
 scoredata['Exterior1st'] = scoredata['Exterior1st'].apply(lambda x: 'VinylSd' if x == 'missing' else x)
 scoredata['Exterior2nd'] = scoredata['Exterior2nd'].apply(lambda x: 'VinylSd' if x == 'missing' else x)
 scoredata['Functional'] = scoredata['Functional'].apply(lambda x: 'Typ' if x == 'missing' else x)
 scoredata['SaleType'] = scoredata['SaleType'].apply(lambda x: 'WD' if x == 'missing' else x)
 scoredata['SaleCondition'] = scoredata['SaleCondition'].apply(lambda x: 'Normal' if x == 'missing' else x)
<feature_engineering>
if(debug):
df_sub = pd.read_csv(".. /input/sample_submission_V2.csv", nrows=pred_test.shape[0])
 df_test = pd.read_csv(".. /input/test_V2.csv", nrows=pred_test.shape[0])
 else:
df_sub = pd.read_csv(".. /input/sample_submission_V2.csv")
 df_test = pd.read_csv(".. /input/test_V2.csv")
 df_sub['winPlacePerc'] = pred_test
df_sub = df_sub.merge(df_test[["Id", "matchId", "groupId", "maxPlace", "numGroups"]], on="Id", how="left")
 df_sub_group = df_sub.groupby(["matchId", "groupId"] ).first().reset_index()
df_sub_group["rank"] = df_sub_group.groupby(["matchId"])["winPlacePerc"].rank()
df_sub_group = df_sub_group.merge(
 df_sub_group.groupby("matchId")["rank"].max().to_frame("max_rank" ).reset_index() ,
on="matchId", how="left")
 df_sub_group["adjusted_perc"] =(df_sub_group["rank"] - 1)/(df_sub_group["numGroups"] - 1)
 df_sub = df_sub.merge(df_sub_group[["adjusted_perc", "matchId", "groupId"]], on=["matchId", "groupId"], how="left")
 df_sub["winPlacePerc"] = df_sub["adjusted_perc"]
df_sub.loc[df_sub.maxPlace == 0, "winPlacePerc"] = 0
df_sub.loc[df_sub.maxPlace == 1, "winPlacePerc"] = 1
subset = df_sub.loc[df_sub.maxPlace > 1]
gap = 1.0 /(subset.maxPlace.values - 1)
 new_perc = np.around(subset.winPlacePerc.values / gap)* gap
df_sub.loc[df_sub.maxPlace > 1, "winPlacePerc"] = new_perc
df_sub.loc[(df_sub.maxPlace > 1)&(df_sub.numGroups == 1), "winPlacePerc"] = 0
assert df_sub["winPlacePerc"].isnull().sum() == 0
df_sub[["Id", "winPlacePerc"]].to_csv("submission_adjusted.csv", index=False)
 df_sub
scoredata = addlogs(scoredata, loglist )<train_model>
<categorify>
def getdummies_transform(res, ls, decoder):
def encode(encode_df, le_df, enc_df):
encode_df = np.array(encode_df)
 res1 = le_df.transform(encode_df ).reshape(-1, 1)
 return pd.DataFrame(enc_df.transform(res1 ).toarray())
 L = len(ls)
 outres = pd.DataFrame({'A' : []})
 for j in range(L):
l = ls[j]
le = decoder[j][0]
enc = decoder[j][1]
cat = encode(res[l], le, enc)
 cat.columns = [l+str(x)for x in cat.columns]
outres.reset_index(drop=True, inplace=True)
 outres = pd.concat([outres, cat], axis = 1)
 return outres
df_scores = getdummies_transform(scoredata, catpredlist, decoder)
 df_scores = pd.concat([df_scores,scoredata[floatpredlist]],axis=1)
 df_scores = addSquared(df_scores, sqpredlist)
<data_type_conversions>
<compute_test_metric>
<predict_on_test><EOS>