Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<count_values>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from math import sqrt
data.Functional.value_counts()<data_type_conversions>
pubg_train = pd.read_csv('.. /input/train_V2.csv')
 pubg_test = pd.read_csv('.. /input/test_V2.csv')
data['Functional']=data['Functional'].fillna(value="Typ" )<count_values>
pubg_train['matchType'].value_counts()
data.SaleType.value_counts()<feature_engineering>
le = LabelEncoder()
pubg_train['matchType'] = le.fit_transform(pubg_train['matchType'])
 pubg_test['matchType'] = le.fit_transform(pubg_test['matchType'] )
data['SaleType']=data['SaleType'].fillna(value="WD" )<prepare_x_and_y>
pubg_train['Id']= pd.factorize(pubg_train.Id)[0]
pubg_train['groupId']= pd.factorize(pubg_train.groupId)[0]
pubg_train['matchId']= pd.factorize(pubg_train.matchId)[0]
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>
submission = pubg_test.loc[:,['Id']]
le = preprocessing.LabelEncoder()
le.fit(Y_source.values )<categorify>
pubg_test['Id']= pd.factorize(pubg_test.Id)[0]
pubg_test['groupId']= pd.factorize(pubg_test.groupId)[0]
pubg_test['matchId']= pd.factorize(pubg_test.matchId)[0]
Y_source = le.transform(Y_source )<compute_train_metric>
dtype_list=[]
for col in pubg_train.columns:
dtype_list.append(pubg_train[col].dtypes)
 dtype_list=list(set(dtype_list))
print("Total Datatypes present: %s "%dtype_list)
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>
for dtype in dtype_list:
if 'int' in str(dtype):
print("Analyse %s"%str(dtype))
df_int=pubg_train.select_dtypes(include=[str(dtype)])
 converted_int = df_int.apply(pd.to_numeric,downcast='unsigned')
 print(mem_usage(df_int))
print(mem_usage(converted_int))
elif 'float' in str(dtype):
print("Analyse %s"%str(dtype))
df_float = pubg_train.select_dtypes(include=[str(dtype)])
 converted_float = df_float.apply(pd.to_numeric,downcast='float')
 print(mem_usage(df_float))
print(mem_usage(converted_float))
_=clf.fit(X_source, Y_source )<prepare_output>
print("Memory Usage of Original dataset: %s"%mem_usage(pubg_train))
pubg_train[converted_int.columns] = converted_int
pubg_train[converted_float.columns] = converted_float
print("Memory Usage of Optimized dataset: %s"%mem_usage(pubg_train))
Y_target = pd.DataFrame(le.inverse_transform(clf.predict(X_target)) ).set_index(X_target.index )<rename_columns>
for dtype in dtype_list:
if 'int' in str(dtype):
print("Analyse %s"%str(dtype))
df_int=pubg_test.select_dtypes(include=[str(dtype)])
 converted_int = df_int.apply(pd.to_numeric,downcast='unsigned')
 print(mem_usage(df_int))
print(mem_usage(converted_int))
elif 'float' in str(dtype):
print("Analyse %s"%str(dtype))
df_float = pubg_test.select_dtypes(include=[str(dtype)])
 converted_float = df_float.apply(pd.to_numeric,downcast='float')
 print(mem_usage(df_float))
print(mem_usage(converted_float))
Y_target.columns = ['MasVnrType']<prepare_x_and_y>
print("Memory Usage of Original dataset: %s"%mem_usage(pubg_test))
pubg_test[converted_int.columns] = converted_int
pubg_test[converted_float.columns] = converted_float
print("Memory Usage of Optimized dataset: %s"%mem_usage(pubg_test))
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>
pubg_train['headShotsPerKill'] = pubg_train['headshotKills']/pubg_train['kills']
pubg_test['headShotsPerKill'] = pubg_test['headshotKills']/pubg_test['kills']
pubg_train['headShotsPerKill'].fillna(0,inplace=True)
 pubg_test['headShotsPerKill'].fillna(0,inplace=True)
 pubg_train['headShotsPerKill'].replace(np.inf,0,inplace=True)
 pubg_test['headShotsPerKill'].replace(np.inf,0,inplace=True )
