Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
if not sys.warnoptions:
warnings.simplefilter("ignore")
train = pd.read_csv(".. /input/train_V2.csv")
 test = pd.read_csv(".. /input/test_V2.csv" )<correct_missing_values>
data_train = pd.read_csv(".. /input/train.csv")
 data_test= pd.read_csv(".. /input/test.csv" )
train = train.dropna()<feature_engineering>
print("Training Data shape:", data_train.shape)
 print("Test Data shape:", data_test.shape )
train['rideDistance'] =(train['rideDistance']/10 ).round(0)
 train['swimDistance'] =(train['swimDistance']/10 ).round(0)
 train['walkDistance'] =(train['walkDistance']/10 ).round(0 )<feature_engineering>
label = data_train['Survived']
test['rideDistance'] =(test['rideDistance']/10 ).round(0)
 test['swimDistance'] =(test['swimDistance']/10 ).round(0)
 test['walkDistance'] =(test['walkDistance']/10 ).round(0 )<feature_engineering>
if label.isnull().sum() ==0:
print("No missing values")
 else:
print(label.isnull().sum() , 'missing values found in dataset' )
test["winPlacePerc"] = -1<concatenate>
for column in data_train.columns:
print(column, len(data_train[column].unique()))
df = pd.concat([train, test] )<drop_column>
print('Amount of missing data in Fare for train:', data_train.Fare.isnull().sum())
 print('Amount of missing data in Fare for test:',data_test.Fare.isnull().sum())
 print("--------------------------------------------------")
 print('Amount of missing data in Embarked for train:',data_train.Embarked.isnull().sum())
 print('Amount of missing data in Embarked for test:', data_test.Embarked.isnull().sum() )
del train
del test<feature_engineering>
data_train['Embarked'] = data_train['Embarked'].fillna("S")
 data_test['Fare'] = data_test['Fare'].fillna(data_train['Fare'].median() )
df["Id"] = df.index<filter>
print(data_train.Age.isnull().sum())
 print(data_test.Age.isnull().sum() )
<filter>
data_train['Age_NA'] =np.where(data_train.Age.isnull() , 1, 0)
 data_test['Age_NA'] =np.where(data_test.Age.isnull() , 1, 0 )
duo = df[(df['matchType']=='duo')|(df['matchType']=='normal-duo')|(df['matchType']=='duo-fpp')|(df['matchType']=='normal-duo-fpp')]
squad = df[(df['matchType']=='squad')|(df['matchType']=='normal-squad')|(df['matchType']=='squad-fpp')|(df['matchType']=='normal-squad-fpp')]
solo = df[(df['matchType']=='solo')|(df['matchType']=='normal-solo')|(df['matchType']=='solo-fpp')|(df['matchType']=='normal-solo-fpp')]
flare = df[(df['matchType']=='flaretpp')|(df['matchType']=='flarefpp')]
crash = df[(df['matchType']=='crashtpp')|(df['matchType']=='crashfpp')]<define_variables>
data_train['Age_mean'] =np.where(data_train.Age.isnull() , data_train['Age'].mean() , data_train['Age'])
 data_test['Age_mean'] =np.where(data_test.Age.isnull() , data_test['Age'].mean() , data_test['Age'])
dp_by_type = {'flare':[flare,62],
'crash':[crash,149],
'squad':[squad,53635],
'solo':[solo,16165],
'duo':[duo,29691]
}<save_to_csv>
for column in data_train.columns:
print(column, len(data_train[column].unique()))
for name,ele in dp_by_type.items() :
print(name + " : " + str(len(ele[0])))
 ele[0].to_csv(name+'.csv', index=False)
 ele[0] = 0<drop_column>
data_train = data_train.drop(['PassengerId'], axis=1)
 data_test = data_test.drop(['PassengerId'], axis=1 )
del duo,squad,solo,flare,crash<set_options>
def ticket_sep(data_ticket):
ticket_type = []
for i in range(len(data_ticket)) :
ticket =data_ticket.iloc[i]
for c in string.punctuation:
ticket = ticket.replace(c,"")
 splited_ticket = ticket.split(" ")
 if len(splited_ticket)== 1:
ticket_type.append('NO')
 else:
ticket_type.append(splited_ticket[0])
 return ticket_type
del df
gc.collect()<concatenate>
data_train["ticket_type"] = ticket_sep(data_train.Ticket)
 data_train.head()
def featureEngineering(df):
return featureEngineeringSecond(reduce_mem_usage(featureEngineeringFirst(df)) )<feature_engineering>
data_test["ticket_type"]= ticket_sep(data_test.Ticket)
 data_test.head()
def items(df):
df['items'] = df['heals'] + df['boosts']
return df
def survival(df):
df["survival"] = df["revives"] + df["boosts"] + df["heals"]
return df
def players_in_team(df):
agg = df.groupby(['groupId'] ).size().to_frame('players_in_team')
 return df.merge(agg, how='left', on=['groupId'])
 def total_distance(df):
df['total_distance'] = df['rideDistance'] + df['swimDistance'] + df['walkDistance']
return df
def total_time_by_distance(df):
df["total_time_by_distance"] = df["rideDistance"]/5+df["walkDistance"]+df["swimDistance"]*5
return df
def headshotKills_over_kills(df):
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['headshotKills_over_kills'].fillna(0, inplace=True)
 return df
def teamwork(df):
df['teamwork'] = df['assists'] + df['revives']
return df
def total_items_acquired(df):
df['total_items_acquired'] = df["boosts"] + df["heals"] + df["weaponsAcquired"]
return df
def killPlace_over_maxPlace(df):
df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['killPlace_over_maxPlace'].fillna(0, inplace=True)
 df['killPlace_over_maxPlace'].replace(np.inf, 0, inplace=True)
 return df
