Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
rfc1=RandomForestClassifier(random_state=42, max_features='auto', n_estimators= 200, max_depth=5, criterion='gini' )<train_model>
def import_training_data(nrows=None):
df=pd.read_csv('.. /input/train_V2.csv', sep=',', encoding='utf-8', nrows=nrows)
 df=df.drop(['rankPoints'],axis=1)
 return df
def import_test_data(nrows=None):
df_test=pd.read_csv('.. /input/test_V2.csv', sep=',', encoding='utf-8', nrows=nrows)
 df_test=df_test.drop(['rankPoints'],axis=1)
 return df_test
rfc1.fit(X1, Y1 )<predict_on_test>
df=import_training_data()
pred=rfc1.predict(X1_test )<compute_test_metric>
df_squad=df[(df['matchType'].str.contains('squad')) |(df['matchType'].str.contains('flare')) ].drop('matchType',axis=1)
 df_duo=df[df['matchType'].str.contains('duo')].drop('matchType',axis=1)
 df_solo=df[df['matchType'].str.contains('solo')].drop(columns=['DBNOs','revives'] ).drop('matchType',axis=1)
 df_crash=df[df['matchType'].str.contains('crash')].drop('matchType',axis=1 ).drop('killPoints', axis=1 ).drop('winPoints', axis=1 )
print("Accuracy for Random Forest on CV data: ",accuracy_score(Y1_test,pred))<import_modules>
df_test=import_test_data()
df_test_squad=df_test[(df_test['matchType'].str.contains('squad')) |(df_test['matchType'].str.contains('flare')) ].drop('matchType',axis=1)
 df_test_duo=df_test[df_test['matchType'].str.contains('duo')].drop('matchType',axis=1)
 df_test_solo=df_test[df_test['matchType'].str.contains('solo')].drop('matchType',axis=1 ).drop(columns=['DBNOs','revives'])
 df_test_crash=df_test[df_test['matchType'].str.contains('crash')].drop('matchType',axis=1 ).drop('killPoints', axis=1 ).drop('winPoints', axis=1 )
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, BaggingClassifier, ExtraTreesClassifier
from sklearn.tree import DecisionTreeClassifier
from xgboost import XGBClassifier, plot_importance
from sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV<define_search_model>
len_df_test=len(df_test)
 del df
del df_test
clf_rf = RandomForestClassifier()
clf_et = ExtraTreesClassifier()
clf_bc = BaggingClassifier()
clf_ada = AdaBoostClassifier()
clf_dt = DecisionTreeClassifier()
clf_xg = XGBClassifier()
clf_lr = LogisticRegression()
clf_svm = SVC()<prepare_x_and_y>
def combine_training_teams(df):
matches = list(dict(tuple(df.groupby('matchId',axis=0)) ).values())
 matches_combined_teams=[]
for i in matches:
num_memb=i['groupId'].value_counts(sort=False ).to_frame().reset_index().rename(columns={"index": "groupId", "groupId": "numMembers"})
 df_red_avg_tmp=i.groupby('groupId' ).agg('mean' ).sort_values(by='groupId')
 df_red_avg_tmp.sort_values(by='groupId', inplace=True)
 df_red_avg_tmp['numMembers']=num_memb.sort_values(by='groupId')['numMembers'].values
df_red_avg_tmp=df_red_avg_tmp.drop(['matchDuration','numGroups'],axis=1)
 matches_combined_teams.append(df_red_avg_tmp.reset_index())
 return matches_combined_teams
def combine_testing_teams(df):
matches_test = list(dict(tuple(df.groupby('matchId',axis=0)) ).values())
 matches_combined_teams_test=[]
for i in matches_test:
dict_groupid_to_id={}
for idx, row in i.iterrows() :
dict_groupid_to_id[row['Id']]=row['groupId']
num_memb_tmp=i['groupId'].value_counts(sort=False ).to_frame().reset_index().rename(columns={"index": "groupId", "groupId": "numMembers"})
 df_red_avg_tmp=i.groupby('groupId' ).agg('mean' ).sort_values(by='groupId')
 df_red_avg_tmp.sort_values(by='groupId', inplace=True)
 df_red_avg_tmp['numMembers']=num_memb_tmp.sort_values(by='groupId')['numMembers'].values
df_red_avg_tmp=df_red_avg_tmp.drop(['matchDuration','numGroups'],axis=1)
 matches_combined_teams_test.append([df_red_avg_tmp.reset_index() ,dict_groupid_to_id])
 return matches_combined_teams_test
