Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<categorify>
pd.options.mode.chained_assignment = None
pd.options.display.max_columns = 999
init_notebook_mode(connected=True)
 color = sns.color_palette()
%matplotlib inline
def trataDados(train, test, drop_list, target):
dados_origin = pd.concat(( train, test))
dados_origin = dados_origin.drop(drop_list, axis=1)
 dados_origin[dados_origin.isnull().any(axis=1)]
if dados_origin.isnull().values.any() :
dados_origin = dados_origin.fillna(dados_origin.mean())
 dados_origin = pd.get_dummies(dados_origin)
 target_rounded = [round(label, ndigits=None)for label in dados_origin[target]]
target_rounded = [int(survived)for survived in target_rounded]
dados_origin[target] = target_rounded
return_train = dados_origin[: train.shape[0]]
train1, test1 = train_test_split(
 return_train, test_size=0.20, random_state=600, stratify=train[target] 
)
 yo_train = train1[target]
yo_test = test1[target]
Xo_train = train1.drop(target, axis=1)
 Xo_test = test1.drop(target, axis=1)
 return_test = dados_origin[train.shape[0] :]
return_test = return_test.drop(target, axis=1)
 return Xo_train, Xo_test, yo_train, yo_test, return_test
def export(name_csv, predict, base_test):
d_test = base_test[["PassengerId"]]
d_test["Survived"] = predict
d_test.to_csv(name_csv + "_titanic.csv", index=False )<split>
train_raw = pd.read_csv('.. /input/train_V2.csv')
 test_raw = pd.read_csv('.. /input/test_V2.csv' )
drop_list = ["Name", "Fare", "Cabin", "Ticket"]
X_train, X_test, y_train, y_test, df_validate = trataDados(
 df_train_original, df_test_original, drop_list, "Survived"
)<choose_model_class>
train_raw.isnull().sum()
temp = []
classifier = [
"Decision Tree",
"Random Forest",
"KNN",
"Extra Trees",
"Ada Boost",
"Gradient Boosting",
]
models = [
DecisionTreeClassifier(
 criterion="gini",
max_depth=2,
max_features=None,
min_samples_leaf=1,
min_samples_split=2,
random_state=8,
splitter="random", 
),
RandomForestClassifier(
 min_samples_split=5,
min_samples_leaf=5,
criterion="entropy",
max_depth=1,
max_features="auto",
n_estimators=10,
random_state=10,
verbose=3, 
),
KNeighborsClassifier(
 algorithm="auto", leaf_size=30, n_neighbors=4, p=2, weights="uniform" 
),
ExtraTreesClassifier(
 criterion="gini",
max_depth=5,
max_features=None,
min_samples_leaf=1,
min_samples_split=2,
n_estimators=10,
random_state=5,
verbose=5, 
),
AdaBoostClassifier(algorithm="SAMME", n_estimators=10, random_state=1),
GradientBoostingClassifier(
 min_samples_split=5,
min_samples_leaf=5,
learning_rate=0.1,
loss="exponential",
max_depth=2,
max_features="sqrt",
n_estimators=20,
random_state=2,
verbose=1, 
),
]<predict_on_test>
test_raw.isnull().sum()
for index, model in enumerate(models):
model.fit(X_train, y_train)
 predict = model.predict(X_test)
 temp.append(metrics.accuracy_score(predict, y_test))
predict = model.predict(df_validate)
 export(classifier[index], predict, df_validate )<create_dataframe>
continuous = ['damageDealt','killPlace','killPoints','longestKill','matchDuration','maxPlace','numGroups','rankPoints','rideDistance','walkDistance','winPoints']
discrete = ['assists','boosts','DBNOs','headshotKills','heals','kills','killStreaks','revives','teamKills','vehicleDestroys','weaponsAcquired']
categories = ['matchType']
models_dataframe = pd.DataFrame(temp, index=classifier)
 models_dataframe.columns = ["Accuracy"]