def distance_over_heals(df):
df['walkDistance_over_heals'] = df['total_distance'] / df['heals']
df['walkDistance_over_heals'].fillna(0, inplace=True)
 df['walkDistance_over_heals'].replace(np.inf, 0, inplace=True)
 return df
def distance_over_kills(df):
df['walkDistance_over_kills'] = df['total_distance'] / df['kills']
df['walkDistance_over_kills'].fillna(0, inplace=True)
 df['walkDistance_over_kills'].replace(np.inf, 0, inplace=True)
 return df
def headshot_kill_rate(df):
df['headshot_kill_rate'] =(df['headshotKills']+1)/(df['kills']+1)
 return df 
<statistical_test>
data_train["ticket_type"] = np.where(data_train["ticket_type"]=='SOTONOQ', 'A5', data_train["ticket_type"])
 data_test["ticket_type"] = np.where(data_test["ticket_type"]=='SOTONOQ', 'A5', data_test["ticket_type"])
def featureEngineeringFirst(df):
print(" Feature Engineering First started...")
 df = items(df)
 gc.collect()
df = survival(df)
 gc.collect()
df = players_in_team(df)
 gc.collect()
df = total_distance(df)
 gc.collect()
df = total_time_by_distance(df)
 gc.collect()
gc.collect()
df = teamwork(df)
 gc.collect()
df = total_items_acquired(df)
 gc.collect()
gc.collect()
gc.collect()
gc.collect()
gc.collect()
df = reduce_mem_usage(df)
 print(" Feature Engineering First fished ")
 return df<groupby>
data_train = data_train.drop(['Ticket'], axis=1)
 data_test = data_test.drop(['Ticket'], axis=1 )
def min_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId','groupId'])[features].min()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def max_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].max()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def sum_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].sum()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def median_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].median()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def mean_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank<merge>
print('Missing values in Train set:', data_train.Cabin.isnull().sum())
 print('Missing values in Test set:', data_test.Cabin.isnull().sum() )
def mergeWithAgg(df,agg,agg_rank,name):
print(" Merge "+name)
 df = df.merge(agg, suffixes=["", "_"+name], how='left', on=['matchId', 'groupId'])
 df = df.merge(agg_rank, suffixes=["", "_"+name+"_rank"], how='left', on=['matchId', 'groupId'])
 return reduce_mem_usage(df )<statistical_test>
def cabin_sep(data_cabin):
cabin_type = []
for i in range(len(data_cabin)) :
if data_cabin.isnull() [i] == True:
cabin_type.append('NaN')
 else:
cabin = data_cabin[i]
cabin_type.append(cabin[:1])
 return cabin_type
def featureEngineeringSecond(df):
print(" Feature Engineering Second started...")
 print(" Min")
 min_, min_rank = min_by_team(df)
 gc.collect()
print(" Max")
 max_, max_rank = max_by_team(df)
 gc.collect()
print(" Sum")
 sum_, sum_rank = sum_by_team(df)
 gc.collect()
print(" Median")
 median_, median_rank = median_by_team(df)
 gc.collect()
print(" Mean")
 mean_, mean_rank = mean_by_team(df)
 gc.collect()
df = mergeWithAgg(df,min_, min_rank,"min")
 del min_, min_rank
df = mergeWithAgg(df,max_, max_rank,"max")
 del max_, max_rank
df = mergeWithAgg(df,sum_, sum_rank,"sum")
 del sum_, sum_rank
df = mergeWithAgg(df,median_, median_rank,"median")
 del median_, median_rank
df = mergeWithAgg(df,mean_, mean_rank,"mean")
 del mean_, mean_rank
print(" Feature Engineering Second finished")
 return df<choose_model_class>
data_train['cabin_type'] = cabin_sep(data_train.Cabin)
 data_test['cabin_type'] = cabin_sep(data_test.Cabin)
 data_train.head()
def baseline_model(input_dim):
model = Sequential()
model.add(Dense(32, kernel_initializer='he_normal',input_dim=input_dim , activation='selu'))
model.add(Dense(64, kernel_initializer='he_normal', activation='selu'))
model.add(Dense(128, kernel_initializer='he_normal', activation='selu'))
model.add(Dropout(0.1))
model.add(Dense(128, kernel_initializer='he_normal', activation='selu'))
model.add(BatchNormalization())
 model.add(Dense(64, kernel_initializer='he_normal', activation='selu'))
model.add(BatchNormalization())
 model.add(Dense(32, kernel_initializer='he_normal', activation='selu'))
model.add(Dense(8, kernel_initializer='he_normal', activation='selu'))
model.add(BatchNormalization())
 model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
 return model<categorify>
data_train = data_train.drop(['Cabin'], axis=1)
 data_test = data_test.drop(['Cabin'], axis=1 )
def normalize(df):
result = df.copy()
for feature_name in df.columns:
if df[feature_name].dtype != object:
max_value = df[feature_name].max()
min_value = df[feature_name].min()
result[feature_name] =(df[feature_name] - min_value)/(max_value - min_value)
 return result<normalization>
def name_sep(data):
families=[]
titles = []
new_name = []
for i in range(len(data)) :
name = data.iloc[i]
if '(' in name:
name_no_bracket = name.split('(')[0]
else:
name_no_bracket = name
family = name_no_bracket.split(",")[0]
title = name_no_bracket.split(",")[1].strip().split(" ")[0]
for c in string.punctuation:
name = name.replace(c,"" ).strip()
family = family.replace(c,"" ).strip()
title = title.replace(c,"" ).strip()
families.append(family)
 titles.append(title)
 new_name.append(name)
 return families, titles, new_name