npX = np.array(X ).copy()
npy = np.array(Y ).copy()<compute_train_metric>
def filter_small_teams(list_of_matches):
df_small_matches_1=[]
small_matches_2=[]
rest=[]
for i in list_of_matches:
if len(i)<2:
df_small_matches_1.append(i)
 elif len(i)>=2 and len(i)<3:
small_matches_2.append(i)
 else:
rest.append(i)
 return df_small_matches_1, small_matches_2, rest
def filter_small_teams_test(list_of_matches):
df_small_matches_1=[]
small_matches_2=[]
rest=[]
for df, dic in list_of_matches:
if len(df)<2:
df_small_matches_1.append([df,dic])
 elif len(df)>=2 and len(df)<3:
small_matches_2.append([df,dic])
 else:
rest.append([df,dic])
 return df_small_matches_1, small_matches_2, rest
Classifiers = ['RandomForest','ExtraTrees','Bagging','AdaBoost','DecisionTree','XGBoost','LogisticRegression','SVM']
scores = []
models = [clf_rf, clf_et, clf_bc, clf_ada, clf_dt, clf_xg, clf_lr, clf_svm]
for model in models:
score = cross_val_score(model, npX, npy, scoring = 'accuracy', cv = 10, n_jobs = -1 ).mean()
scores.append(score )<create_dataframe>
def prepare_pca(matches_combined_teams, in_components=None):
print(len(matches_combined_teams[1].drop(['winPlacePerc'],axis=1 ).columns))
ipca = IncrementalPCA(n_components=in_components)
 for i in matches_combined_teams:
ipca.partial_fit(i.drop(['winPlacePerc'],axis=1 ).values)
 print(ipca.explained_variance_ratio_)
 print(ipca.n_components_)
 return ipca
def prepare_pca_input(df_train, df_test, dfeatures=None):
df_train_in=[]
df_train_groupids=[]
for i in df_train:
df_train_groupids.append(i[['groupId','maxPlace']])
 if len(dfeatures)>0:
df_train_in.append(i.drop(['groupId','maxPlace'],axis=1 ).drop(dfeatures,axis=1 ).reindex(sorted(i.drop(['groupId','maxPlace'],axis=1 ).drop(dfeatures,axis=1 ).columns), axis=1))
else:
df_train_in.append(i.drop(['groupId','maxPlace'],axis=1 ).reindex(sorted(i.drop(['groupId','maxPlace'],axis=1 ).columns), axis=1))
df_test_in=[]
df_test_groupids=[]
for df, dic in df_test:
df_test_groupids.append([df[['groupId','maxPlace']],dic])
 if len(dfeatures)>0:
df_test_in.append(df.drop(['groupId','maxPlace'],axis=1 ).drop(dfeatures,axis=1 ).reindex(sorted(df.drop(['groupId','maxPlace'],axis=1 ).drop(dfeatures,axis=1 ).columns), axis=1))
else:
df_test_in.append(df.drop(['groupId','maxPlace'],axis=1 ).reindex(sorted(df.drop(['groupId','maxPlace'],axis=1 ).columns), axis=1))
return df_train_in, df_train_groupids, df_test_in, df_test_groupids
def pca_transform_data(matches_combined_teams, matches_combined_teams_test, ipca):
x_train_pca = []
for i in matches_combined_teams:
x_train_pca.append(ipca.transform(i.drop('winPlacePerc',axis=1 ).values))
y_train_pca=[]
for i in matches_combined_teams:
y_train_pca.append(i['winPlacePerc'].values.reshape(-1, 1))
x_test_pca = []
for i in matches_combined_teams_test:
x_test_pca.append(ipca.transform(i.values))
return x_train_pca, y_train_pca, x_test_pca
mode = pd.DataFrame(scores, index = Classifiers, columns = ['score'] ).sort_values(by = 'score',
ascending = False )<define_search_space>
def scale_data(x_train_pca, x_test_pca):
scaler = MinMaxScaler()
for i in x_train_pca:
scaler.partial_fit(i)
 x_train_scaled=[]