clf = RandomForestRegressor(n_estimators=100, min_samples_split=3, random_state=0)
 scores = cross_val_score(clf, X_source, Y_source, cv=5, scoring="neg_mean_squared_error",n_jobs=-1)
 scores.mean()<train_model>
pubg_train['distTravelled'] = pubg_train['rideDistance']+pubg_train['swimDistance']+pubg_train['walkDistance']
pubg_test['distTravelled'] = pubg_test['rideDistance']+pubg_test['swimDistance']+pubg_test['walkDistance']
_=clf.fit(X_source, Y_source)
 Y_target = pd.DataFrame(clf.predict(X_target)).set_index(X_target.index)
 Y_target.columns = ['MasVnrArea']<count_missing_values>
pubg_train['itemsUsed'] = pubg_train['heals']+pubg_train['boosts']
pubg_test['itemsUsed'] = pubg_test['heals']+pubg_test['boosts']
data.isna().sum() [lambda x: x>0]<predict_on_test>
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<count_values>
data.dtypes.value_counts()<set_options>
pubg_train['teamwork']=pubg_train['revives']+pubg_train['assists']
pubg_test['teamwork']=pubg_test['revives']+pubg_test['assists']
pd.set_option('display.max_columns', 100)
 data[data.select_dtypes(['int64'] ).columns].head()<data_type_conversions>
pubg_train['killpoints_norm'] =(pubg_train['killPoints']*pubg_train['playerJoined'])/100
pubg_train['damageDealt_norm']=(pubg_train['damageDealt']*pubg_train['playerJoined'])/100
pubg_train['kills_norm'] =(pubg_train['kills']*pubg_train['playerJoined'])/100
pubg_train['rankPoints_norm']=(pubg_train['rankPoints']*pubg_train['playerJoined'])/100
pubg_train['roadKills_norm']=(pubg_train['roadKills']*pubg_train['playerJoined'])/100
pubg_train['teamKills_norm']=(pubg_train['teamKills']*pubg_train['playerJoined'])/100
pubg_train['winPoints_norm']=(pubg_train['winPoints']*pubg_train['playerJoined'])/100
pubg_train['headShotsPerKill_norm']=(pubg_train['headShotsPerKill']*pubg_train['playerJoined'])/100
pubg_train['teamwork_norm']=(pubg_train['teamwork']*pubg_train['playerJoined'])/100
data[['MSSubClass', 'MoSold','OverallQual','OverallCond','YrSold']] = data[['MSSubClass', 'MoSold','OverallQual','OverallCond','YrSold']].astype(str )<data_type_conversions>
pubg_train['roadKills_log'] = np.log(1+pubg_train.roadKills)
 pubg_test['roadKills_log'] = np.log(1+pubg_test.roadKills )
data[['LotFrontage', 'MasVnrArea', 'BsmtFinSF1',
'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath',
'BsmtHalfBath', 'GarageYrBlt', 'GarageCars',
'GarageArea']] = data[['LotFrontage', 'MasVnrArea', 'BsmtFinSF1',
'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath',
'BsmtHalfBath', 'GarageYrBlt', 'GarageCars', 'GarageArea']].astype(int)
<predict_on_test>
pubg_test['killpoints_norm'] =(pubg_test['killPoints']*pubg_test['playerJoined'])/100
pubg_test['damageDealt_norm']=(pubg_test['damageDealt']*pubg_test['playerJoined'])/100
pubg_test['kills_norm'] =(pubg_test['kills']*pubg_test['playerJoined'])/100
pubg_test['rankPoints_norm']=(pubg_test['rankPoints']*pubg_test['playerJoined'])/100
pubg_test['roadKills_norm']=(pubg_test['roadKills']*pubg_test['playerJoined'])/100
pubg_test['teamKills_norm']=(pubg_test['teamKills']*pubg_test['playerJoined'])/100
pubg_test['winPoints_norm']=(pubg_test['winPoints']*pubg_test['playerJoined'])/100
pubg_test['headShotsPerKill_norm']=(pubg_test['headShotsPerKill']*pubg_test['playerJoined'])/100
pubg_test['teamwork_norm']=(pubg_test['teamwork']*pubg_test['playerJoined'])/100
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<define_variables>
pubg_train['vehicleDestroys_log'] = np.log(1+pubg_train.vehicleDestroys)
 pubg_test['vehicleDestroys_log'] = np.log(1+pubg_test.vehicleDestroys)
 pubg_train['swimDistance_log']=np.log(1+pubg_train.swimDistance)
 pubg_test['swimDistance_log']=np.log(1+pubg_test.swimDistance)
 pubg_train['teamKills_log']=np.log(1+pubg_train.teamKills)
 pubg_test['teamKills_log']=np.log(1+pubg_test.teamKills)
