Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<feature_engineering>
%matplotlib inline
gc.enable()
df_train["Family Size"] =(df_train['SibSp'] + df_train['Parch'] + 1)
 df_test["Family Size"] = df_test['SibSp'] + df_test['Parch'] + 1
print('Family size feature created' )<feature_engineering>
warnings.simplefilter("ignore")
df_data["Last_Name"] = df_data['Name'].apply(lambda x: str.split(x, ",")[0])
 DEFAULT_SURVIVAL_VALUE = 0.5
df_data["Family_Survival"] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in df_data[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passengers with family survival information:",
df_data.loc[df_data['Family_Survival']!=0.5].shape[0])
 for _, grp_df in df_data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passenger with family/group survival information: "
+str(df_data[df_data['Family_Survival']!=0.5].shape[0]))
df_train["Family_Survival"] = df_data['Family_Survival'][:891]
df_test["Family_Survival"] = df_data['Family_Survival'][891:]<data_type_conversions>
df_train = pd.read_csv('.. /input/train_V2.csv', sep=',')
df_data["HadCabin"] =(df_data["Cabin"].notnull().astype('int'))
df_train["HadCabin"] = df_data["HadCabin"][:891]
df_test["HadCabin"] = df_data["HadCabin"][891:]
print('Cabin feature created' )<categorify>
df_train = reduce_mem_usage(df_train )
df_data["Deck"] = df_data.Cabin.str.extract('([A-Za-z])', expand=False)
 df_data["Deck"] = df_data["Deck"].fillna("N")
 deck_mapping = {"N":0,"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
df_data['Deck'] = df_data['Deck'].map(deck_mapping)
 df_train["Deck"] = df_data["Deck"][:891]
df_test["Deck"] = df_data["Deck"][891:]
print('Deck feature created')
 df_data["Deck"] = df_data.Cabin.str.extract('([A-Za-z])', expand=False)
 deck_mapping = {"0":0,"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
df_data['Deck'] = df_data['Deck'].map(deck_mapping)
 df_data["Deck"] = df_data["Deck"].fillna("0")
 df_data["Deck"]=df_data["Deck"].astype('int')
 df_train["Deck"] = df_data['Deck'][:891]
df_test["Deck"] = df_data['Deck'][891:]
print('Deck feature created')
 dummies=pd.get_dummies(df_train[['Deck']].astype('category'), prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[['Deck']].astype('category'), prefix_sep='_')
 df_test = pd.concat([df_test,dummies], axis=1)
 print('Deck Categories created' )<feature_engineering>
df_train.loc[pd.notnull(df_train['winPlacePerc'])== False]
Ticket = []
for i in list(df_data.Ticket):
if not i.isdigit() :
Ticket.append(i.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket.append("X")
 df_data["Ticket"] = Ticket
df_data["Ticket"].head()
df_train["Ticket"] = df_data["Ticket"][:891]
df_test["Ticket"] = df_data["Ticket"][891:]
print('Ticket feature created' )<categorify>
df_train = df_train[pd.notnull(df_train['winPlacePerc'])]
df_data['TicketRef'] = df_data['Ticket'].apply(lambda x: str(x)[0])
 df_data['TicketRef'].value_counts()
df_train["TicketRef"] = df_data["TicketRef"][:891]
df_test["TicketRef"] = df_data["TicketRef"][891:]
dummies=pd.get_dummies(df_train[["TicketRef"]].astype('category'), prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[["TicketRef"]].astype('category'), prefix_sep='_')
 df_test = pd.concat([df_test, dummies], axis=1)
 print("TicketBand categories created" )<categorify>
df_train.loc[pd.notnull(df_train['winPlacePerc'])== False]
dummies=pd.get_dummies(df_train[["Pclass"]].astype('category'), prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[["Pclass"]].astype('category'), prefix_sep='_')
 df_test = pd.concat([df_test, dummies], axis=1)
 print("pclass categories created" )<data_type_conversions>
df_train = df_train.assign(team_size=df_train.groupby('groupId' ).groupId.transform('count'))
df_train = df_train.assign(max_team_size=df_train.groupby('matchId' ).team_size.transform('max'))
df_train = df_train.assign(match_size=df_train.groupby('matchId' ).Id.transform('nunique'))
df_data["Free"] = np.where(df_data['Fare'] ==0, 1,0)
 df_data["Free"] = df_data['Free'].astype(int)
 df_train["Free"] = df_data["Free"][:891]
df_test["Free"] = df_data["Free"][891:]
print('Free Category created' )<data_type_conversions>
df_train = df_train.assign(team_indicator = df_train.team_size.apply(lambda x: 5 if x>= 5 else x))
df_train = pd.get_dummies(df_train, columns=['team_indicator'])
 dummy_cols = ['team_indicator_{}'.format(i)for i in np.arange(1,6)]
df_train[dummy_cols] = df_train.groupby('matchId')[dummy_cols].transform('mean' )
Pclass = [1,2,3]
for aclass in Pclass:
fare_to_impute = df_data.groupby('Pclass')['Fare'].median() [aclass]
df_data.loc[(df_data['Fare'].isnull())&(df_data['Pclass'] == aclass), 'Fare'] = fare_to_impute
df_train["Fare"] = df_data["Fare"][:891]
df_test["Fare"] = df_data["Fare"][891:]
df_train["FareBand"] = pd.qcut(df_train['Fare'], 4, labels = [1, 2, 3, 4] ).astype('category')
 df_test["FareBand"] = pd.qcut(df_test['Fare'], 4, labels = [1, 2, 3, 4] ).astype('category')
 dummies=pd.get_dummies(df_train[["FareBand"]], prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[["FareBand"]], prefix_sep='_')
 df_test = pd.concat([df_test, dummies], axis=1)
 print("Fareband categories created" )<categorify>
df_train.loc[df_train.team_indicator_1 >= 0.7, 'game_mode'] = 'solo'
df_train.loc[df_train.team_indicator_2 >= 0.6, 'game_mode'] = 'duo'
df_train.loc[(df_train.team_indicator_3 + df_train.team_indicator_4)>= 0.5, 'game_mode'] = 'squad'
df_train.game_mode = np.where(( df_train.team_indicator_5 >= 0.2), 'custom', df_train.game_mode)
 df_train.game_mode = df_train.game_mode.fillna('custom' )
dummies=pd.get_dummies(df_train[["Embarked"]].astype('category'), prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[["Embarked"]].astype('category'), prefix_sep='_')
 df_test = pd.concat([df_test, dummies], axis=1)
 print("Embarked feature created" )<count_missing_values>
df_train = df_train.assign(match_mean = df_train.groupby('matchId')['winPlacePerc'].transform('mean'))
df_train = df_train.assign(match_median = df_train.groupby('matchId')['winPlacePerc'].transform('median'))
print(len(df_test.columns))
print(pd.isnull(df_test ).sum() )<define_variables>
print('The average match winning percentile is {:.2f}, the median is {:,.2f}'.format(df_train.winPlacePerc.mean() , df_train.winPlacePerc.median()))
SIMPLE_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Sex_female','Sex_male','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
INTERESTING_COLUMNS=['Survived','Pclass','Age','SibSp','Parch','Title','Alone','Mother','Family Size','Family_Survival','Embarked','FareBand','TicketRef']
CATEGORY_COLUMNS=['Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','Pclass_1', 'Pclass_2', 'Pclass_3','HadCabin','Free','FareBand_1', 'FareBand_2', 'FareBand_3', 'FareBand_4']<train_model>
sampled_matches = pd.Series(df_train.matchId.unique() ).sample(2000, random_state=42)
 df_train_sample = df_train[df_train.matchId.isin(sampled_matches)]
df_train_sample.groupby("matchId" ).size().mean()
test = df_test[CATEGORY_COLUMNS].fillna(-1000)
 data_to_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 X_train, X_test, y_train, y_test = train_test_split(data_to_train, df_train['Survived'], test_size=0.3,random_state=21, stratify=df_train['Survived'])
 RandomForest = RandomForestClassifier(random_state = 0)
 RandomForest.fit(X_train, y_train)
 print('Evaluation complete')
 print("Accuracy: {}".format(RandomForest.score(X_test, y_test)) )<train_model>
boosts_df = pd.concat([df_train_sample['winPlacePerc'], df_train_sample['matchDuration'], df_train_sample['boosts']], axis=1)
 boosts_mnPlusStd = boosts_df['boosts'].mean() + boosts_df['boosts'].std()
boosts_df['high_boosts'] = [i if i > boosts_mnPlusStd else 'None' for i in boosts_df['boosts']]
boosts_df = pd.DataFrame(boosts_df.loc[boosts_df['high_boosts'] != 'None'].astype(float))
RandomForest_checker = RandomForestClassifier()
RandomForest_checker.fit(X_train, y_train)
 importances_df = pd.DataFrame(RandomForest_checker.feature_importances_, columns=['Feature_Importance'],
index=X_train.columns)
 importances_df.sort_values(by=['Feature_Importance'], ascending=False, inplace=True)
 print(importances_df )<predict_on_test>
long_boosts_df = pd.DataFrame(boosts_df.loc[boosts_df['matchDuration'] > boosts_df['matchDuration'].mean() ].astype(int))
short_boosts_df = pd.DataFrame(boosts_df.loc[boosts_df['matchDuration'] < boosts_df['matchDuration'].mean() ].astype(int))
Submission['Survived']=RandomForest.predict(test)
 print(Submission.head())
 print('Submission created' )<save_to_csv>
del df_train_sample, boosts_df, long_boosts_df, short_boosts_df
gc.collect()
Submission.to_csv('randomforestcat01.csv',sep=',')
 print('file created' )<define_variables>
print('The largest team after dropping custom games has {} team members'.format(df_train.max_team_size.max()))
REVISED_NUMERIC_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
SIMPLE_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Sex_female','Sex_male','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
INTERESTING_COLUMNS=['Survived','Pclass','Age','SibSp','Parch','Title','Alone','Mother','Family Size','Family_Survival','Embarked','FareBand','TicketRef']
CATEGORY_COLUMNS=['Pclass','SibSp','Parch','Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','HadCabin','Free']
CATEGORY_COLUMNS=['Pclass','SibSp','Parch','Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','HadCabin','Free']
data_to_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 prediction = df_train["Survived"]
test = df_test[CATEGORY_COLUMNS].fillna(-1000)
 X_train, X_val, y_train, y_val = train_test_split(data_to_train, prediction, test_size = 0.3,random_state=21, stratify=y)
 print('Data split' )<compute_train_metric>
matchType_keep = ['matchType_squad', 'matchType_solo-fpp', 'matchType_squad-fpp', 'matchType_duo-fpp',
'matchType_solo', 'matchType_duo']
matchType_drop = ['matchType_squad', 'matchType_solo-fpp', 'matchType_squad-fpp', 'matchType_duo-fpp',
'matchType_solo', 'matchType_duo', 'matchType_normal-squad-fpp', 'matchType_flaretpp',
'matchType_crashfpp', 'matchType_normal-duo-fpp', 'matchType_crashtpp', 'matchType_normal-solo-fpp',
'matchType_flarefpp', 'matchType_normal-solo', 'matchType_normal-squad', 'matchType_normal-duo']
adaboost=AdaBoostClassifier()
adaboost.fit(X_train, y_train)
 y_pred = adaboost.predict(X_val)
 acc_adaboost = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_adaboost )<choose_model_class>
print('Number of players who traveled more than 8000 by walking: ' + str(len(df_train[df_train['walkDistance'] > 8000])) )
bagging=BaggingClassifier()
bagging.fit(X_train, y_train)
 y_pred = bagging.predict(X_val)
 acc_bagging = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_bagging )<compute_train_metric>
def remove_outliers_walk(data):
print('Number of walk distance outliers: ' + str(len(data.loc[data['walkDistance'] > 8000])))
 data.drop(data.loc[data['walkDistance'] > 8000].index, inplace=True)
 return str('Walk distance outliers removed.' )
decisiontree = DecisionTreeClassifier()
decisiontree.fit(X_train, y_train)
 y_pred = decisiontree.predict(X_val)
 acc_decisiontree = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_decisiontree )<compute_train_metric>