for i in x_train_pca:
x_train_scaled.append(scaler.transform(i))
x_test_scaled=[]
for i in x_test_pca:
x_test_scaled.append(scaler.transform(i))
return x_train_scaled, x_test_scaled
parameters_xg = {'max_depth':[3,6,7], 'learning_rate': [0.1,0.2], 'n_estimators': [300,200],
'min_child_weight': [4], 'reg_alpha': [6,0], 'reg_lambda': [1,8],'max_delta_step':[2],
'gamma':[0],'seed':[1]}
parameters_svm = {'C':[0.9,0.01],'kernel':['rbf','linear'], 'gamma':[0,0.1,'auto'], 'probability':[True,False],
'random_state':[0,7,16],'decision_function_shape':['ovo','ovr'],'degree':[3,4,10]} 
<train_on_grid>
def mlp_evaluate(x_train, y_train, df_train_groupids, training_level=100, verbose=False):
working_sample=np.random.choice(len(x_train), training_level*5)
 mlp=MLPRegressor(solver='adam',hidden_layer_sizes=(50,50,))
train_counter=0
lenxtrain=len(x_train)
 kfold_score=[]
for k in range(training_level):
for i in range(4):
mlp.partial_fit(x_train[working_sample[train_counter]], y_train[working_sample[train_counter]].ravel())
 train_counter+=1
y_pred_kfold=pd.DataFrame(mlp.predict(x_train[working_sample[train_counter]]), columns=['ranks'])
 y_pred_kfold=y_pred_kfold.join(df_train_groupids[working_sample[train_counter]] ).sort_values(by='ranks', ascending=False)
 y_test=pd.DataFrame(y_train[working_sample[train_counter]], columns=['winPlacePerc'] ).join(df_train_groupids[working_sample[train_counter]] ).sort_values(by='groupId', ascending=True)
 tmp_winPlacePerc=[]
for i in range(1,len(y_pred_kfold)+1):
tmp_winPlacePerc.append([y_pred_kfold['groupId'].values[i-1],(len(y_pred_kfold)-i)/(len(y_pred_kfold)-1)])
 tmp_winPlacePerc=pd.DataFrame(tmp_winPlacePerc, columns=['groupId','winPlacePerc'] ).sort_values(by='groupId', ascending=True)
 kfold_score.append(mean_absolute_error(y_test['winPlacePerc'],tmp_winPlacePerc['winPlacePerc']))
train_counter+=1
if verbose:
print("Evaluating "+str(k)+"/"+str(training_level))
print("Current mean bsolut error: "+str(np.mean(kfold_score)))
 print("Average mean absolute error: "+str(np.mean(kfold_score)))
 return kfold_score
def grid(model,parameters):
grid = GridSearchCV(estimator = model, param_grid = parameters, cv = 10,
scoring = 'accuracy')
 grid.fit(npX,npy)
 return grid.best_score_, grid.best_estimator_.get_params()<find_best_params>
def mlp_train(x_train, y_train):
mlp=MLPRegressor(solver='adam',hidden_layer_sizes=(50,50,))
lenxtrain=len(x_train)
 for i in range(lenxtrain):
mlp.partial_fit(x_train[i], y_train[i].ravel())
 return mlp
best_score_svm, best_params_svm = grid(clf_svm, parameters_svm)
 print(best_score_svm )<normalization>
def predict_winPlacePerc(x_test, df_test_groupids, mlp, verbose=False):
winPlacePerc=[]
num_matches=len(x_test)
 for k in range(num_matches):
if verbose and k%100==0:
print("Predicting match "+str(k)+"/"+str(num_matches))
y_pred=pd.DataFrame(mlp.predict(x_test[k]), columns=['ranks'])
 y_pred=y_pred.join(df_test_groupids[k][0] ).sort_values(by='ranks', ascending=False)
 tmp_winPlacePerc=[]
for i in range(1,len(y_pred)+1):
tmp_winPlacePerc.append([y_pred['groupId'].values[i-1],(len(y_pred)-i)/(len(y_pred)-1)])
 winPlacePerc.append(pd.DataFrame(tmp_winPlacePerc, columns=['groupId','winPlacePerc'] ).sort_values(by='groupId', ascending=True))
result_list=[]
counter=0
for df, el in df_test_groupids:
match_results=[]
for i, gid in el.items() :
wpp=winPlacePerc[counter][winPlacePerc[counter]['groupId']==gid]['winPlacePerc'].values
match_results.append([i,wpp])
 result_list.append(pd.DataFrame(match_results, columns=['Id','winPlacePerc']))
counter+=1
winPlacePerc_tot=pd.concat(result_list)
 winPlacePerc_tot['winPlacePerc']=winPlacePerc_tot['winPlacePerc'].str[0]
return winPlacePerc_tot
x = StandardScaler().fit_transform(X)
 X_train, X_test, y_train, y_test = train_test_split(x, Y, test_size =.2)
 X_train_reduced = PCA(n_components = 2 ).fit_transform(X_train)
 X_test_reduced = PCA(n_components= 2 ).fit_transform(X_test )<train_model>