env_cols =['MSZoning','Street','Alley','Neighborhood','Condition1','Condition2']<categorify>
pubg_train.drop(columns=['Id','groupId','matchId',],inplace=True)
 pubg_test.drop(columns=['Id','groupId','matchId',],inplace=True )
mean_encoder(data,['MSZoning','Street','Alley','Neighborhood'])
<feature_engineering>
X=pubg_train.drop(['winPlacePerc'],axis=1)
 y=pubg_train['winPlacePerc']
data['ConditionFeedr'] =(data.Condition2=="Feedr")*1+(data.Condition1=="Feedr")*1
data['ConditionArtery'] =(data.Condition2=="Artery")*1+(data.Condition1=="Artery")*1
data['ConditionPosA'] =(data.Condition2=="PosA")*1+(data.Condition1=="PosA")*1
data['ConditionPosN'] =(data.Condition2=="PosN")*1+(data.Condition1=="PosN")*1
data['ConditionRRAn'] =(data.Condition2=="RRAn")*1+(data.Condition1=="RRAn")*1
data['ConditionRRAe'] =(data.Condition2=="RRAe")*1+(data.Condition1=="RRAe")*1
data['ConditionRRNn'] =(data.Condition2=="RRNn")*1+(data.Condition1=="RRNn")*1
data['ConditionRRNe'] =(data.Condition2=="RRNe")*1+(data.Condition1=="RRNe")*1
data = data.drop(['Condition1','Condition2'],axis=1 )<predict_on_test>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=22,shuffle=True )
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<define_variables>
from sklearn.preprocessing import StandardScaler
lot_cols =['LotFrontage','LotArea','LotShape','LandContour','LotConfig','LandSlope','Fence','Fireplaces','FireplaceQu','GarageType','GarageYrBlt','GarageFinish','GarageCars','GarageArea','GarageQual','GarageCond','PoolArea','PoolQC']<define_variables>
scaler = StandardScaler(copy=False)
 scaler.fit(X_train)
 Scaled_X_train = scaler.transform(X_train)
 Scaled_X_test = scaler.transform(X_test )