print(models_dataframe)
<load_from_csv>
train_raw['mvmtDistance'] = train_raw['walkDistance'] + train_raw['rideDistance']+train_raw['swimDistance']
features = list(train_raw.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 features.remove("winPlacePerc" )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
<drop_column>
df_team = train_raw.copy()
df_max= train_raw.groupby(['matchId','groupId'])[features].agg('max')
 df_team = pd.merge(train_raw, df_max.reset_index() , suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 df_team = df_team.drop(["assists_max","killPoints_max","headshotKills_max","numGroups_max","revives_max","teamKills_max","roadKills_max","vehicleDestroys_max"], axis=1)
 df_rank = df_max.groupby('matchId')[features].rank(pct=True ).reset_index()
df_team = pd.merge(df_team, df_rank, suffixes=["", "_maxRank"], how='left', on=['matchId', 'groupId'])
 df_team = df_team.drop(["roadKills_maxRank","matchDuration_maxRank","maxPlace_maxRank","numGroups_maxRank"], axis=1)
 del df_max
del df_rank
gc.collect()
df_sum = train_raw.groupby(['matchId','groupId'])[features].agg('sum')
 df_team = pd.merge(df_team, df_sum.reset_index() , suffixes=["", "_sum"], how='left', on=['matchId', 'groupId'])
 df_team = df_team.drop(["assists_sum","killPoints_sum","headshotKills_sum","numGroups_sum","revives_sum","teamKills_sum","roadKills_sum","vehicleDestroys_sum"], axis=1)
 del df_sum
gc.collect()
train_id = train["Id"]
test_id = test["Id"]
train.drop("Id", axis=1, inplace= True)
 test.drop("Id", axis=1, inplace= True)
<prepare_x_and_y>
df_team = reduce_mem_usage(df_team)
 test_raw = reduce_mem_usage(test_raw)
 df_pred = df_team.drop(['Id','groupId','matchId'],axis=1)
 df_pred = reduce_mem_usage(df_pred)
 df_pred = pd.get_dummies(df_pred)
 df_pred_y = df_pred['winPlacePerc']
df_pred_x = df_pred.drop(['winPlacePerc'],axis=1 )
train_salePrice = train.SalePrice
train.SalePrice = np.log1p(train.SalePrice)
 y_train = train.SalePrice.values
y_train_orig = train.SalePrice<concatenate>
X_train, X_test, y_train, y_test = train_test_split(df_pred_x, df_pred_y, test_size=0.33, random_state=42 )
data_features = pd.concat(( train, test), sort=True ).reset_index(drop=True)
 print(data_features.shape)
 data_features_na = data_features.isnull().sum()
data_features_na = data_features_na[data_features_na>0]
data_features_na.sort_values(ascending=False )<data_type_conversions>
params = {
'boosting_type':'gbdt',
'objective': 'regression',
'nthread': -1,
'verbose': 0,
'num_leaves': 31,
'learning_rate': 0.05,
'max_depth': -1,
'subsample': 0.8,
'subsample_freq': 1,
'colsample_bytree': 0.6,
'reg_aplha': 1,
'reg_lambda': 0.001,
'metric': 'rmse',
'min_split_gain': 0.5,
'min_child_weight': 1,
'min_child_samples': 10
}
train_set = lgb.Dataset(X_train, y_train, silent=True)
 model = lgb.train(params, train_set = train_set, num_boost_round=300)
 pred_test_y = model.predict(X_test, num_iteration = model.best_iteration)
 rms = sqrt(mean_squared_error(y_test, pred_test_y))
rms
str_vars = ['MSSubClass','YrSold','MoSold']
for var in str_vars:
data_features[var] = data_features[var].apply(str)
 common_vars = ['Exterior1st','Exterior2nd']
for var in common_vars:
data_features[var] = data_features[var].fillna('Other')
 common_vars = ['SaleType']
for var in common_vars:
data_features[var] = data_features[var].fillna('Oth')
 common_vars = ['Electrical']
for var in common_vars:
data_features[var] = data_features[var].fillna('FuseP')
 common_vars = ['KitchenQual']
for var in common_vars:
data_features[var] = data_features[var].fillna('Po')
 data_features['MSZoning'] = data_features['MSZoning'].fillna('I')
 for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond','BsmtQual',
'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',"PoolQC"
,'Alley','Fence','MiscFeature','FireplaceQu','MasVnrType','Utilities']:
data_features[col] = data_features[col].fillna('None')
 for col in('GarageYrBlt', 'GarageArea', 'GarageCars','MasVnrArea','BsmtFinSF1','BsmtFinSF2'
,'BsmtFullBath','BsmtHalfBath','FullBath','HalfBath','BsmtUnfSF','TotalBsmtSF'):
data_features[col] = data_features[col].fillna(0)
 data_features['LotFrontage'] = data_features.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.min()))
print('Features size:', data_features.shape)
 data_features['Functional'] = data_features['Functional'].fillna('Typ' )<feature_engineering>
rms = sqrt(mean_squared_error(y_test, prediction))
print(rms)
 df_result = pd.DataFrame(columns=['PRED','REAL'])
 df_result['PRED'] = prediction
df_result['REAL'] = y_test.reset_index(drop = True)
 df_result.head(100 )
skewness = skewness[abs(skewness)> 0.5]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
print("Mean skewnees: {}".format(np.mean(skewness)))
 skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
feat_num[feat] = boxcox1p(feat_num[feat], boxcox_normmax(feat_num[feat] + 1))
data_features[feat] = boxcox1p(data_features[feat], boxcox_normmax(data_features[feat] + 1))
skewness.sort_values(ascending=False )<sort_values>
test_raw['mvmtDistance'] = test_raw['walkDistance'] + test_raw['rideDistance']+test_raw['swimDistance']
features = list(test_raw.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 df_max= test_raw.groupby(['matchId','groupId'])[features].agg('max')
 df_team = pd.merge(test_raw, df_max.reset_index() , suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 df_team = df_team.drop(["assists_max","killPoints_max","headshotKills_max","numGroups_max","revives_max","teamKills_max","roadKills_max","vehicleDestroys_max"], axis=1)
 df_rank = df_max.groupby('matchId')[features].rank(pct=True ).reset_index()
df_team = pd.merge(df_team, df_rank, suffixes=["", "_maxRank"], how='left', on=['matchId', 'groupId'])
 df_team = df_team.drop(["roadKills_maxRank","matchDuration_maxRank","maxPlace_maxRank","numGroups_maxRank"], axis=1)
 del df_max
del df_rank
gc.collect()
df_sum = train_raw.groupby(['matchId','groupId'])[features].agg('sum')
 df_team = pd.merge(df_team, df_sum.reset_index() , suffixes=["", "_sum"], how='left', on=['matchId', 'groupId'])
 df_team = df_team.drop(["assists_sum","killPoints_sum","headshotKills_sum","numGroups_sum","revives_sum","teamKills_sum","roadKills_sum","vehicleDestroys_sum"], axis=1)
 del df_sum
gc.collect()
test = df_team.drop(['Id', 'groupId', 'matchId'],axis=1)
 test = pd.get_dummies(test)
 pred_test_y = model.predict(test, num_iteration = model.best_iteration)
 test = pd.DataFrame(columns=['Id', 'winPlacePerc'])
 test['Id'] = test_raw['Id']
test['winPlacePerc'] = pred_test_y
test.to_csv('submission.csv',index=False )