remove_outliers_walk(df_train )
et = ExtraTreesClassifier()
et.fit(X_train, y_train)
 y_pred = et.predict(X_val)
 acc_et = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_et )<compute_train_metric>
print('Number of players who traveled more than 15000 by vehicle: ' + str(len(df_train[df_train['rideDistance'] > 15000])) )
gaussian = GaussianNB()
gaussian.fit(X_train, y_train)
 y_pred = gaussian.predict(X_val)
 acc_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gaussian )<choose_model_class>
def remove_outliers_ride(data):
print('Number of ride distance outliers: ' + str(len(data.loc[data['rideDistance'] > 15000])))
 data.drop(data.loc[data['rideDistance'] > 15000].index, inplace=True)
 return str('Ride distance outliers removed.' )
gbk = GradientBoostingClassifier()
gbk.fit(X_train, y_train)
 y_pred = gbk.predict(X_val)
 acc_gbk = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gbk )<compute_train_metric>
remove_outliers_ride(df_train )
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
 y_pred = knn.predict(X_val)
 acc_knn = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_knn )<compute_train_metric>
print('Number of players who swam more than 500: ' + str(len(df_train[df_train['swimDistance'] > 500])) )
linear_da=LinearDiscriminantAnalysis()
linear_da.fit(X_train, y_train)
 y_pred = linear_da.predict(X_val)
 acc_linear_da = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_linear_da )<compute_train_metric>