def predict_one_team_matches(df):
result_list=[]
counter=0
for df, dic in df:
match_results=[]
for i, gid in dic.items() :
match_results.append([i,0.0])
 result_list.append(pd.DataFrame(match_results, columns=['Id','winPlacePerc']))
counter+=1
if len(result_list)>0:
return pd.concat(result_list)
 else:
return []
def predict_two_team_matches(df):
result_list=[]
counter=0
for df, dic in df:
match_results=[]
killpointsMax=np.max([df['killPlace'].values[0],df['killPlace'].values[1]])
 maxKillpointsGId=df[df['killPlace']==killpointsMax]['groupId'].values[0]
for i, gid in dic.items() :
if maxKillpointsGId in gid:
match_results.append([i,0.0])
 else:
match_results.append([i,1.0])
 result_list.append(pd.DataFrame(match_results, columns=['Id','winPlacePerc']))
counter+=1
if len(result_list)>0:
return pd.concat(result_list)
 else:
return []
def boundaries(model, heading, best_params):
Model = model(**best_params)
 Model.fit(X_train_reduced, y_train)
 X_set, y_set = np.concatenate([X_train_reduced, X_test_reduced], axis = 0), np.concatenate([y_train, y_test], axis = 0)
 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, Model.predict(np.array([X1.ravel() , X2.ravel() ] ).T ).reshape(X1.shape),
alpha = 0.5, cmap = ListedColormap(( 'k', 'blue')))
 plt.xlim(X1.min() , X1.max())
 plt.ylim(X2.min() , X2.max())
 for i, j in enumerate(np.unique(y_set)) :
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(( 'red', 'green'))(i), label = j)
 plt.xticks(fontsize = 3)
 plt.yticks(fontsize = 3 )<train_on_grid>
df_red_squad=combine_training_teams(df_squad )
clf_svm = SVC(**best_params_svm)
 clf_svm.fit(npX,npy)
<drop_column>
df_red_squad_1, df_red_squad_2, df_red_squad=filter_small_teams(df_red_squad )
test_df = test[cols]<predict_on_test>
df_test_per_match_squad = combine_testing_teams(df_test_squad )
nptest = np.array(test_df)
 pred = clf_svm.predict(nptest)
<create_dataframe>
df_test_per_match_squad_1, df_test_per_match_squad_2, df_test_per_match_squad=filter_small_teams_test(df_test_per_match_squad )
predictions = pd.DataFrame(pred, index = test_df.index, columns = ['Survived'])
<create_dataframe>
df_squad.drop(['killPoints','maxPlace','roadKills','teamKills','vehicleDestroys','winPoints','swimDistance'],axis=1 ).corr().style.format("{:.2}" ).background_gradient(cmap=plt.get_cmap('coolwarm'), axis=1 )
pred_df=pd.DataFrame(test['PassengerId'])
 pred_df['Survived']=predictions<save_to_csv>
del df_squad
del df_test_squad
pred_df.to_csv('predictions_svm_titanic_final.csv',index=False )<import_modules>
drop_features_squad=['killPoints','roadKills','teamKills','vehicleDestroys','winPoints']
import numpy as np
import pandas as pd
import scipy.special
import matplotlib.pyplot as plt
import os
import random<import_modules>
df_train_in_squad, df_train_groupids_squad, df_test_in_squad, df_test_groupids_squad = prepare_pca_input(df_red_squad, df_test_per_match_squad, drop_features_squad )
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from sklearn.metrics import accuracy_score
from xgboost import XGBClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier<set_options>
pca_squad=prepare_pca(df_train_in_squad, in_components=None )
warnings.filterwarnings("ignore" )<load_from_csv>
x_train_pca_squad, y_train_pca_squad, x_test_pca_squad=pca_transform_data(df_train_in_squad, df_test_in_squad, pca_squad )
train_data = pd.read_csv(path_in+'train.csv', index_col=0)
 test_data = pd.read_csv(path_in+'test.csv', index_col=0)
 samp_subm = pd.read_csv(path_in+'gender_submission.csv', index_col=0 )<count_values>