data['LotShape'].dtype=='object'<categorify>
from sklearn import model_selection
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.pipeline import Pipeline
mean_encoder(data,['LotShape','LandContour','LotConfig','LandSlope','Fence','FireplaceQu','GarageType','GarageFinish','GarageQual','GarageCond'])
<count_missing_values>
sample_X_train = Scaled_X_train[:100000,]
sample_y_train = y_train[:100000]
data.PoolQC.isna().sum()<feature_engineering>
def randomized_search() :
pipeline1 = Pipeline(( 
('clf', RandomForestRegressor(max_depth=500,max_features=None)) , 
))
pipeline2 = Pipeline(( 
('clf', LogisticRegression(class_weight='balanced',penalty='l2',solver='saga',max_iter=100)) , 
))
pipeline3 = Pipeline(( 
('clf', MLPRegressor()), 
))
pipeline4 = Pipeline(( 
('clf', DecisionTreeRegressor(splitter='random')) , 
))
parameters1 = {
'clf__n_estimators': [ 100],
}
parameters2 = {
'clf__C':[0.1,0.001,1,10]
}
parameters3 = {
'clf__alpha':[0.001,0.00001,0.0001],
'clf__max_iter':[250,400,500],
'clf__batch_size':[100,500,1000]
}
parameters4 = {
'clf__max_depth': [10,100,500,1000],
'clf__max_features': ['auto', 'sqrt', 'log2', None],
'clf__min_weight_fraction_leaf':[0,0.25,0.5]
}
pars = [(parameters1,"RandomForestRegressor"),(parameters2,"LogisticRegression"),(parameters3,"MLPRegressor"),(parameters4,"DecisionTreeRegressor")]
pips = [pipeline1, pipeline2, pipeline3, pipeline4]

 model,name = pars[0]
print('-'*50)
 print("Starting Randomized Search for %s" %name)
 rs = RandomizedSearchCV(pips[0], model, verbose=5, refit=False, n_jobs=3,cv=5,random_state=22,n_iter=20)
 rs = rs.fit(sample_X_train, sample_y_train)
 print("Finished Randomized Search for %s"%name)
 print('Best Score %.5f'%rs.best_score_)
 print('Best Param %s'%rs.best_params_)
 print('-'*50)
data['Pool'] =(data.PoolQC!="NA")*1<drop_column>
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
from math import sqrt
data = data.drop(['PoolQC'],axis=1 )<count_values>
data[data.Pool==0].PoolArea.value_counts()<feature_engineering>
from sklearn.neural_network import MLPClassifier, MLPRegressor
data.loc[(data.Pool==0)&(data.PoolArea>0),'Pool']=1<filter>
data[data.LotFrontage>300]<filter>
gc.collect()
data = data[data.LotFrontage<300]<feature_engineering>
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import KFold
data['LotFrontage']=np.power(data.LotFrontage,0.25 )<filter>
import lightgbm as lgb
data = data[data.LotArea<100000]<feature_engineering>
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'max_depth':-1,
'learning_rate': 0.01,
'feature_fraction': 0.9,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'num_leaves':1000,
'max_bin':1000,
'verbose': 0
}
data['LotArea'] = np.log1p(data.LotArea )<groupby>
lgb_train = lgb.Dataset(Scaled_X_train, y_train)
 lgb_eval = lgb.Dataset(Scaled_X_test, y_test, reference=lgb_train )
data.groupby('Fireplaces')['tmpPrice'].mean()<groupby>
print('Starting training...')
 gbm = lgb.train(params,
lgb_train,
num_boost_round=100,
valid_sets=lgb_eval
)
data.groupby('GarageCars')['tmpPrice'].mean()<count_values>
print('Starting predicting...')
 y_pred = gbm.predict(Scaled_X_test, num_iteration=gbm.best_iteration)
 print('Done !' )
data.GarageCars.value_counts()<feature_engineering>
print('The rmse of prediction is:', mean_squared_error(y_test, y_pred)** 0.5 )
data['GarageCars']=data.GarageCars.replace([4,5],3 )<filter>
gbm_score=r2_score(y_pred=y_pred,y_true=y_test)
 print("Variance score(r2_score): %f"%gbm_score)
 print('Model accuracy:%.2f '%(gbm_score*100))
print("Root mean squared error of test:%f"%sqrt(mean_squared_error(y_test,y_pred)) )
data[data.GarageYrBlt>2010]<feature_engineering>

 print('' )
data.loc[data.GarageYrBlt>2010,'GarageYrBlt']=2007<feature_engineering>
final_pred2 = gbm.predict(scaled_test, num_iteration=gbm.best_iteration)
data['GarageYrBlt']=np.log1p(2011-data.GarageYrBlt )<groupby>
submission['winPlacePerc'] = final_pred2
<drop_column><EOS> 
submission.to_csv('final_submission.csv',index=False )