def remove_outliers_swim(data):
print('Number of swim distance outliers: ' + str(len(data.loc[data['swimDistance'] > 500])))
 data.drop(data.loc[data['swimDistance'] > 500].index, inplace=True)
 return str('Swim distance outliers removed.' )
linear_svc = LinearSVC()
linear_svc.fit(X_train, y_train)
 y_pred = linear_svc.predict(X_val)
 acc_linear_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_linear_svc )<compute_train_metric>
remove_outliers_swim(df_train )
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
 y_pred = logreg.predict(X_val)
 acc_logreg = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_logreg )<choose_model_class>
df_train['total_distance'] = df_train.rideDistance + df_train.swimDistance + df_train.walkDistance
MLP = MLPClassifier()
MLP.fit(X_train, y_train)
 y_pred = MLP.predict(X_val)
 acc_MLP= round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_MLP )<choose_model_class>
def remove_movecheat(data):
data['killsWithoutMoving'] =(( data['kills'] > 0)&(data['total_distance'] == 0))
print('Number of movement cheaters: ' + str(len(data.loc[data['killsWithoutMoving'] == True])))
 data.drop(data.loc[data['killsWithoutMoving'] == True].index, inplace=True)
 return str('Cheaters removed.' )
passiveaggressive = PassiveAggressiveClassifier()
passiveaggressive.fit(X_train, y_train)
 y_pred = passiveaggressive.predict(X_val)
 acc_passiveaggressive = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_passiveaggressive )<compute_train_metric>
remove_movecheat(df_train )
perceptron = Perceptron()
perceptron.fit(X_train, y_train)
 y_pred = perceptron.predict(X_val)
 acc_perceptron = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_perceptron )<predict_on_test>
def remove_outliers_road(data):
roadk1_mnPlus3Std = data[data['roadKills'] >= 1]['roadKills'].mean() + 3*data[data['roadKills'] >= 1]['roadKills'].std()
print('Number of vehicle kill outliers: ' + str(len(data.loc[data['roadKills'] > roadk1_mnPlus3Std])))
 data.drop(data.loc[data['roadKills'] > roadk1_mnPlus3Std].index, inplace=True)
 return str('Vehicle kill outliers removed.' )
randomforest = RandomForestClassifier(random_state = 0)
 randomforest.fit(X_train, y_train)
 y_pred = randomforest.predict(X_val)
 acc_randomforest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_randomforest )<choose_model_class>
remove_outliers_road(df_train )
ridge = RidgeClassifierCV()
ridge.fit(X_train, y_train)
 y_pred = ridge.predict(X_val)
 acc_ridge = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_ridge )<compute_train_metric>
def remove_outliers_kills_old(data):
kills_mnPlus3Std = data['kills'].mean() + 3*data['kills'].std()
print('Number of kill outliers: ' + str(len(data.loc[data['kills'] > kills_mnPlus3Std])))
 data.drop(data.loc[data['kills'] > kills_mnPlus3Std].index, inplace=True)
 return str('Kill outliers removed.' )
sgd = SGDClassifier()
sgd.fit(X_train, y_train)
 y_pred = sgd.predict(X_val)
 acc_sgd = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_sgd )<compute_train_metric>
def remove_outliers_kills(data):
print('Number of kill outliers: ' + str(len(data.loc[data['kills'] > 20])))
 data.drop(data.loc[data['kills'] > 20].index, inplace=True)
 return str('Kill outliers removed.' )
clf = SVC()
clf.fit(X_train, y_train)
 y_pred = clf.predict(X_val)
 acc_clf = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_clf )<predict_on_test>
remove_outliers_kills(df_train )
xgb = XGBClassifier(n_estimators=10)
 xgb.fit(X_train, y_train)
 y_pred = xgb.predict(X_val)
 acc_xgb = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_xgb )<create_dataframe>
def remove_outliers_longkills(data):
print('Number of longest kill outliers: ' + str(len(data.loc[data['longestKill'] > 800])))
 data.drop(data.loc[data['longestKill'] > 800].index, inplace=True)
 return str('Longest kill outliers removed.' )
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression', 'Ridge Classifier',
'Random Forest', 'Naive Bayes', 'Linear SVC', 'MLP','AdaBoost','Linear discriminant','Passive Aggressive',
'Decision Tree', 'Gradient Boosting Classifier','Extra Trees','Stochastic Gradient Descent','Perceptron','xgboost'],
'Score': [acc_clf, acc_knn, acc_logreg,acc_ridge,acc_randomforest, acc_gaussian,acc_linear_svc, acc_MLP,acc_adaboost,acc_linear_da,acc_passiveaggressive,acc_decisiontree,acc_gbk,acc_et,acc_sgd,acc_perceptron,acc_xgb]})
 models.sort_values(by='Score', ascending=False )<predict_on_test>
remove_outliers_longkills(df_train )
Submission['Survived']=ridge.predict(test)
 print(Submission.head(5))
print('Prediction complete' )<save_to_csv>
print('Number of players who acquired more than 50 weapons: ' + str(len(df_train[df_train['weaponsAcquired'] > 50])))
Submission.set_index('PassengerId', inplace=True)
 Submission.to_csv('ridgesubmission02.csv',sep=',')
 print('File created' )<define_variables>
def remove_outliers_weapons(data):
print('Number of weapons acquired outliers: ' + str(len(data.loc[data['weaponsAcquired'] > 50])))
 data.drop(data.loc[data['weaponsAcquired'] > 50].index, inplace=True)
 return str('Weapons acquired outliers removed.' )