del df_red_squad
del df_test_in_squad
del df_train_in_squad
print('number train samples: ', len(train_data.index))
print('number test samples: ', len(test_data.index))
print('number features: ', len(train_data.columns)-1 )<count_missing_values>
x_train_scaled_squad, x_test_scaled_squad=scale_data(x_train_pca_squad, x_test_pca_squad )
train_data.isnull().sum()<count_missing_values>
eval_score_squad=mlp_evaluate(x_train_scaled_squad, y_train_pca_squad, df_train_groupids_squad, training_level=10000, verbose=False )
cols_with_missing_train = [col for col in train_data.columns if train_data[col].isnull().any() ]
cols_with_missing_test = [col for col in test_data.columns if test_data[col].isnull().any() ]<count_missing_values>
mlp_squad=mlp_train(x_train_scaled_squad, y_train_pca_squad )
print('train missing values: {:0.2f}%'.format(100*train_data['Age'].isna().sum() /len(train_data)))
 print('test missing values: {:0.2f}%'.format(100*test_data['Age'].isna().sum() /len(test_data)) )<categorify>
winPlacePerc_one_team_squad=predict_one_team_matches(df_test_per_match_squad_1)
 winPlacePerc_two_teams_squad=predict_two_team_matches(df_test_per_match_squad_2 )
def fill_age(s):
if np.isnan(s)== False:
return s
else:
return random.randrange(age_mean-age_std, age_mean+age_std )<feature_engineering>
winPlacePerc_squad = predict_winPlacePerc(x_test_scaled_squad, df_test_groupids_squad, mlp_squad, verbose=False)
 winPlacePerc_squad = winPlacePerc_squad.append(winPlacePerc_two_teams_squad )
train_data['Age'] = train_data['Age'].apply(fill_age)
 test_data['Age'] = test_data['Age'].apply(fill_age )<count_missing_values>
df_red_duo=combine_training_teams(df_duo )
print('train missing values: {:0.2f}%'.format(100*train_data['Cabin'].isna().sum() /len(train_data)))
 print('test missing values: {:0.2f}%'.format(100*test_data['Cabin'].isna().sum() /len(test_data)) )<data_type_conversions>
df_red_duo_1, df_red_duo_2, df_red_duo=filter_small_teams(df_red_duo )
train_data['Cabin'] = train_data['Cabin'].fillna('Unknown', inplace=False)
 test_data['Cabin'] = test_data['Cabin'].fillna('Unknown', inplace=False )<count_missing_values>
df_test_per_match_duo = combine_testing_teams(df_test_duo )
print('train missing values:', train_data['Embarked'].isna().sum() )<feature_engineering>
df_test_per_match_duo_1, df_test_per_match_duo_2, df_test_per_match_duo=filter_small_teams_test(df_test_per_match_duo )
train_data['Embarked'] = train_data['Embarked'].fillna('Unknown', inplace=False )<count_missing_values>
df_duo.drop(['killPoints','roadKills','teamKills','vehicleDestroys','winPoints'],axis=1 ).corr().style.format("{:.2}" ).background_gradient(cmap=plt.get_cmap('coolwarm'), axis=1 )
print('test missing values:', test_data['Fare'].isna().sum() )<feature_engineering>
del df_duo
del df_test_duo
mean = test_data['Fare'].mean()
test_data['Fare'] = test_data['Fare'].fillna(mean, inplace=False )<feature_engineering>
drop_features_duo=['killPoints','roadKills','teamKills','vehicleDestroys','winPoints']
train_data['FamilySize'] = train_data['SibSp'] + train_data['Parch'] + 1
test_data['FamilySize'] = test_data['SibSp'] + test_data['Parch'] + 1<groupby>
df_train_in_duo, df_train_groupids_duo, df_test_in_duo, df_test_groupids_duo = prepare_pca_input(df_red_duo, df_test_per_match_duo, dfeatures=drop_features_duo )
def family_group(s):
if(s >= 2)&(s <= 4):
return 2
elif(( s > 4)&(s <= 7)) |(s == 1):
return 1
elif(s > 7):
return 0<feature_engineering>
pca_duo=prepare_pca(df_train_in_duo, in_components=None )
train_data['FamilyGroup'] = train_data['FamilySize'].apply(family_group)
 test_data['FamilyGroup'] = test_data['FamilySize'].apply(family_group )<filter>
x_train_pca_duo, y_train_pca_duo, x_test_pca_duo=pca_transform_data(df_train_in_duo, df_test_in_duo, pca_duo )
def IsAlone(s):
if s==1:
return 1
else:
return 0<feature_engineering>
del df_red_duo
del df_test_in_duo
del df_train_in_duo
train_data['IsAlone'] = train_data['FamilySize'].apply(IsAlone)
 test_data['IsAlone'] = test_data['FamilySize'].apply(IsAlone )<count_values>
