Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
data[data.BsmtFinSF1.isna() ]<feature_engineering>
data = pd.read_csv('.. /input/train_V2.csv' )
data.loc[data.BsmtFinSF1.isna() ,'BsmtFinSF2' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtUnfSF' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtFullBath' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'TotalBsmtSF' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtHalfBath' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtFinSF1' ]=0.0
data.loc[data.BsmtQual=="NA",'BsmtExposure' ]="NA"
data.loc[data.BsmtQual=="NA",'BsmtFullBath' ]=0.0
data.loc[data.BsmtQual=="NA",'BsmtHalfBath' ]=0.0<filter>
data = data.dropna()
data.shape
data[data.Electrical.isna() ]<count_values>
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_absolute_error
data.Electrical.value_counts()<data_type_conversions>
def merge_matchType(x):
if x in {'normal-squad-fpp', 'crashfpp', 'crashtpp', 'normal-duo-fpp',
'flarefpp', 'normal-solo-fpp', 'flaretpp', 'normal-duo',
'normal-squad', 'normal-solo'}:
return 'others'
else:
return x
data[['Electrical']]=data[['Electrical']].fillna(value="SBrkr" )<feature_engineering>
data['matchType'] = data.matchType.apply(merge_matchType)
 data.matchType.unique()
data['KitchenQual']=data['KitchenQual'].fillna(value="TA" )<feature_engineering>
data_dumm = pd.get_dummies(data, columns=['matchType'])
 data_dumm.head()
data['GarageYrBlt']=data['GarageYrBlt'].fillna(data.YearBuilt )<filter>
data_dumm = data_dumm.drop('matchType_others', axis=1 )
data[data.GarageCars.isna() ]<filter>
data = data_dumm.loc[:,['Id', 'groupId', 'matchId', 'assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'killPlace', 'killPoints', 'kills',
'killStreaks', 'longestKill', 'matchDuration', 'maxPlace', 'numGroups',
'rankPoints', 'revives', 'rideDistance', 'roadKills', 'swimDistance',
'teamKills', 'vehicleDestroys', 'walkDistance', 'weaponsAcquired',
'winPoints', 'matchType_duo', 'matchType_duo-fpp',
'matchType_solo', 'matchType_solo-fpp', 'matchType_squad',
'matchType_squad-fpp', 'winPlacePerc']]
data.loc[data.GarageCars.isna() ,data.columns.str.contains('Gar')]<feature_engineering>
print('There are {} unique maxPlace.'.format(len(data.maxPlace.unique())) )
data['GarageArea']=data['GarageArea'].fillna(value=300)
 data['GarageCars']=data['GarageCars'].fillna(value=1.0 )<count_values>
data_store_by_maxPlace = {}
for x in data.maxPlace.unique() :
data_store_by_maxPlace[x] = data.loc[data.maxPlace==x]
data.Functional.value_counts()<data_type_conversions>
def adjust_pred(x, maxPlace):
space = 1/(maxPlace-1)
 return np.round(x / space)* space
data['Functional']=data['Functional'].fillna(value="Typ" )<count_values>
def generate_lgb_model(data, for_eval):
X = data.iloc[:,3:33]
maxPlace = X.maxPlace.unique()
X = X.drop('maxPlace', axis=1)
 Y = data.iloc[:,33]
if for_eval == True:
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
 else:
X_train, y_train = X, Y
d_train = lgb.Dataset(X_train, label=y_train)
 params = {}
params['objective'] = 'regression'
params['metric'] = 'mae'
model = lgb.train(params, d_train)
 if for_eval == True:
y_pred=model.predict(X_test)
 y_pred = adjust_pred(y_pred, maxPlace)
 mae = mean_absolute_error(y_test, y_pred)
 print("MAE for maxPlace = {}: {}".format(maxPlace, mae))
return [model,(list(y_test), y_pred)]
else:
return model
data.SaleType.value_counts()<feature_engineering>
model_store = {}
maxPlace_set = set(data_store_by_maxPlace.keys())
 print('There are {} unique maxPlace.'.format(len(maxPlace_set)))
 for key, value in data_store_by_maxPlace.items() :
model_store[key] = generate_lgb_model(value,for_eval=True)
 maxPlace_set = maxPlace_set - {key}