def learningPart(df,batch_size):
print(" Data processing started...")
 df_winPlacePerc = df.winPlacePerc
df = df.drop('winPlacePerc', axis=1)
 df = df.drop('matchType', axis=1)
 df = featureEngineering(df)
 df = df.drop('matchId', axis=1)
 df = df.drop('groupId', axis=1)
 gc.collect()
df['winPlacePerc'] = df_winPlacePerc.values
del df_winPlacePerc
gc.collect()
print(" Data scaling started...")
 df = normalize(df)
 print(" Data scaling finised")
 train_df = df[df["winPlacePerc"] != -1]
train_df = train_df.dropna()
gc.collect()
test_df = df[df["winPlacePerc"] == -1]
test_df = test_df.drop('winPlacePerc', axis=1)
 del df
gc.collect()
train_df_Y = train_df.winPlacePerc
train_df_X = train_df.drop('winPlacePerc', axis=1)
 del train_df
print(" Data processing finished")
 print(" Data spliting started...")
 X_train_df, X_test_df, y_train_df, y_test_df = train_test_split(train_df_X, train_df_Y, test_size=0.2)
 del train_df_Y
del train_df_X
print(" Data spliting finished")
 print(" Model training started...")
 epochs = 80
callbacks = [ModelCheckpoint('best_model_df.h5', verbose=0, monitor='val_loss',save_best_only=True, mode='auto')]
model_df = baseline_model(X_train_df.shape[1])
 history_df = model_df.fit(X_train_df, y_train_df, batch_size=batch_size, epochs=epochs, verbose=0, validation_data=(X_test_df, y_test_df),callbacks=callbacks)
 gc.collect()
print(" Model training finished")
 print(" Prediction started...")
 best_model_df = load_model('best_model_df.h5')
 predict_y_df = best_model_df.predict(t_test_df)
 predict_y_df = pd.DataFrame({'Id':test_df['Id'].values,'winPlacePerc':predict_y_df.flatten() })
 print(" Prediction finished")
 return predict_y_df, history_df<prepare_output>
data_train['family'], data_train['title'], data_train['Name'] = name_sep(data_train.Name)
 data_test['family'], data_test['title'], data_test['Name'] = name_sep(data_test.Name)
 data_train.head()
def minmax(df):
df_minmax = pd.DataFrame()
for feature_name in df.columns:
v_min = df[feature_name].min()
v_max = df[feature_name].max()
df_minmax[feature_name] = pd.Series([v_min,v_max])
 return df_minmax<prepare_x_and_y>
len([x for x in data_train.family.unique() if x in data_test.family.unique() ] )
def learningPart(df,batch_size):
print(" Data processing started...")
 df = df.drop('matchType', axis=1)
 train_df = df[df["winPlacePerc"] != -1]
gc.collect()
test_df = df[df["winPlacePerc"] == -1]
test_df = test_df.drop('winPlacePerc', axis=1)
 del df
gc.collect()
train_df_Y = train_df.winPlacePerc
train_df_X = train_df.drop('winPlacePerc', axis=1)
 del train_df
train_df_X = featureEngineering(train_df_X)
 train_df_X = train_df_X.drop('matchId', axis=1)
 train_df_X = train_df_X.drop('groupId', axis=1)
 print(" Data processing finished")
 print(" Data spliting started...")
 X_train_df, X_test_df, y_train_df, y_test_df = train_test_split(train_df_X, train_df_Y, test_size=0.2)
 del train_df_Y
del train_df_X
gc.collect()
print(" Data spliting finished")
 print(" Data test processing started...")
 test_df = featureEngineering(test_df)
 test_df = test_df.drop('matchId', axis=1)
 test_df = test_df.drop('groupId', axis=1)
 print(" Data test processing finished")
 print(" Data scaling started...")
 minmaxv = pd.concat([minmax(test_df),minmax(X_train_df),minmax(X_test_df)])
 scaler_df = preprocessing.MinMaxScaler(feature_range=(-1, 1),copy=False ).fit(minmaxv)
 del minmaxv
gc.collect()
X_train_df = scaler_df.transform(X_train_df)
 gc.collect()
X_test_df = scaler_df.transform(X_test_df)
 gc.collect()
t_test_df = scaler_df.transform(test_df)
 print(" Data scaling finised")
 print(" Model training started...")
 epochs = 80
callbacks = [ModelCheckpoint('best_model_df.h5', verbose=0, monitor='val_loss',save_best_only=True, mode='auto')]
model_df = baseline_model(X_train_df.shape[1])
 history_df = model_df.fit(X_train_df, y_train_df, batch_size=batch_size, epochs=epochs, verbose=0, validation_data=(X_test_df, y_test_df),callbacks=callbacks)
 gc.collect()
print(" Model training finished")
 print(" Prediction started...")
 best_model_df = load_model('best_model_df.h5')
 predict_y_df = best_model_df.predict(t_test_df)
 predict_y_df = pd.DataFrame({'Id':test_df['Id'].values,'winPlacePerc':predict_y_df.flatten() })
 print(" Prediction finished")
 return predict_y_df, history_df<load_from_csv>
len([x for x in data_train.family.unique() if x not in data_test.family.unique() ] )
result = {}
for name,typeGame in dp_by_type.items() :
print("Start "+ name)
 predict, history = learningPart(pd.read_csv(name+".csv"),typeGame[1])
 result[name] = [predict,history]
print("End "+ name)
 dp_by_type[name] = 0<define_variables>