x_train_scaled_duo, x_test_scaled_duo=scale_data(x_train_pca_duo, x_test_pca_duo )
ticket_group = dict(( train_data['Ticket'].append(test_data['Ticket'])).value_counts())
 train_data['TicketGroup'] = train_data['Ticket'].apply(lambda x: ticket_group[x])
 test_data['TicketGroup'] = test_data['Ticket'].apply(lambda x: ticket_group[x] )<feature_engineering>
eval_score_duo=mlp_evaluate(x_train_scaled_duo, y_train_pca_duo, df_train_groupids_duo, training_level=10000, verbose=False )
train_data['Title'] = train_data['Name'].apply(lambda x:x.split(',')[1].split('.')[0].strip())
 test_data['Title'] = test_data['Name'].apply(lambda x:x.split(',')[1].split('.')[0].strip())
 title_dict = {}
title_dict.update(dict.fromkeys(['Capt', 'Col', 'Major', 'Dr', 'Rev'], 'Officer'))
title_dict.update(dict.fromkeys(['Don', 'Sir', 'the Countess', 'Dona', 'Lady'], 'Royalty'))
title_dict.update(dict.fromkeys(['Mme', 'Ms', 'Mrs'], 'Mrs'))
title_dict.update(dict.fromkeys(['Mlle', 'Miss'], 'Miss'))
title_dict.update(dict.fromkeys(['Mr'], 'Mr'))
title_dict.update(dict.fromkeys(['Master','Jonkheer'], 'Master'))
train_data['Title'] = train_data['Title'].map(title_dict)
 test_data['Title'] = test_data['Title'].map(title_dict )<categorify>
mlp_duo=mlp_train(x_train_scaled_duo, y_train_pca_duo )
def age_group(s):
if s == 0:
return -1
elif(s > 0)&(s <= 13):
return 1
elif(s > 13)&(s <= 20):
return 2
elif(s > 20)&(s <= 30):
return 3
elif(s > 30)&(s <= 40):
return 4
elif(s > 40)&(s <= 50):
return 5
elif(s > 50)&(s <= 60):
return 6
elif(s > 60)&(s <= 70):
return 7
elif(s > 70)&(s <= 80):
return 8<feature_engineering>
winPlacePerc_one_team_duo=predict_one_team_matches(df_test_per_match_duo_1)
 winPlacePerc_two_teams_duo=predict_two_team_matches(df_test_per_match_duo_2 )
train_data['AgeGroup'] = train_data['Age'].apply(age_group)
 test_data['AgeGroup'] = test_data['Age'].apply(age_group )<feature_engineering>
winPlacePerc_duo = predict_winPlacePerc(x_test_scaled_duo, df_test_groupids_duo, mlp_duo, verbose=False)
 winPlacePerc_duo=winPlacePerc_duo.append(winPlacePerc_one_team_duo )
train_data['FareGroup'] = train_data['Fare'].apply(fare_group)
 test_data['FareGroup'] = test_data['Fare'].apply(fare_group )<feature_engineering>
df_solo['winPlacePerc'].fillna(0.0, inplace=True )
train_data['LastName'] = train_data['Name'].apply(last_name)
 test_data['LastName'] = test_data['Name'].apply(last_name )<feature_engineering>
df_red_solo=combine_training_teams(df_solo )
for row in train_data.index:
if train_data.loc[row, 'Sex']=='female' or train_data.loc[row, 'Age']<14:
train_data.loc[row, 'WomenOrChild'] = 1
else:
train_data.loc[row, 'WomenOrChild'] = 0
for row in test_data.index:
if test_data.loc[row, 'Sex']=='female' or test_data.loc[row, 'Age']<14:
test_data.loc[row, 'WomenOrChild'] = 1
else:
test_data.loc[row, 'WomenOrChild'] = 0<feature_engineering>
df_red_solo_1, df_red_solo_2, df_red_solo=filter_small_teams(df_red_solo )
train_data['FarePerPerson']=train_data['Fare']/(train_data['FamilySize']+1)
 test_data['FarePerPerson']=test_data['Fare']/(test_data['FamilySize']+1 )<feature_engineering>
df_test_per_match_solo = combine_testing_teams(df_test_solo )
train_data['AgePclass'] = train_data['Age']*train_data['Pclass']
test_data['AgePclass'] = test_data['Age']*test_data['Pclass']<feature_engineering>
df_test_per_match_solo_1, df_test_per_match_solo_2, df_test_per_match_solo=filter_small_teams_test(df_test_per_match_solo )
train_data['Cabin'] = train_data['Cabin'].str[0]
test_data['Cabin'] = test_data['Cabin'].str[0]<categorify>
del df_solo
del df_test_solo
features_cat = ['Sex', 'Cabin', 'Embarked', 'Title']
le = LabelEncoder()
for col in features_cat:
le.fit(train_data[col])
 train_data[col] = le.transform(train_data[col])
 test_data[col] = le.transform(test_data[col] )<categorify>