REVISED_NUMERIC_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
SIMPLE_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Sex_female','Sex_male','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
INTERESTING_COLUMNS=['Survived','Pclass','Age','SibSp','Parch','Title','Alone','Mother','Family Size','Family_Survival','Embarked','FareBand','TicketRef']
CATEGORY_COLUMNS=['Pclass','SibSp','Parch','Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','HadCabin','Free']
data_to_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 prediction = df_train["Survived"]
test = df_test[CATEGORY_COLUMNS].fillna(-1000)
 X_train, X_val, y_train, y_val = train_test_split(data_to_train, prediction, test_size = 0.3,random_state=21, stratify=prediction)
 print('Data split' )<train_on_grid>
remove_outliers_weapons(df_train )
param_grid = {'C':np.arange(1, 7),
'degree':np.arange(1, 7),
'max_iter':np.arange(0, 12),
'kernel':['rbf','linear'],
'shrinking':[0,1]}
clf = SVC()
svc_cv=GridSearchCV(clf, param_grid, cv=10)
 svc_cv.fit(X_train, y_train)
 print("Tuned SVC Parameters: {}".format(svc_cv.best_params_))
print("Best score is {}".format(svc_cv.best_score_))
acc_svc_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_svc_cv )<train_on_grid>
df_train['heals_boosts'] = df_train['heals'] + df_train['boosts']
param_grid = {"solver": ['newton-cg','lbfgs','liblinear','sag','saga'],'C': [0.01, 0.1, 1, 10, 100]}
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
 logreg_cv = GridSearchCV(logreg, param_grid, cv=30)
 logreg_cv.fit(X_train, y_train)
 y_pred = logreg_cv.predict(X_val)
 print("Tuned Logistic Regression Parameters: {}".format(logreg_cv.best_params_))
print("Best score is {}".format(logreg_cv.best_score_))
acc_logreg_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_logreg_cv )<train_on_grid>
print('Number of players who used more than 35 heals/boosts: ' + str(len(df_train[df_train['weaponsAcquired'] > 35])) )
param_grid = {"n_neighbors": np.arange(1, 50),
"leaf_size": np.arange(20, 40),
"algorithm": ["ball_tree","kd_tree","brute"]
}
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
 knn_cv = GridSearchCV(knn, param_grid, cv=10)
 knn_cv.fit(X_train, y_train)
 y_pred = knn_cv.predict(X_val)
 print("Tuned knn Parameters: {}".format(knn_cv.best_params_))
print("Best score is {}".format(knn_cv.best_score_))
acc_knn_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_knn_cv )<train_on_grid>
def remove_outliers_heals_boosts(data):
print('Number of heals/boosts used outliers: ' + str(len(data.loc[data['weaponsAcquired'] > 35])))
 data.drop(data.loc[data['heals_boosts'] > 35].index, inplace=True)
 return str('Heals/boosts outliers removed.' )
param_dist = {"random_state" : np.arange(0, 10),
"max_depth": np.arange(1, 10),
"max_features": np.arange(1, 10),
"min_samples_leaf": np.arange(1, 10),
"criterion": ["gini","entropy"]}
tree = DecisionTreeClassifier()
tree_cv = RandomizedSearchCV(tree, param_dist, cv=30)
 tree_cv.fit(X_train,y_train)
 y_pred = tree_cv.predict(X_val)
 print("Tuned Decision Tree Parameters: {}".format(tree_cv.best_params_))
print("Best score is {}".format(tree_cv.best_score_))
acc_tree_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_tree_cv )<train_on_grid>
remove_outliers_heals_boosts(df_train )
param_dist = {"random_state" : np.arange(0, 10),
"n_estimators" : np.arange(1, 20),
"max_depth": np.arange(1, 10),
"max_features": np.arange(1, 10),
"min_samples_leaf": np.arange(1, 10),
"criterion": ["gini","entropy"]}
randomforest = RandomForestClassifier()
randomforest_cv = RandomizedSearchCV(randomforest, param_dist, cv=30)
 randomforest_cv.fit(X_train,y_train)
 y_pred = randomforest_cv.predict(X_val)
 print("Tuned Decision Tree Parameters: {}".format(randomforest_cv.best_params_))
print("Best score is {}".format(randomforest_cv.best_score_))
acc_randomforest_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_randomforest_cv )<train_on_grid>
winPoints_clean = df_train.drop(df_train[df_train['winPoints'] < 1].index)
 winPoints_clean = winPoints_clean['winPoints']
winPoints_mean = winPoints_clean.mean()
df_train['winPoints_imp'] = df_train['winPoints'].replace({0 : winPoints_mean} )
param_dist = {'max_depth':np.arange(1, 7),
'min_samples_leaf': np.arange(1, 6),
"max_features": np.arange(1, 10),
}
gbk = GradientBoostingClassifier()
gbk_cv = RandomizedSearchCV(gbk, param_dist, cv=30)
 gbk_cv.fit(X_train, y_train)
 y_pred = gbk_cv.predict(X_val)
 print("Tuned Gradient Boost Parameters: {}".format(gbk_cv.best_params_))
print("Best score is {}".format(gbk_cv.best_score_))
acc_gbk_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gbk_cv )<train_on_grid>
rankPoints_clean = df_train.drop(df_train[df_train['rankPoints'] < 1].index)
 rankPoints_clean = rankPoints_clean['rankPoints']
rankPoints_mean = rankPoints_clean.mean()
df_train['rankPoints_imp'] = df_train['rankPoints'].replace({-1 : rankPoints_mean,
0 : rankPoints_mean} )
param_dist = {'learning_rate': [.01,.03,.05,.1,.25],
'max_depth': np.arange(1, 10),
'n_estimators': [10, 50, 100, 300],
'booster':['gbtree','gblinear','dart']
}
xgb = XGBClassifier()
xgb_cv = RandomizedSearchCV(xgb, param_dist, cv=20)
 xgb_cv.fit(X_train, y_train)
 y_pred = xgb_cv.predict(X_val)
 print("xgBoost Parameters: {}".format(xgb_cv.best_params_))
print("Best score is {}".format(xgb_cv.best_score_))
acc_xgb_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_xgb_cv )<create_dataframe>
df_train = df_train.drop(columns=matchType_drop, errors='ignore' )
optmodels = pd.DataFrame({
'optModel': ['Linear Regression','KNearestNieghbours','Decision Tree','Gradient Boost','Logistic Regression','xgboost'],
'optScore': [acc_svc_cv,acc_knn_cv,acc_tree_cv,acc_gbk_cv,acc_logreg_cv,acc_xgb_cv]})
 optmodels.sort_values(by='optScore', ascending=False )<prepare_x_and_y>