len([x for x in data_test.family.unique() if x not in data_train.family.unique() ] )
data = []
for key,ele in result.items() :
data.append(ele[0])
<concatenate>
data_train['family_size'] = data_train.SibSp + data_train.Parch +1
data_test['family_size'] = data_test.SibSp + data_test.Parch +1
rate_family = data_train.groupby('family')['Survived', 'family','family_size'].median()
rate_family.head()
df = pd.concat(data, ignore_index=True)
 df = df.sort_values(by='Id', ascending=[True])
 df = df.reset_index(drop=True )<load_from_csv>
overlap_family ={}
for i in range(len(rate_family)) :
if rate_family.index[i] in overlap and rate_family.iloc[i,1] > 1:
overlap_family[rate_family.index[i]] = rate_family.iloc[i,0]
submission = pd.read_csv(".. /input/sample_submission_V2.csv" )<feature_engineering>
mean_survival_rate = np.mean(data_train.Survived)
 family_survival_rate = []
family_survival_rate_NA = []
for i in range(len(data_train)) :
if data_train.family[i] in overlap_family:
family_survival_rate.append(overlap_family[data_train.family[i]])
 family_survival_rate_NA.append(1)
 else:
family_survival_rate.append(mean_survival_rate)
 family_survival_rate_NA.append(0)
 data_train['family_survival_rate']= family_survival_rate
data_train['family_survival_rate_NA']= family_survival_rate_NA
submission["winPlacePerc"] = df.winPlacePerc<save_to_csv>
mean_survival_rate = np.mean(data_train.Survived)
 family_survival_rate = []
family_survival_rate_NA = []
for i in range(len(data_test)) :
if data_test.family[i] in overlap_family:
family_survival_rate.append(overlap_family[data_test.family[i]])
 family_survival_rate_NA.append(1)
 else:
family_survival_rate.append(mean_survival_rate)
 family_survival_rate_NA.append(0)
 data_test['family_survival_rate']= family_survival_rate
data_test['family_survival_rate_NA']= family_survival_rate_NA
submission.to_csv('submission.csv', index=False )<save_to_csv>
data_train = data_train.drop(['Name', 'family'], axis=1)
 data_test = data_test.drop(['Name', 'family'], axis=1)
 data_train.head()
submission.to_csv('submission.csv', index=False )<load_from_csv>
IQR = data_train.Fare.quantile(0.75)- data_train.Fare.quantile(0.25)
 upper_bound = data_train.Fare.quantile(0.75)+ 3*IQR
data_train.loc[data_train.Fare >upper_bound, 'Fare'] = upper_bound
data_test.loc[data_test.Fare >upper_bound, 'Fare'] = upper_bound
max(data_train.Fare )
df_train= pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 df_test= pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv' )<feature_engineering>
IQR = data_train.Age_mean.quantile(0.75)- data_train.Age_mean.quantile(0.25)
 upper_bound = data_train.Age_mean.quantile(0.75)+ 3*IQR
data_train.loc[data_train.Age_mean >upper_bound, 'Age_mean'] = upper_bound
data_test.loc[data_test.Age_mean >upper_bound, 'Age_mean'] = upper_bound
max(data_train.Age_mean )
df_train['belongs']= 'train'
df_test['belongs']= 'test'<concatenate>
upper_bound = data_train.Age.mean() + 3* data_train.Age.std()
data_train.loc[data_train.Age >upper_bound, 'Age'] = upper_bound
data_test.loc[data_test.Age >upper_bound, 'Age'] = upper_bound
max(data_train.Age )
df= pd.concat([df_train.drop('winPlacePerc', axis= 1), df_test], axis= 0, ignore_index= True )<feature_engineering>
print(data_train["family_size"].value_counts() /len(data_train))
df['killRate']= df['kills']/ df['matchDuration']<feature_engineering>
print('Pclass')
 print(data_train["Pclass"].value_counts() /len(data_train))
print(data_test["Pclass"].value_counts() /len(data_train))
print("------------------------------")
 print('Sex')
 print(data_train["Sex"].value_counts() /len(data_train))
print(data_test["Sex"].value_counts() /len(data_train))
print("------------------------------")
 print('Embarked')
 print(data_train["Embarked"].value_counts() /len(data_train))
print(data_test["Embarked"].value_counts() /len(data_train))
print("------------------------------" )
df['DBNORate']= df['DBNOs']/ df['matchDuration']<feature_engineering>
data = pd.concat([data_train.drop(['Survived'], axis=1), data_test], axis =0, sort = False)
 data.head()
df['entryCount']= 1<groupby>
le = LabelEncoder()
columns = ['Sex', 'Embarked', 'ticket_type', 'cabin_type', 'title']
for col in columns:
le.fit(data[col])
 data[col] = le.transform(data[col])
 data.head()
df['total_players_match']= df.groupby(['matchId'])['entryCount'].transform(np.sum )<groupby>
data = data.drop(['Age_mean', 'Age_NA'], axis =1 )
df['total_players_group']= df.groupby(['groupId'])['entryCount'].transform(np.sum )<feature_engineering>
sum(data.Age.isnull() )
df['killPlacePerc']=(df['killPlace']/ df['total_players_match'] )<feature_engineering>
x_train_age = data.dropna().drop(['Age'], axis =1)
 y_train_age = data.dropna() ['Age']