drop_features_solo=['killPoints','roadKills','teamKills','winPoints']
train_data_dummy = pd.get_dummies(train_data['Pclass'], prefix = 'Pclass')
 train_data = pd.concat([train_data, train_data_dummy], axis=1)
 del train_data['Pclass']
test_data_dummy = pd.get_dummies(test_data['Pclass'], prefix = 'Pclass')
 test_data = pd.concat([test_data, test_data_dummy], axis=1)
 del test_data['Pclass']<define_variables>
df_train_in_solo, df_train_groupids_solo, df_test_in_solo, df_test_groupids_solo = prepare_pca_input(df_red_solo, df_test_per_match_solo, dfeatures=drop_features_solo )
no_features = ['Survived', 'Name', 'Ticket', 'LastName']<prepare_x_and_y>
pca_solo=prepare_pca(df_train_in_solo, in_components=None )
X_train = train_data[train_data.columns.difference(no_features)].copy(deep=False)
 y_train = train_data['Survived']
X_test = test_data[test_data.columns.difference(no_features)].copy(deep=False )<normalization>
x_train_pca_solo, y_train_pca_solo, x_test_pca_solo=pca_transform_data(df_train_in_solo, df_test_in_solo, pca_solo )
min_max = MinMaxScaler()
X_train_scaled = min_max.fit_transform(X_train)
 X_test_scaled = min_max.transform(X_test )<train_on_grid>
del df_red_solo
del df_test_in_solo
del df_train_in_solo
param_grid = {'criterion': ['gini'],
'max_features': [None, 'auto', 'sqrt', 'log2'],
'max_depth': [i for i in range(1, 6)],
'class_weight': [None, 'balanced'],
'min_samples_split': [2, 4, 6, 8, 10 ,12],
'min_samples_leaf': [1, 2, 3, 4],
'random_state': [2020]}
grid = GridSearchCV(DecisionTreeClassifier() , param_grid=param_grid, cv=5, scoring='accuracy')
 grid.fit(X_train_scaled, y_train)
 best_params = grid.best_params_
print('Best score of cross validation: {:.2f}'.format(grid.best_score_))
print('Best parameters:', best_params )<choose_model_class>
x_train_scaled_solo, x_test_scaled_solo=scale_data(x_train_pca_solo, x_test_pca_solo )
model = DecisionTreeClassifier()
model.set_params(**best_params )<train_model>
eval_score_solo=mlp_evaluate(x_train_scaled_solo, y_train_pca_solo, df_train_groupids_solo, training_level=10000, verbose=False )
model.fit(X_train_scaled, y_train )<predict_on_test>
mlp_solo=mlp_train(x_train_scaled_solo, y_train_pca_solo )
y_test = model.predict(X_test_scaled )<save_to_csv>
winPlacePerc_one_teams_solo=predict_one_team_matches(df_test_per_match_solo_1)
 winPlacePerc_two_teams_solo=predict_two_team_matches(df_test_per_match_solo_2 )
output = pd.DataFrame({'PassengerId': samp_subm.index,
'Survived': y_test})
 output.to_csv('submission.csv', index=False )<count_values>
winPlacePerc_solo = predict_winPlacePerc(x_test_scaled_solo, df_test_groupids_solo, mlp_solo, verbose=False)
 winPlacePerc_solo = winPlacePerc_solo.append(winPlacePerc_one_teams_solo ).append(winPlacePerc_two_teams_solo )