print('There are {} more models to go.'.format(len(maxPlace_set)) )
data['SaleType']=data['SaleType'].fillna(value="WD" )<prepare_x_and_y>
y_test_overall = []
y_pred_overall = []
for value in model_store.values() :
y_test_overall.append(value[1][0])
 y_pred_overall.append(value[1][1] )
X = data.drop(['SalePrice', 'tmpPrice','MasVnrType','MasVnrArea'],axis=1)
 X = X.reset_index(level=0, drop=True)
 X = pd.get_dummies(X)
 Y = data[['MasVnrType']]
Y = Y.reset_index(level=0, drop=True)
 Y_source = Y.dropna()
X_source = X.loc[Y_source.index,:]
X_target = X.loc[set(X.index)- set(Y_source.index),:]<categorify>
print('The overall mae is {}.'.format(mean_absolute_error([y for x in y_test_overall for y in x],[y for x in y_pred_overall for y in x])) )
le = preprocessing.LabelEncoder()
le.fit(Y_source.values )<categorify>
model_store_full = {}
maxPlace_set = set(data_store_by_maxPlace.keys())
 print('There are {} unique maxPlace.'.format(len(maxPlace_set)))
 for key, value in data_store_by_maxPlace.items() :
model_store_full[key] = generate_lgb_model(value,for_eval=False)
 maxPlace_set = maxPlace_set - {key}
print('There are {} more models to go.'.format(len(maxPlace_set)) )
Y_source = le.transform(Y_source )<compute_train_metric>
X_submit = pd.read_csv('.. /input/test_V2.csv')
 print(X_submit.shape)
 X_submit.head()
clf = RandomForestClassifier(n_estimators=300, max_depth=None,min_samples_split=2, random_state=0)
 scores = cross_val_score(clf, X_source, Y_source, cv=5)
 scores.mean() 
<train_model>
X_submit['matchType'] = X_submit.matchType.apply(merge_matchType)
 X_submit.matchType.unique()
X_submit_dumm = pd.get_dummies(X_submit, columns=['matchType'])
 X_submit_dumm.head()
X_submit_dumm = X_submit_dumm.drop('matchType_others', axis=1)
 X_submit = X_submit_dumm.loc[:,['Id', 'groupId', 'matchId', 'assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'killPlace', 'killPoints', 'kills',
'killStreaks', 'longestKill', 'matchDuration', 'maxPlace', 'numGroups',
'rankPoints', 'revives', 'rideDistance', 'roadKills', 'swimDistance',
'teamKills', 'vehicleDestroys', 'walkDistance', 'weaponsAcquired',
'winPoints', 'matchType_duo', 'matchType_duo-fpp',
'matchType_solo', 'matchType_solo-fpp', 'matchType_squad',
'matchType_squad-fpp']]
X_submit.head()
_=clf.fit(X_source, Y_source )<prepare_output>
data_store_by_maxPlace_submit = {}
for x in X_submit.maxPlace.unique() :
data_store_by_maxPlace_submit[x] = X_submit.loc[X_submit.maxPlace==x]
Y_target = pd.DataFrame(le.inverse_transform(clf.predict(X_target)) ).set_index(X_target.index )<rename_columns>
prediction = {}
for key, value in data_store_by_maxPlace_submit.items() :
maxPlace = key
train_data = value.iloc[:,3:33]
train_data = train_data.drop('maxPlace', axis=1)
 pred_submit = model_store_full[key].predict(train_data)
 pred_submit = adjust_pred(pred_submit, maxPlace)
 prediction[key] = pd.concat([value.Id.reset_index(drop=True), pd.Series(pred_submit, name='winPlacePerc')], axis=1 )
Y_target.columns = ['MasVnrType']<prepare_x_and_y>
submission = pd.concat([x for x in prediction.values() ])
 submission.head()
X = data.drop(['SalePrice', 'tmpPrice','MasVnrArea'],axis=1)
 X = X.reset_index(level=0, drop=True)
 X = pd.get_dummies(X)
 Y = data[['MasVnrArea']]
Y = Y.reset_index(level=0, drop=True)
 Y_source = Y.dropna()
X_source = X.loc[Y_source.index,:]
X_target = X.loc[set(X.index)- set(Y_source.index),:]<compute_train_metric>
submission.to_csv('submission.csv', index=False )
<train_model><EOS> 
submission.to_csv('submission.csv', index=False )