df.loc[df['killPoints']== 0, 'killPoints']= 1<groupby>
x_test_age = data[pd.isnull(data.Age)].drop(['Age'], axis =1 )
df['maxKillPointsMatch']= df.groupby(['matchId'])['killPoints'].transform(np.max )<groupby>
model_lin = make_pipeline(StandardScaler() ,KernelRidge())
 kfold = model_selection.KFold(n_splits=10, random_state=4, shuffle = True)
 parameters = {'kernelridge__gamma' : [0.001, 0.01, 0.1, 1, 10, 100, 1000],
'kernelridge__kernel': ['rbf', 'linear'],
'kernelridge__alpha' :[0.001, 0.01, 0.1, 1, 10, 100, 1000],
}
search_lin = GridSearchCV(model_lin, parameters, n_jobs = -1, cv = kfold, scoring = 'r2',verbose=1)
 search_lin.fit(x_train_age, y_train_age )
df['maxKillPointsGroup']= df.groupby(['groupId'])['killPoints'].transform(np.max )<feature_engineering>
print("Best parameters are:", search_lin.best_params_)
 print("Best accuracy achieved:",search_lin.cv_results_['mean_test_score'].mean() )
df['ratioMatchKillPoints']= df['killPoints']/ df['maxKillPointsMatch']<feature_engineering>
y_test_age = search_lin.predict(x_test_age )
df['ratioGroupKillPoints']= df['killPoints']/ df['maxKillPointsGroup']<data_type_conversions>
data.loc[data['Age'].isnull() , 'Age'] = y_test_age
df['killPointsBuckets']= df['killPointsBuckets'].astype(np.int8 )<feature_engineering>
idx = int(data_train.shape[0])
 data_train['Age'] = data.iloc[:idx].Age
data_test['Age'] = data.iloc[idx:].Age
df.loc[df['winPoints']== 0, 'winPoints']= 1<groupby>
le = LabelEncoder()
data_train_LE = data_train.copy()
data_test_LE = data_test.copy()
columns = ['Sex', 'Embarked', 'ticket_type', 'cabin_type', 'title']
for col in columns:
le.fit(data_train_LE[col])
 data_train_LE[col] = le.fit_transform(data_train_LE[col])
 data_test_LE[col] = le.transform(data_test_LE[col])
 data_train_LE.head()
df['maxWinPointsMatch']= df.groupby(['matchId'])['winPoints'].transform(np.max )<groupby>
drop_col = ['Age_mean', 'SibSp', 'Parch']
data_train_LE = data_train_LE.drop(drop_col, axis=1)
 data_test_LE = data_test_LE.drop(drop_col, axis=1 )
df['maxWinPointsGroup']= df.groupby(['groupId'])['winPoints'].transform(np.max )<feature_engineering>
X_train_onehot = data_train.drop(drop_col, axis=1)
 X_test_onehot = data_test.drop(drop_col, axis=1 )
df['ratioMatchWinPoints']= df['winPoints']/ df['maxKillPointsMatch']<feature_engineering>
columns = ['cabin_type', 'title', 'Sex', 'Embarked', 'ticket_type', 'Pclass']
for col in columns:
X_train_onehot = pd.concat([X_train_onehot, pd.get_dummies(X_train_onehot[col], drop_first = True)], axis =1)
 X_test_onehot = pd.concat([X_test_onehot, pd.get_dummies(X_test_onehot[col], drop_first = True)], axis =1)
df['ratioGroupWinPoints']= df['winPoints']/ df['maxKillPointsGroup']<data_type_conversions>
X_train_onehot = X_train_onehot.drop(columns, axis=1)
 X_test_onehot = X_test_onehot.drop(columns, axis=1 )
df['winPointsBuckets']= df['winPointsBuckets'].astype(np.int8 )<groupby>
X_train_lab = data_train.drop(drop_col, axis=1)
 X_test_lab = data_test.drop(drop_col, axis=1 )
df['killPointsSumMatch']= df.groupby(['matchId'])['killPoints'].transform(np.sum )<groupby>
le = LabelEncoder()
columns = ['Sex', 'Embarked', 'ticket_type', 'cabin_type', 'title']
for col in columns:
le.fit(data_train[col])
 X_train_lab[col] = le.transform(X_train_lab[col])
 X_test_lab[col] = le.transform(X_test_lab[col])
 X_test_lab.head()
df['killPointsSumGroup']= df.groupby(['groupId'])['killPoints'].transform(np.sum )<feature_engineering>
X_train_mean = data_train.drop(drop_col, axis=1)
 X_test_mean = data_test.drop(drop_col, axis=1 )
df['ratioKillPointsGroupAndMatch']= df['killPointsSumGroup']/ df['killPointsSumMatch']<groupby>
columns = ['cabin_type', 'title', 'Sex', 'Embarked', 'ticket_type']
for col in columns:
ordered_labels = X_train_mean.groupby([col])['Survived'].mean().to_dict()
X_train_mean[col] = X_train_mean[col].map(ordered_labels)
 X_test_mean[col] = X_test_mean[col].map(ordered_labels )
df['avgKillPointsGroup']= df.groupby(['matchId'])['killPoints'].transform(np.mean )<groupby>
X_train_freq = data_train.drop(drop_col, axis=1)
 X_test_freq = data_test.drop(drop_col, axis=1 )
df['avgKillPointsMatch']= df.groupby(['groupId'])['killPoints'].transform(np.mean )<feature_engineering>
columns = ['cabin_type', 'title', 'Sex', 'Embarked', 'ticket_type']
for col in columns:
ordered_labels = X_train_freq[col].value_counts().to_dict()
X_train_freq[col] = X_train_freq[col].map(ordered_labels)
 X_test_freq[col] = X_test_freq[col].map(ordered_labels )