output['Survived'].value_counts()<set_options>
df_red_crash=combine_training_teams(df_crash )
sns.set(context='notebook', style='white', palette='colorblind' )<import_modules>
df_red_crash_1, df_red_crash_2, df_red_crash=filter_small_teams(df_red_crash )
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
from sklearn.neighbors import KNeighborsClassifier
from xgboost import XGBClassifier
from sklearn.model_selection import StratifiedKFold, GridSearchCV, learning_curve, cross_val_score<load_from_csv>
df_test_per_match_crash = combine_testing_teams(df_test_crash )
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 df = pd.concat([train, test], axis=0, ignore_index=True)
 print(f'Train:{train.shape}
Test:{test.shape}
Df:{df.shape}' )<count_missing_values>
df_test_per_match_crash_1, df_test_per_match_crash_2, df_test_per_match_crash=filter_small_teams_test(df_test_per_match_crash )
df.isna().sum() [df.isna().sum() >0]<data_type_conversions>
df_crash.drop(['headshotKills','numGroups','swimDistance','teamKills'],axis=1 ).corr().style.format("{:.2}" ).background_gradient(cmap=plt.get_cmap('coolwarm'), axis=1 )
df.Sex=df.Sex.map({'male':0, 'female':1} ).astype('int' )<feature_engineering>
del df_crash
del df_test_crash
df['Family_Size']=df.SibSp + df.Parch
df.groupby('Family_Size')['Survived'].mean()<count_values>
drop_features_crash=['headshotKills','swimDistance','teamKills']
df.Title.value_counts()<feature_engineering>
df_train_in_crash, df_train_groupids_crash, df_test_in_crash, df_test_groupids_crash = prepare_pca_input(df_red_crash, df_test_per_match_crash, dfeatures=drop_features_crash )
df['Family_Size'] = df['Parch'] + df['SibSp']<feature_engineering>
pca_crash=prepare_pca(df_train_in_crash, in_components=None )
df['Last_Name'] = df['Name'].apply(lambda x: str.split(x, ",")[0])
 df['Fare'].fillna(df['Fare'].mean() , inplace=True)
 DEFAULT_SURVIVAL_VALUE = 0.5
df['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in df[['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.loc[df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 0
for _, grp_df in df.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.loc[df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 0<count_values>
x_train_pca_crash, y_train_pca_crash, x_test_pca_crash=pca_transform_data(df_train_in_crash, df_test_in_crash, pca_crash )
df['Family_Survival'].value_counts()<categorify>
del df_red_crash
del df_test_in_crash
del df_train_in_crash
df['Fare'].fillna(df['Fare'].median() , inplace = True)
 df['FareBin'] = pd.qcut(df['Fare'], 5)
 label = LabelEncoder()
df['FareBin_Code'] = label.fit_transform(df['FareBin'])
 df.drop(['Fare'], 1, inplace=True )<count_values>
x_train_scaled_crash, x_test_scaled_crash=scale_data(x_train_pca_crash, x_test_pca_crash )
df['FareBin_Code'].value_counts()<feature_engineering>
eval_score_crash=mlp_evaluate(x_train_scaled_crash, y_train_pca_crash, df_train_groupids_crash, training_level=len(x_train_scaled_crash), verbose=False )
titles = ['Dr', 'Master', 'Miss', 'Mr', 'Mrs', 'Rev']
for title in titles:
age_to_impute = df.groupby('Title')['Age'].median() [titles.index(title)]
df.loc[(df['Age'].isnull())&(df['Title'] == title), 'Age'] = age_to_impute<categorify>
mlp_crash=mlp_train(x_train_scaled_crash, y_train_pca_crash )
df['AgeBin'] = pd.qcut(df['Age'], 4)
 label = LabelEncoder()
df['AgeBin_Code'] = label.fit_transform(df['AgeBin'] )<count_values>
winPlacePerc_one_teams_crash=predict_one_team_matches(df_test_per_match_crash_1)
 winPlacePerc_two_teams_crash=predict_two_team_matches(df_test_per_match_crash_2 )
df['AgeBin_Code'].value_counts()<drop_column>
winPlacePerc_crash = predict_winPlacePerc(x_test_scaled_crash, df_test_groupids_crash, mlp_crash, verbose=False )
df.drop(['Name', 'PassengerId', 'SibSp', 'Parch', 'Ticket', 'Cabin',
'Embarked', 'Last_Name', 'FareBin', 'AgeBin', 'Survived', 'Title', 'Age'], axis = 1, inplace = True )<prepare_x_and_y>
eval_score=np.concatenate(( np.array(eval_score_squad),np.array(eval_score_duo),np.array(eval_score_solo),np.array(eval_score_crash)))
 eval_score_avg=np.mean(eval_score)
 print("Evaluation mean absolute error: "+str(eval_score_avg))
X_train = df[:len(train)]
X_test = df[len(train):]
y_train = train['Survived']<normalization>
df_sub=winPlacePerc_squad.append(winPlacePerc_duo ).append(winPlacePerc_solo ).append(winPlacePerc_crash )
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
 X_test = scaler.transform(X_test )<choose_model_class>
df_sub=df_sub.reset_index().drop('index',axis=1 )
kfold = StratifiedKFold(n_splits=8 )<define_search_space>
df_sub.to_csv('submission.csv', index=False )