def playersJoined_norm(list_of_cols, df):
for column in list_of_cols:
df[column] = df[column]*(( 100-df['match_size'])/ 100 + 1)
X_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 y_train = df_train["Survived"]
X_test = df_test[CATEGORY_COLUMNS].fillna(-1000)
 test = df_test[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 randomf=RandomForestClassifier(random_state= 7,n_estimators=17,min_samples_leaf= 4, max_features=9,max_depth=5, criterion='gini')
 randomf.fit(data_to_train, prediction)
 Submission['Survived']=randomf.predict(X_test)
 Submission.to_csv('randomforestcats01.csv',sep=',')
 print(Submission.head(5))
print('File created' )<define_variables>
to_pJnorm = ['DBNOs', 'kills', 'maxPlace']
REVISED_NUMERIC_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
SIMPLE_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Sex_female','Sex_male','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
INTERESTING_COLUMNS=['Survived','Pclass','Age','SibSp','Parch','Title','Alone','Mother','Family Size','Family_Survival','Embarked','FareBand','TicketRef']
CATEGORY_COLUMNS=['Pclass','SibSp','Parch','Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','HadCabin','Free']
data_to_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 X_test2= df_test[CATEGORY_COLUMNS].fillna(-1000)
 prediction = df_train["Survived"]
X_train, X_test, y_train, y_test = train_test_split(data_to_train, prediction, test_size = 0.3,random_state=21, stratify=prediction)
 print('Data Split')
 hyperparams = {'algorithm': ['auto'], 'weights': ['uniform', 'distance'] ,'leaf_size': list(range(1,50,5)) ,
'n_neighbors':[6,7,8,9,10,11,12,14,16,18,20,22]}
gd=GridSearchCV(estimator = KNeighborsClassifier() , param_grid = hyperparams, verbose=True, cv=10, scoring = "roc_auc")
 gd.fit(X_train, y_train)
 gd.best_estimator_.fit(X_train,y_train)
 y_pred=gd.best_estimator_.predict(X_test)
 Submission['Survived']=gd.best_estimator_.predict(X_test2)
 print('Best Score')
 print(gd.best_score_)
 print('Best Estimator')
 print(gd.best_estimator_)
 acc_gd_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('Accuracy')
 print(acc_gd_cv)
 print('Confusion Matrrix')
 print(confusion_matrix(y_test, y_pred))
print('Classification_report')
 print(classification_report(y_test, y_pred))
print('Sample Prediction')
 print(Submission.head(10))
print('KNN prediction created' )<define_variables>
playersJoined_norm(to_pJnorm, df_train )
REVISED_NUMERIC_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
SIMPLE_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Sex_female','Sex_male','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
INTERESTING_COLUMNS=['Survived','Pclass','Age','SibSp','Parch','Title','Alone','Mother','Family Size','Family_Survival','Embarked','FareBand','TicketRef']
CATEGORY_COLUMNS=['Pclass','SibSp','Parch','Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','HadCabin','Free']
data_to_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 X_test2= df_test[CATEGORY_COLUMNS].fillna(-1000)
 prediction = df_train["Survived"]
X_train, X_test, y_train, y_test = train_test_split(data_to_train, prediction, test_size = 0.3,random_state=21, stratify=prediction)
 print('Data Split')
 hyperparams = {"random_state" : np.arange(0, 10),
"max_depth": np.arange(1, 10),
"max_features": np.arange(1, 10),
"min_samples_leaf": np.arange(1, 10),
"criterion": ["gini","entropy"]}
gd=GridSearchCV(estimator = DecisionTreeClassifier() , param_grid = hyperparams, verbose=True, cv=10, scoring = "roc_auc")
 gd.fit(X_train, y_train)
 gd.best_estimator_.fit(X_train,y_train)
 y_pred=gd.best_estimator_.predict(X_test)
 Submission['Survived']=gd.best_estimator_.predict(X_test2)
 print('Best Score')
 print(gd.best_score_)
 print('Best Estimator')
 print(gd.best_estimator_)
 acc_gd_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('Accuracy')
 print(acc_gd_cv)
 print('Confusion Matrrix')
 print(confusion_matrix(y_test, y_pred))
print('Classification_report')
 print(classification_report(y_test, y_pred))
print(Submission.head(10))
Submission.to_csv('Treegridsearch03.csv',sep=',')
 print('Decision Tree prediction created' )<define_variables>
def matchDuration_wt(list_of_cols, df):
for column in list_of_cols:
df[column] =(df[column] * df[column])/ df['matchDuration'].mean()
df[column].clip(0)
REVISED_NUMERIC_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
SIMPLE_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Sex_female','Sex_male','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
INTERESTING_COLUMNS=['Survived','Pclass','Age','SibSp','Parch','Title','Alone','Mother','Family Size','Family_Survival','Embarked','FareBand','TicketRef']
CATEGORY_COLUMNS=['Pclass','SibSp','Parch','Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','HadCabin','Free']
data_to_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 X_test2= df_test[CATEGORY_COLUMNS].fillna(-1000)
 prediction = df_train["Survived"]
X_train, X_test, y_train, y_test = train_test_split(data_to_train, prediction, test_size = 0.3,random_state=21, stratify=prediction)
 print('Data Split')
 hyperparams = {'solver':["newton-cg", "lbfgs", "liblinear", "sag", "saga"],
'C': [0.01, 0.1, 1, 10, 100]}
gd=GridSearchCV(estimator = LogisticRegression() , param_grid = hyperparams, verbose=True, cv=10, scoring = "roc_auc")
 gd.fit(X_train, y_train)
 gd.best_estimator_.fit(X_train,y_train)
 y_pred=gd.best_estimator_.predict(X_test)
 Submission['Survived']=gd.best_estimator_.predict(X_test2)
 print('Best Score')
 print(gd.best_score_)
 print('Best Estimator')
 print(gd.best_estimator_)
 acc_gd_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('Accuracy')
 print(acc_gd_cv)
 print('Confusion Matrrix')
 print(confusion_matrix(y_test, y_pred))
print('Classification_report')
 print(classification_report(y_test, y_pred))
print(Submission.head(10))
Submission.to_csv('Logregwithconfusion01.csv',sep=',')
 print('Logistic Regression prediction created' )<train_model>
to_matchDwt = ['heals_boosts']
X_train = df_train[CATEGORY_COLUMNS].fillna(-1000)
 y_train = df_train["Survived"]
X_test = df_test[CATEGORY_COLUMNS].fillna(-1000)
 randomf=RandomForestClassifier(criterion='gini', n_estimators=700, min_samples_split=10,min_samples_leaf=1,max_features='auto',oob_score=True,random_state=1,n_jobs=-1)
 randomf.fit(X_train, y_train)
 Submission['Survived']=randomf.predict(X_test)
 acc_gd_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('Accuracy')
 print(acc_gd_cv)
 print(Submission.head(10))
Submission.to_csv('finalrandomforest01.csv',sep=',')
 print('Random Forest prediction created' )<train_on_grid>
matchDuration_wt(to_matchDwt, df_train )
MLA = [
ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() ,
ensemble.RandomForestClassifier() ,
linear_model.LogisticRegressionCV() ,
neighbors.KNeighborsClassifier() ,
svm.SVC(probability=True),
]
index = 1
for alg in MLA:
predicted = alg.fit(X_train, y_train ).predict(X_test)
 fp, tp, th = roc_curve(y_test, predicted)
 roc_auc_mla = auc(fp, tp)
 MLA_name = alg.__class__.__name__
plt.plot(fp, tp, lw=2, alpha=0.3, label='ROC %s(AUC = %0.2f)' %(MLA_name, roc_auc_mla))
index+=1
plt.title('ROC Curve comparison')
 plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.plot([0,1],[0,1],'r--')
 plt.xlim([0,1])
 plt.ylim([0,1])
 plt.ylabel('True Positive Rate')
 plt.xlabel('False Positive Rate')
 plt.show()<define_variables>
def engineer_features(data):
data['max_possible_kills'] = data.match_size - data.team_size
data['total_items_acquired'] = data.boosts + data.heals + data.weaponsAcquired
data['items_per_distance'] = data.total_items_acquired/data.total_distance
data['kills_per_distance'] = data.kills/data.total_distance
data['knocked_per_distance'] = data.DBNOs/data.total_distance
data['damage_per_distance'] = data.damageDealt/data.total_distance
data['headshot_kill_rate'] = data.headshotKills/data.kills
data['headshot_kill_rate'] = data['headshot_kill_rate'].fillna(data['headshot_kill_rate'].mean())
 data['max_kills_by_team'] = data.groupby(['matchId','groupId'] ).kills.transform('max')
 data['total_team_damage'] = data.groupby(['matchId','groupId'] ).damageDealt.transform('sum')
 data['total_team_kills'] = data.groupby(['matchId','groupId'] ).kills.transform('sum')
 data['total_team_items'] = data.groupby(['matchId','groupId'] ).total_items_acquired.transform('sum')
 data['pct_killed'] = data.kills/data.max_possible_kills
data['pct_knocked'] = data.DBNOs/data.max_possible_kills
data['pct_team_killed'] = data.total_team_kills/data.max_possible_kills
data['team_kill_points'] = data.groupby(['matchId','groupId'] ).killPoints.transform('sum')
 data['team_kill_rank'] = data.groupby(['matchId','groupId'] ).killPlace.transform('mean')
 data['max_kills_match'] = data.groupby('matchId' ).kills.transform('max')
 data['total_kills_match'] = data.groupby('matchId' ).kills.transform('sum')
 data = data.join(pd.get_dummies(data['game_mode']))
data.fillna(0, axis=1, inplace=True)
 return data
REVISED_NUMERIC_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
SIMPLE_COLUMNS=['Pclass','Age','SibSp','Parch','Family_Survival','Alone','Sex_female','Sex_male','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','Embarked']
INTERESTING_COLUMNS=['Survived','Pclass','Age','SibSp','Parch','Title','Alone','Mother','Family Size','Family_Survival','Embarked','FareBand','TicketRef']
CATEGORY_COLUMNS=['Pclass','SibSp','Parch','Family Size','Family_Survival','Alone','Mother','Sex_female','Sex_male','AgeBand_Child',
'AgeBand_Young Adult', 'AgeBand_Adult', 'AgeBand_Older Adult',
'AgeBand_Senior','Title_Master', 'Title_Miss','Title_Mr', 'Title_Mrs', 'Title_Millitary','NameBand_1',
'NameBand_2', 'NameBand_3', 'NameBand_4', 'NameBand_5','Embarked','TicketRef_A', 'TicketRef_C', 'TicketRef_F', 'TicketRef_L',
'TicketRef_P', 'TicketRef_S', 'TicketRef_W', 'TicketRef_X','HadCabin','Free']
data_to_train = df_train[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 data_to_test = df_test[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 prediction = df_train["Survived"]
X_train, X_val, y_train, y_val = train_test_split(data_to_train, prediction, test_size = 0.3,random_state=21, stratify=prediction)
 print('Data Split' )<find_best_model_class>
engineer_features(df_train )
logreg = LogisticRegression(C=10, solver='newton-cg')
 logreg.fit(X_train, y_train)
 y_pred_train_logreg = cross_val_predict(logreg,X_val, y_val)
 y_pred_test_logreg = logreg.predict(X_test)
 print('logreg first layer predicted')
 tree = DecisionTreeClassifier(random_state=8,min_samples_leaf=6, max_features= 7, max_depth= 4, criterion='gini', splitter='best')
 tree.fit(X_train, y_train)
 y_pred_train_tree = cross_val_predict(tree,X_val,y_val)
 y_pred_test_tree = tree.predict(X_test)
 print('decision tree first layer predicted')
 randomforest = RandomForestClassifier(random_state=8, n_estimators=15, min_samples_leaf= 4, max_features= 6, max_depth=4,criterion='gini')
 randomforest.fit(X_train, y_train)
 y_pred_train_randomforest = cross_val_predict(randomforest, X_val, y_val)
 y_pred_test_randomforest = randomforest.predict(X_test)
 print('random forest first layer predicted')
 gbk = GradientBoostingClassifier(min_samples_leaf=3, max_features= 3, max_depth= 3)
 gbk.fit(X_train, y_train)
 y_pred_train_gbk = cross_val_predict(gbk, X_val, y_val)
 y_pred_test_gbk = gbk.predict(X_test)
 print('gbk first layer predicted')
 knn = KNeighborsClassifier(algorithm='auto', leaf_size=36, metric='minkowski',metric_params=None, n_jobs=1, n_neighbors=12, p=2,weights='uniform')
 knn.fit(X_train, y_train)
 y_pred_train_knn = cross_val_predict(knn, X_val, y_val)
 y_pred_test_knn = gbk.predict(X_test)
 print('knn first layer predicted')
 clf = SVC(C=3, degree=1, kernel='linear', max_iter=1, shrinking=0)
 clf.fit(X_train, y_train)
 y_pred_train_clf = cross_val_predict(clf, X_val, y_val)
 y_pred_test_clf = clf.predict(X_test)
 print('clf first layer predicted' )<save_to_csv>
df_train = reduce_mem_usage(df_train )
votingC = VotingClassifier(estimators=[('logreg', logreg_cv.best_estimator_),('gbk', gbk_cv.best_estimator_), 
('tree', tree_cv.best_estimator_),('randomforest',randomforest_cv.best_estimator_),('knn',knn_cv.best_estimator_)], voting='soft', n_jobs=4)
 votingC = votingC.fit(X_train, y_train)
 Submission['Survived'] = votingC.predict(X_test)
 Submission.to_csv('Votingclassifier02.csv',sep=',')
 print('Voting Classifier Ensemble File created')
 print(Submission.head() )<create_dataframe>
def engineer_features2(is_train=True):
test_idx = None
if is_train:
print("processing train")
 df = df_train
df = df[df['maxPlace'] > 1]
else:
print("processing test")
 df = df_test
test_idx = df['Id']
print("selected features to generate aggregates for")
 target = 'winPlacePerc'
features = [
'assists',
'damageDealt',
'DBNOs',
'killPlace',
'killPoints',
'kills',
'killStreaks',
'longestKill',
'maxPlace',
'numGroups',
'revives',
'teamKills',
'vehicleDestroys',
'weaponsAcquired',
'team_size',
'match_size',
'team_indicator_1',
'team_indicator_2',
'team_indicator_3',
'team_indicator_4',
'total_distance',
'heals_boosts',
'winPoints_imp',
'rankPoints_imp',
'max_possible_kills',
'total_items_acquired',
'items_per_distance',
'kills_per_distance',
'knocked_per_distance',
'damage_per_distance',
'headshot_kill_rate',
'total_team_damage',
'total_team_kills',
'total_team_items',
'pct_killed',
'pct_knocked',
'pct_team_killed',
'team_kill_points',
'team_kill_rank',
'total_kills_match',
]
y = None
if is_train:
print("get target")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 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'])
 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
second_layer_train = pd.DataFrame({'Logistic Regression': y_pred_train_logreg.ravel() ,
'Gradient Boosting': y_pred_train_gbk.ravel() ,
'Decision Tree': y_pred_train_tree.ravel() ,
'Random Forest': y_pred_train_randomforest.ravel()
})
 X_train_second = np.concatenate(( y_pred_train_logreg.reshape(-1, 1), y_pred_train_gbk.reshape(-1, 1),
y_pred_train_tree.reshape(-1, 1), y_pred_train_randomforest.reshape(-1, 1)) ,
axis=1)
 X_test_second = np.concatenate(( y_pred_test_logreg.reshape(-1, 1), y_pred_test_gbk.reshape(-1, 1),
y_pred_test_tree.reshape(-1, 1), y_pred_test_randomforest.reshape(-1, 1)) ,
axis=1)
 tree = DecisionTreeClassifier(random_state=8,min_samples_leaf=6, max_depth= 4, criterion='gini' ).fit(X_train_second,y_val)
 Submission['Survived'] = tree.predict(X_test_second)
 print(Submission.head())
 print('Tuned Ensemble model prediction complete' )<save_to_csv>
df_train, y_train, train_columns, _ = engineer_features2(is_train=True )
Submission.to_csv('tunedensemblesubmission04.csv',sep=',')
 print('tuned Ensemble File created' )<load_from_csv>
df_train = reduce_mem_usage(df_train )
train_data = pd.read_csv('/kaggle/input/train.csv')
 test_data = pd.read_csv('/kaggle/input/test.csv' )<categorify>
to_drop = ['winPoints', 'rankPoints', 'rideDistance', 'walkDistance', 'swimDistance', 'headshotKills',
'roadKills', 'max_team_size', 'match_mean', 'match_median', 'team_indicator_5',
'game_mode', 'killsWithoutMoving', 'killsWithoutMoving_mean', 'heals', 'boosts',
'match_mean_mean', 'rankPoints']
to_drop_test = ['winPoints', 'rankPoints', 'rideDistance', 'walkDistance', 'swimDistance', 'headshotKills',
'roadKills', 'max_team_size', 'team_indicator_5', 'game_mode', 'killsWithoutMoving_mean',
'heals', 'boosts', 'match_mean_mean', 'winPlacePerc', 'rankPoints']
to_drop2 = ['Id', 'groupId', 'matchId', 'team_size']
categorical = ['matchId', 'groupId']
full_data = [train_data, test_data]
for idx, dataset in enumerate(full_data):
dataset = pd.concat([pd.get_dummies(dataset['Pclass'], prefix='Pclass'), dataset], axis=1)
 dataset = dataset.drop(['Pclass', 'Pclass_3'], axis=1)
 dataset['Title'] = dataset['Name'].apply(get_title)
 dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset = dataset.drop(['Name'], axis=1)
 dataset = pd.concat([pd.get_dummies(dataset['Sex'], prefix='Sex'), dataset], axis=1)
 dataset = dataset.drop(['Sex', 'Sex_male'], axis=1)
 dataset['Age'].fillna(dataset['Age'].median() , inplace = True)
 dataset['AgeBin'] = pd.cut(dataset['Age'].astype(int), 5, labels=[1, 2, 3, 4, 5])
 dataset = dataset.drop(['Age'], axis=1)
 dataset = dataset.drop(['Embarked'], axis=1)
 dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['IsAlone'] = 1
dataset['IsAlone'].loc[dataset['FamilySize'] > 1] = 0
dataset = dataset.drop(['SibSp', 'Parch'], axis=1)
 dataset = dataset.drop(['Fare'], axis=1)
 dataset = dataset.drop(['Cabin'], axis=1)
 dataset = dataset.drop(['Ticket'], axis=1)
 if idx == 0:
X_train, y_train = dataset.loc[:, dataset.columns != 'Survived'], dataset.loc[:, dataset.columns == 'Survived']
else:
submission_id = dataset['PassengerId']
X_test = dataset 
<train_model>
df_train.drop(labels=to_drop,inplace=True,axis=1, errors='ignore' )
kf = KFold(n_splits=5, random_state = 0)
 clf = clf = RandomForestClassifier(n_estimators=400, max_depth=4, min_samples_split=4, random_state=0)
 for train_index, test_index in kf.split(X_train):
__kf_X_train, __kf_X_test = X_train.values[train_index], X_train.values[test_index]
__kf_y_train, __kf_y_test = y_train.values[train_index].ravel() , y_train.values[test_index].ravel()
clf.fit(__kf_X_train, __kf_y_train)
 print(accuracy_score(__kf_y_test, clf.predict(__kf_X_test)) )<save_to_csv>
df_train['winPlacePerc'] = y_train
y_pred = pd.Series(clf.predict(X_test))
submission = pd.concat([submission_id, y_pred], axis=1)
 submission = submission.rename(columns={0:'Survived'})
 submission.to_csv('submisson.csv', index=False )<import_modules>
def train_validation(df, train_size=0.9):
unique_games = df.matchId.unique()
train_index = round(int(unique_games.shape[0]*train_size))
np.random.seed(42)
 np.random.shuffle(unique_games)
 train_Id = unique_games[:train_index]
validation_Id = unique_games[train_index:]
train = df[df.matchId.isin(train_Id)]
validation = df[df.matchId.isin(validation_Id)]
return train, validation
train, validation = train_validation(df_train)
import numpy as np
import pandas as pd
import seaborn as sns
from seaborn import countplot
import matplotlib.pyplot as plt
from keras import optimizers
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, Activation, BatchNormalization
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix<define_variables>
train_weights =(1/train['team_size'])
 validation_weights =(1/validation['team_size'])
TRAIN_PATH = '.. /input/train.csv'
TEST_PATH = '.. /input/test.csv'
SUBMISSION_NAME = 'submission.csv'
BATCH_SIZE = 64
EPOCHS = 45
LEARNING_RATE = 0.001
HEIGHT = 28
WIDTH = 28
CANAL = 1
N_CLASSES = 10<load_from_csv>
train.drop(labels=to_drop2,inplace=True,axis=1, errors='ignore')
 validation.drop(labels=to_drop2,inplace=True,axis=1, errors='ignore')
 X_train = train.drop(labels=['winPlacePerc'],axis=1)
 X_val = validation.drop(labels=['winPlacePerc'], axis=1)
 y_train = train['winPlacePerc']
y_val = validation['winPlacePerc']
print(str(X_train.shape)+ str(X_val.shape))
train = pd.read_csv(TRAIN_PATH)
 test = pd.read_csv(TEST_PATH)
 labels = train['label']
train = train.drop(['label'], axis=1 )<count_values>
del df_train
gc.collect()
labels.value_counts()<prepare_x_and_y>
lgbm = LGBMRegressor(objective='mae', n_estimators=4500,
learning_rate=0.03, num_leaves=300,
max_depth=14,
n_jobs=-1, random_state=42, verbose=50)
 lgb_reg = lgbm.fit(X_train, y_train, eval_set=[(X_val, y_val)], eval_metric='mae', early_stopping_rounds=100, verbose=50 )
train = train.values.reshape(-1,HEIGHT,WIDTH,CANAL)
 test = test.values.reshape(-1,HEIGHT,WIDTH,CANAL)
 labels = labels.values<categorify>
pd.DataFrame(sorted(zip(lgbm.feature_importances_, X_train.columns)) )
labels = pd.get_dummies(labels ).values<feature_engineering>
del X_train, y_train, X_val, y_val
gc.collect()
train = train / 255.0
test = test / 255.0<split>
df_test = pd.read_csv('.. /input/test_V2.csv', sep=',')
 df_test = reduce_mem_usage(df_test )
x_train, x_val, y_train, y_val = train_test_split(train, labels, test_size=0.1, random_state=1 )<init_hyperparams>
df_test = df_test.assign(team_size=df_test.groupby('groupId' ).groupId.transform('count'))
df_test = df_test.assign(max_team_size=df_test.groupby('matchId' ).team_size.transform('max'))
df_test = df_test.assign(match_size=df_test.groupby('matchId' ).Id.transform('nunique'))
df_test = df_test.assign(team_indicator = df_test.team_size.apply(lambda x: 5 if x>= 5 else x))
df_test = pd.get_dummies(df_test, columns=['team_indicator'])
 dummy_cols = ['team_indicator_{}'.format(i)for i in np.arange(1,6)]
df_test[dummy_cols] = df_test.groupby('matchId')[dummy_cols].transform('mean')
 df_test.loc[df_test.team_indicator_1 >= 0.7, 'game_mode'] = 'solo'
df_test.loc[df_test.team_indicator_2 >= 0.6, 'game_mode'] = 'duo'
df_test.loc[(df_test.team_indicator_3 + df_test.team_indicator_4)>= 0.5, 'game_mode'] = 'squad'
df_test.game_mode = np.where(( df_test.team_indicator_5 >= 0.2), 'custom', df_test.game_mode)
 df_test.game_mode = df_test.game_mode.fillna('custom')
 df_test['total_distance'] = df_test.rideDistance + df_test.swimDistance + df_test.walkDistance
df_test['heals_boosts'] = df_test['heals'] + df_test['boosts']
winPoints_clean = df_test.drop(df_test[df_test['winPoints'] < 1].index)
 winPoints_clean = winPoints_clean['winPoints']
winPoints_mean = winPoints_clean.mean()
df_test['winPoints_imp'] = df_test['winPoints'].replace({0 : winPoints_mean})
 rankPoints_clean = df_test.drop(df_test[df_test['rankPoints'] < 1].index)
 rankPoints_clean = rankPoints_clean['rankPoints']
rankPoints_mean = rankPoints_clean.mean()
df_test['rankPoints_imp'] = df_test['rankPoints'].replace({-1 : rankPoints_mean})
 df_test = pd.get_dummies(df_test, columns=['matchType'])
 matchType_enc = df_test.filter(regex='matchType')
 playersJoined_norm(to_pJnorm, df_test)
 matchDuration_wt(to_matchDwt, df_test)
 df_test = engineer_features(df_test)
 df_test, _, test_columns, test_id = engineer_features2(is_train=False)
 df_test.drop(labels=to_drop_test,inplace=True, axis=1, errors='ignore')
 df_test.drop(labels=to_drop2,inplace=True, axis=1, errors='ignore')
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
horizontal_flip=False,
vertical_flip=False,
rotation_range=15,
zoom_range = 0.15,
width_shift_range=0.15,
height_shift_range=0.15)
 datagen.fit(x_train )<choose_model_class>
df_test = reduce_mem_usage(df_test )
<train_model><EOS> 
results = pd.DataFrame()
results['Id'] = test_id
results['winPlacePerc'] = lgbm.predict(df_test, num_iteration=lgbm.best_iteration_)
 results.winPlacePerc = results.winPlacePerc.clip(0, 1)
 results.to_csv("submission.csv", index=False)
 results.head(20 )