df['ratioAvgKillPointsGroupAndMatch']= df['avgKillPointsGroup']/ df['avgKillPointsMatch']<groupby>
random_state = 4
df['groupRevived']= df.groupby(['groupId'])['revives'].transform(np.sum )<groupby>
kfold = StratifiedKFold(n_splits=5 )
df['groupTeamKills']= df.groupby(['groupId'])['teamKills'].transform(np.sum )<feature_engineering>
def separate(X_train):
X = X_train.drop(columns= ['Survived'])
 Y = X_train['Survived']
return X, Y
df['avgSpeed']=(df['walkDistance']+ df['swimDistance']+ df['rideDistance'])/ df['matchDuration']<categorify>
X_onehot, Y_onehot = separate(X_train_onehot)
 X_lab, Y_lab = separate(X_train_lab)
 X_mean, Y_mean = separate(X_train_mean)
 X_freq, Y_freq = separate(X_train_freq )
le= LabelEncoder()
le.fit(df['matchType'] )<categorify>
random_state = 4
classifiers = []
classifiers.append(( 'SVC', make_pipeline(StandardScaler() ,SVC(random_state=random_state))))
classifiers.append(( 'DecisionTree', DecisionTreeClassifier(random_state=random_state)))
 classifiers.append(( 'AdaBoost', AdaBoostClassifier(DecisionTreeClassifier(random_state=random_state),\
random_state=random_state,learning_rate=0.1)))
 classifiers.append(( 'RandomForest', RandomForestClassifier(random_state=random_state)))
 classifiers.append(( 'GradientBoost', GradientBoostingClassifier(random_state=random_state)))
 classifiers.append(( 'MPL', make_pipeline(StandardScaler() , MLPClassifier(random_state=random_state))))
classifiers.append(( 'KNN',make_pipeline(MinMaxScaler() ,KNeighborsClassifier(n_neighbors=7))))
results = []
names = []
for name, classifier in classifiers:
kfold = model_selection.KFold(n_splits= 3, random_state=random_state, shuffle = True)
 cv_results = model_selection.cross_val_score(classifier, X_onehot, y = Y_onehot, cv=kfold, scoring='accuracy')
 results.append(cv_results)
 names.append(name)
 msg = "%s: %f(%f)" %(name, cv_results.mean() , cv_results.std())
 print(msg)
df['matchTypeLabels']= le.fit_transform(df['matchType'] )<drop_column>
def random_forest(X, Y, X_test):
parameters = {'max_depth' : [2, 4, 5, 10],
'n_estimators' : [200, 500, 1000, 2000],
'min_samples_split' : [3, 4, 5],
}
kfold = model_selection.KFold(n_splits=3, random_state=random_state, shuffle = True)
 model_RFC = RandomForestClassifier(random_state = 4, n_jobs = -1)
 search_RFC = GridSearchCV(model_RFC, parameters, n_jobs = -1, cv = kfold, scoring = 'accuracy',verbose=1)
 search_RFC.fit(X, Y)
 predicted= search_RFC.predict(X_test)
 print("Best parameters are:", search_RFC.best_params_)
 print("Best accuracy achieved:",search_RFC.best_score_)
 return search_RFC.best_params_, model_RFC, search_RFC, predicted
df.drop(['entryCount', 'groupId', 'matchId', 'matchType', 'DBNOs', 'winPoints', 'killPoints', 'killStreaks', 'maxKillPointsGroup', 'revives', 'headshotKills', 'teamKills', 'roadKills', 'vehicleDestroys'], axis= 1, inplace= True )<rename_columns>
param_RFC_onehot, model_RFC_onehot, search_RFC_onehot, predicted_cv_RFC_onehot = random_forest(X_onehot, Y_onehot, X_test_onehot)
 param_RFC_lab, model_RFC_lab, search_RFC_lab, predicted_cv_RFC_lab = random_forest(X_lab, Y_lab, X_test_lab)
 param_RFC_mean, model_RFC_mean, search_RFC_mean, predicted_cv_RFC_mean = random_forest(X_mean, Y_mean, X_test_mean)
 param_RFC_freq, model_RFC_freq, search_RFC_freq, predicted_cv_RFC_freq = random_forest(X_freq, Y_freq, X_test_freq )
df.set_index('Id', inplace= True )<filter>
def fit_pred_RF(X, Y, X_test):
model_RFC = RandomForestClassifier(max_depth =2, min_samples_split =3, n_estimators = 5000,
random_state = 4, n_jobs = -1)
 model_RFC.fit(X, Y)
 predicted= model_RFC.predict(X_test)
 return predicted, model_RFC
df_train= df[df['belongs']== 'train']<filter>
predicted_RFC_onehot, model_RFC_onehot = fit_pred_RF(X_onehot, Y_onehot, X_test_onehot)
 predicted_RFC_lab, model_RFC_lab = fit_pred_RF(X_lab, Y_lab, X_test_lab)
 predicted_RFC_mean, model_RFC_mean = fit_pred_RF(X_mean, Y_mean, X_test_mean)
 predicted_RFC_freq, model_RFC_freq = fit_pred_RF(X_freq, Y_freq, X_test_freq )
df_test= df[df['belongs']== 'test']<drop_column>
def grad_boost(X, Y, X_test):
parameters = {'max_depth' : [2, 4, 10, 15],
'n_estimators' : [10, 50, 100],
'min_samples_split' : [5, 10, 15],
}
kfold = model_selection.KFold(n_splits=3, random_state=random_state, shuffle = True)
 model_GBC = GradientBoostingClassifier(random_state = 4)
 search_GBC = GridSearchCV(model_GBC, parameters, n_jobs = -1, cv = kfold, scoring = 'accuracy',verbose=1)
 search_GBC.fit(X, Y)
 predicted= search_GBC.predict(X_test)
 print("Best parameters are:", search_GBC.best_params_)
 print("Best accuracy achieved:",search_GBC.cv_results_['mean_test_score'].mean())
 return search_GBC.best_params_, model_GBC, search_GBC, predicted
df_train.drop('belongs', axis= 1, inplace= True )<drop_column>
param_GBC_onehot, model_GBC_onehot, search_GBC_onehot, predicted_cv_GBC_onehot = grad_boost(X_onehot, Y_onehot, X_test_onehot)
 param_GBC_lab, model_GBC_lab, search_GBC_lab, predicted_cv_GBC_lab = grad_boost(X_lab, Y_lab, X_test_lab)
 param_GBC_mean, model_GBC_mean, search_GBC_mean, predicted_cv_GBC_mean = grad_boost(X_mean, Y_mean, X_test_mean)
 param_GBC_freq, model_GBC_freq, search_GBC_freq, predicted_cv_GBC_freq = grad_boost(X_freq, Y_freq, X_test_freq )
df_test.drop('belongs', axis= 1, inplace= True )<load_from_csv>
def fit_pred_GBC(X, Y, X_test):
model_GBC = GradientBoostingClassifier(max_depth = 2, min_samples_split = 15, n_estimators = 10,\
random_state = 4, max_features= 'auto')
 model_GBC.fit(X, Y)
 predicted= model_GBC.predict(X_test)
 return predicted, model_GBC
df_train['winPlacePerc']= pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv', usecols= ['winPlacePerc'] )<rename_columns>
predicted_GBC_onehot, model_GBC_onehot = fit_pred_GBC(X_onehot, Y_onehot, X_test_onehot)
 predicted_GBC_lab, model_GBC_lab = fit_pred_GBC(X_lab, Y_lab, X_test_lab)
 predicted_GBC_mean, model_GBC_mean = fit_pred_GBC(X_mean, Y_mean, X_test_mean)
 predicted_GBC_freq, model_GBC_freq = fit_pred_GBC(X_freq, Y_freq, X_test_freq )
df_train.set_index('Id', inplace= True )<correct_missing_values>
def mod_KNN(X, Y, X_test):
model_KNN=make_pipeline(MinMaxScaler() ,KNeighborsClassifier())
 kfold = model_selection.KFold(n_splits=3, random_state=random_state, shuffle = True)
 parameters=[{'kneighborsclassifier__n_neighbors': [2,3,4,5,6,7,8,9,10]}]
search_KNN = GridSearchCV(estimator=model_KNN, param_grid=parameters, scoring='accuracy', cv=kfold)
 scores_KNN=cross_val_score(search_KNN, X, Y,scoring='accuracy', cv=kfold, verbose=1)
 search_KNN.fit(X, Y)
 predicted= search_KNN.predict(X_test)
 print("Best parameters are:", search_KNN.best_params_)
 print("Best accuracy achieved:",search_KNN.cv_results_['mean_test_score'].mean())
 return search_KNN.best_params_, model_KNN, search_KNN, predicted
df_train.dropna(inplace= True )<split>
param_KNN_onehot, model_KNN_onehot, search_KNN_onehot, predicted_cv_KNN_onehot = mod_KNN(X_onehot, Y_onehot, X_test_onehot)
 param_KNN_lab, model_KNN_lab, search_KNN_lab, predicted_cv_KNN_lab = mod_KNN(X_lab, Y_lab, X_test_lab)
 param_KNN_mean, model_KNN_mean, search_KNN_mean, predicted_cv_KNN_mean = mod_KNN(X_mean, Y_mean, X_test_mean)
 param_KNN_freq, model_KNN_freq, search_KNN_freq, predicted_cv_KNN_freq = mod_KNN(X_freq, Y_freq, X_test_freq )
X_train, X_test, y_train, y_test= train_test_split(df_train.drop(['winPlacePerc'], axis= 1), df_train['winPlacePerc'], test_size= 0.3 )<choose_model_class>
def fit_pred_KNN(X, Y, X_test):
model_KNN = make_pipeline(MinMaxScaler() ,KNeighborsClassifier(n_neighbors=11))
model_KNN.fit(X, Y)
 predicted= model_KNN.predict(X_test)
 return predicted, model_KNN
model= XGBRegressor(n_estimators= 500, max_depth= 7, n_jobs= -1, min_child_weight= 7, subsample=0.84, colsample_bytree= 0.97, eta=0.3, seed=42 )<train_model>
predicted_KNN_onehot, model_KNN_onehot = fit_pred_KNN(X_onehot, Y_onehot, X_test_onehot)
 predicted_KNN_lab, model_KNN_lab = fit_pred_KNN(X_lab, Y_lab, X_test_lab)
 predicted_KNN_mean, model_KNN_mean = fit_pred_KNN(X_mean, Y_mean, X_test_mean)
 predicted_KNN_freq, model_KNN_freq = fit_pred_KNN(X_freq, Y_freq, X_test_freq )
model.fit(
 X_train,
y_train,
eval_metric="rmse",
eval_set=[(X_train, y_train),(X_test, y_test)],
verbose=True,
early_stopping_rounds = 10 )<save_to_csv>
def mod_SVC(X, Y, X_test):
model_SVC=make_pipeline(StandardScaler() ,SVC(random_state=1))
parameters=[{'svc__C': [0.0001,0.001,0.1,1, 10, 100],
'svc__gamma':[0.0001,0.001,0.1,1,10,50,100],
'svc__kernel':['rbf'],
'svc__degree' : [1,2,3,4]
}]
kfold = model_selection.KFold(n_splits=3, random_state=random_state, shuffle = True)
 search_SVC = GridSearchCV(estimator=model_SVC, param_grid = parameters, scoring='accuracy', cv=kfold)
 scores_SVC=cross_val_score(search_SVC, X, Y,scoring='accuracy', cv=kfold, verbose =1)
 search_SVC.fit(X, Y)
 predicted= search_SVC.predict(X_test)
 print("Best parameters are:", search_SVC.best_params_)
 print("Best accuracy achieved:",search_SVC.cv_results_['mean_test_score'].mean())
 return search_SVC.best_params_, model_SVC, search_SVC, predicted
predictions= pd.DataFrame({'winPlacePerc': model.predict(df_test ).clip(0,1)}, index= df_test.index)
 predictions.to_csv('submission_1.csv' )<categorify>
param_SVC_onehot, model_SVC_onehot, search_SVC_onehot, predicted_cv_SVC_onehot = mod_SVC(X_onehot, Y_onehot, X_test_onehot)
 param_SVC_lab, model_SVC_lab, search_SVC_lab, predicted_cv_SVC_lab = mod_SVC(X_lab, Y_lab, X_test_lab)
 param_SVC_mean, model_SVC_mean, search_SVC_mean, predicted_cv_SVC_mean = mod_SVC(X_mean, Y_mean, X_test_mean)
 param_SVC_freq, model_SVC_freq, search_SVC_freq, predicted_cv_SVC_freq = mod_SVC(X_freq, Y_freq, X_test_freq )
def memory_reduce(df):
start_memory = df.memory_usage().sum() /1024**2
for col in df.columns:
col_type = df[col].dtype
if(col_type != object):
min_val = min(df[col])
 max_val = max(df[col])
 if(str(col_type)[:3] == 'int'):
if(min_val > np.iinfo(np.int8 ).min and max_val < np.iinfo(np.int8 ).max):
df[col] = df[col].astype(np.int8)
 elif(min_val > np.iinfo(np.int16 ).min and max_val < np.iinfo(np.int16 ).max):
df[col] = df[col].astype(np.int16)
 elif(min_val > np.iinfo(np.int32 ).min and max_val < np.iinfo(np.int32 ).max):
df[col] = df[col].astype(np.int32)
 elif(min_val > np.iinfo(np.int64 ).min and max_val < np.iinfo(np.int64 ).max):
df[col] = df[col].astype(np.int64)
 else:
if(min_val > np.finfo(np.float16 ).min and max_val < np.finfo(np.float16 ).max):
df[col] = df[col].astype(np.float16)
 elif(min_val > np.finfo(np.float32 ).min and max_val < np.finfo(np.float32 ).max):
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 end_memory = df.memory_usage().sum() /1024**2
return df<load_from_csv>
def fit_pred_SVC(X, Y, X_test):
model_SVC = make_pipeline(StandardScaler() ,SVC(random_state=random_state, C= 1, gamma = 0.001, kernel = 'rbf', degree =1))
model_SVC.fit(X, Y)
 predicted= model_SVC.predict(X_test)
 return predicted, model_SVC
<load_from_csv>
predicted_SVC_onehot, model_SVC_onehot = fit_pred_SVC(X_onehot, Y_onehot, X_test_onehot)
 predicted_SVC_lab, model_SVC_lab = fit_pred_SVC(X_lab, Y_lab, X_test_lab)
 predicted_SVC_mean, model_SVC_mean = fit_pred_SVC(X_mean, Y_mean, X_test_mean)
 predicted_SVC_freq, model_SVC_freq = fit_pred_SVC(X_freq, Y_freq, X_test_freq )
startTime = time.time()
def feature_engineering(is_train = True , debug = True):
test_Idx = None
if(is_train):
print('processing train data')
 if(debug):
df = memory_reduce(pd.read_csv('.. /input/train_V2.csv' , nrows=10000))
else:
df = memory_reduce(pd.read_csv('.. /input/train_V2.csv'))
df = df[pd.notnull(df['winPlacePerc'])]
else:
print('processing test data')
 if(debug):
df = memory_reduce(pd.read_csv('.. /input/test_V2.csv' , nrows = 10000))
else:
df = memory_reduce(pd.read_csv('.. /input/test_V2.csv'))
test_Idx = df.Id
print('remove many feature')
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 features.remove('matchType')
 y = None
if(is_train):
print('get target')
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print('get group mean featuers')
 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 features')
 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 features')
 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 group matchId mean')
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg , suffixes = ['','_match_mean'], how = 'left' , on = 'matchId')
 print('get match size feature')
 agg = df.groupby(['matchId'] ).size().reset_index(name = 'matchSize')
 df_out = df_out.merge(agg , on = 'matchId' , how = 'left')
 df_out.drop(['matchId' , 'groupId'] , axis = 1 ,inplace = True)
 X = df_out
columnsName = list(df_out.columns)
 del df , df_out , agg , agg_rank
gc.collect()
return X , y , columnsName , test_Idx
x_train , y_train , train_columns , _ = feature_engineering(True , False)
 x_test , _ , _ , test_Idx = feature_engineering(False , False)
 print('This time is {:.2f}'.format(time.time() - startTime))<feature_engineering>
predicted = np.where(((predicted_SVC_mean + predicted_KNN_onehot+predicted_RFC_onehot+predicted_RFC_freq+ predicted_RFC_mean)/5)> 0.5, 1, 0)
<categorify><EOS> 
test =pd.read_csv(".. /input/test.csv")
 submission = pd.DataFrame({'PassengerId': test['PassengerId'],'Survived':predicted})
 submission.head()
filename = 'Titanic Predictions Public.csv'
submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )