Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<load_from_csv>
%matplotlib inline
sb.set()
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
train = pd.read_csv('.. /input/train_V2.csv')
 test = pd.read_csv('.. /input/test_V2.csv')
 train.dropna(inplace=True )
X_train = train.drop(labels = ["label"],axis = 1)
 Y_train = train["label"]
len(Y_train )<feature_engineering>
train['matchType'].value_counts()
X_train = X_train / 255.0
test = test / 255.0<train_model>
train['teamPlayers']=train.groupId.map(train.groupId.value_counts())
 test['teamPlayers']=test.groupId.map(test.groupId.value_counts())
 train['gamePlayers']=train.matchId.map(train.matchId.value_counts())
 test['gamePlayers']=test.matchId.map(test.matchId.value_counts() )
img_width = 28
img_height = 28
n_channels = 1
X_train = X_train.values.reshape(-1,img_height,img_width,n_channels)
 test = test.values.reshape(-1,img_height,img_width,n_channels )<categorify>
train['enemyPlayers']=train['gamePlayers']-train['teamPlayers']
test['enemyPlayers']=test['gamePlayers']-test['teamPlayers']
Y_train = to_categorical(Y_train, num_classes = 10 )<split>
train['totalDistance']=train['rideDistance']+train['swimDistance']+train['walkDistance']
test['totalDistance']=test['rideDistance']+test['swimDistance']+test['walkDistance']
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )<train_model>
train['enemyDamage']=train['assists']+train['kills']
test['enemyDamage']=test['assists']+test['kills']
print("Total Images:",len(Y_train)+len(Y_val))
print("Training Images:",len(Y_train))
print("Validation Images:",len(Y_val))<choose_model_class>
totalKills = train.groupby(['matchId','groupId'] ).agg({'kills': lambda x: x.sum() })
 totalKills.rename(columns={"kills": "squadKills"}, inplace=True)
 train = train.join(other=totalKills, on=['matchId', 'groupId'])
 totalKills = test.groupby(['matchId','groupId'] ).agg({'kills': lambda x: x.sum() })
 totalKills.rename(columns={"kills": "squadKills"}, inplace=True)
 test = test.join(other=totalKills, on=['matchId', 'groupId'] )
model = Sequential()
model.add(Convolution2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape = input_shape))
model.add(Convolution2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Convolution2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Convolution2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
train['medicKits']=train['heals']+train['boosts']
test['medicKits']=test['heals']+test['boosts']
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<train_model>
train['medicPerKill'] = train['medicKits']/train['enemyDamage']
test['medicPerKill'] = test['medicKits']/test['enemyDamage']
datagen = ImageDataGenerator(
 featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False,
samplewise_std_normalization=False, zca_whitening=False, rotation_range=10,
zoom_range = 0.1, width_shift_range=0.1, height_shift_range=0.1,
horizontal_flip=False, vertical_flip=False)
 datagen.fit(X_train )<train_model>
train['distancePerHeals'] = train['totalDistance']/train['heals']
test['distancePerHeals'] = test['totalDistance']/test['heals']
Model = model.fit_generator(datagen.flow(X_train, Y_train,batch_size=200),epochs=30,verbose=1,validation_data=(X_val, Y_val))<save_model>
train['headShotKillRatio']=train['headshotKills']/train['kills']
test['headShotKillRatio']=test['headshotKills']/test['kills']
model.save("cnn_digit_recognizer.h5" )<compute_test_metric>
train['headshotKillRate'] = train['headshotKills'] / train['kills']
test['headshotKillRate'] = test['headshotKills'] / test['kills']
score = model.evaluate(X_train, Y_train, verbose=1)
 print('Train Loss:', score[0])
 print('Train Accuracy:', score[1] )<compute_test_metric>
train['killPlaceOverMaxPlace'] = train['killPlace'] / train['maxPlace']
test['killPlaceOverMaxPlace'] = test['killPlace'] / test['maxPlace']
score = model.X_valuate(X_val, Y_val, verbose=1)
 print('Validation Loss:', score[0])
 print('Validation Accuracy:', score[1] )<compute_train_metric>
train['kills/distance']=train['kills']/train['totalDistance']
test['kills/distance']=test['kills']/test['totalDistance']
Y_pred = model.predict(X_val)
 Y_pred_classes = np.argmax(Y_pred,axis = 1)
 Y_true = np.argmax(Y_val,axis = 1)
 confusion_Matrix = confusion_matrix(Y_true, Y_pred_classes)
 print(confusion_Matrix )<predict_on_test>
train['kills/walkDistance']=train['kills']/train['walkDistance']
test['kills/walkDistance']=test['kills']/test['walkDistance']
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
train['avgKills'] = train['squadKills']/train['teamPlayers']
test['avgKills'] = test['squadKills']/test['teamPlayers']
final_Result = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 final_Result.to_csv("cnn_mnist_datagen.csv",index=False )<load_from_csv>
train['damageRatio'] = train['damageDealt']/train['enemyDamage']
test['damageRatio'] = test['damageDealt']/test['enemyDamage']
random.seed(123)
 sns.set_style("darkgrid")
 train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv")
 len_train = len(train)
 train_y = train["Survived"]<train_model>
train['distTravelledPerGame'] = train['totalDistance']/train['matchDuration']
test['distTravelledPerGame'] = test['totalDistance']/test['matchDuration']
lm = ols('Age~ C(Pclass)+ C(Sex)* C(Embarked)+ C(SibSp)+ C(Parch)', pd.concat([train,test],axis=0)).fit()
anova_lm(lm,typ=2 )<train_on_grid>
train['killPlacePerc'] = train['killPlace']/train['gamePlayers']
test['killPlacePerc'] = test['killPlace']/test['gamePlayers']
lm = ols('Fare~ C(Pclass)+ C(Sex)+ C(Embarked)+ C(SibSp)+ C(Parch)', pd.concat([train,test],axis=0)).fit()
anova_lm(lm,typ=2 )<train_model>
train["playerSkill"] = train["headshotKills"]+ train["roadKills"]+train["assists"]-(5*train['teamKills'])
 test["playerSkill"] = test["headshotKills"]+ test["roadKills"]+test["assists"]-(5*test['teamKills'] )
mapping = {'Mlle': 'Miss', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Mr', 'Don': 'Mr', 'Mme': 'Miss',
'Jonkheer': 'Mr', 'Lady': 'Mrs', 'Capt': 'Mr', 'the Countess': 'Mrs', 'Ms': 'Miss', 'Dona': 'Mrs'}
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, attribute_names):
self.attribute_names = attribute_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attribute_names]
class FirstTransformer(BaseEstimator, TransformerMixin):
def __init__(self, name):
self.name = name
def fit(self, X, y=None):
return self
def transform(self, X):
value = [i[0][0] for i in X[self.name]]
X[self.name] = value
return X
class ValueImputer(BaseEstimator, TransformerMixin):
def __init__(self, name, value):
self.value = value
self.name = name
def fit(self, X, y=None):
return self
def transform(self, X):
X[self.name].fillna(self.value,inplace=True)
 return X
class GroupbyImputer(BaseEstimator, TransformerMixin):
def __init__(self, name, group_by):
self.name = name
self.group_by = group_by
def fit(self, X, y=None):
self.value = X.groupby(self.group_by)[self.name]
return self
def transform(self, X):
X[self.name] = self.value.apply(lambda x: x.fillna(x.median()))
i=1
while(X[self.name].isnull().sum() >0):
k = X.groupby(self.group_by[:-i])[self.name]
X[self.name] =(k.apply(lambda x: x.fillna(x.median())))
 i+=1
if i == len(self.group_by):
break
return X
class MostTransformer(BaseEstimator, TransformerMixin):
def __init__(self, names,mapping):
self.names = names
self.mapping = mapping
def fit(self, X, y=None):
self.freq = X[self.names].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
return self
def transform(self, X, y=None):
X[self.names + "_map"] = self.freq
X.replace({self.names+"_map": self.mapping}, inplace=True)
 return X.drop("Name",axis=1)
 class FrequentTransformer(BaseEstimator, TransformerMixin):
def __init__(self, names):
self.names = names
def fit(self, X, y=None):
self.freq = X.groupby(self.names)[self.names].transform('count')
 return self
def transform(self, X, y=None):
X[self.names + "_freq"] = self.freq
return X.drop(self.names, axis=1)
 imputer_pipeline = Pipeline([ 
('Cabin_imputer', ValueImputer(name = "Cabin",value="None")) , 
('Embarked_imputer', ValueImputer(name= "Embarked",value="S")) , 
('Fare_imputer', GroupbyImputer(name= "Fare",group_by = ['Pclass', 'Parch','SibSp'])) , 
('Age_imputer', GroupbyImputer(name= "Age",group_by = ['Pclass','Parch','Embarked'])) ,
])
 transform_pipeline = Pipeline([ 
('Cabin', FirstTransformer(name= "Cabin")) , 
('Name_transformer', MostTransformer(names = "Name", mapping = mapping)) , 
('Ticket_transformer', FrequentTransformer(names= "Ticket")) ,
] )<categorify>
train['gamePlacePerc'] = train['killPlace']/train['maxPlace']
test['gamePlacePerc'] = test['killPlace']/test['maxPlace']
class TaitanicProcessing(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
self.y = y
return self
def transform(self, X):
X["Sex"] = X["Sex"].map({"male":1, "female":0})
 X["Family"] = X["SibSp"] + X["Parch"] + 1
X["Family"] = X["Family"].astype(str)
 X['Family'] = X['Family'].replace("1", 'Alone')
 X['Family'] = X['Family'].replace(["2","3","4"], 'Normal')
 X['Family'] = X['Family'].replace(["5","6"], 'Mid')
 X['Family'] = X['Family'].replace(["7","8","11"], 'Big')
 X["Fare"] = np.log1p(X["Fare"])
 X["Fare"] = MinMaxScaler().fit_transform(X['Fare'].values.reshape(-1, 1))
X['Cabin'] = X['Cabin'].replace(['A', 'B', 'C','T'], 'ABCT')
 X['Cabin'] = X['Cabin'].replace(['D', 'E'], 'DE')
 X['Cabin'] = X['Cabin'].replace(['F', 'G'], 'FG')
 X['Age'] = pd.qcut(X['Age'], 10)
 X['Age'] = LabelEncoder().fit_transform(X['Age'])
 return X
class DummyCategory(BaseEstimator, TransformerMixin):
def __init__(self, names):
self.names = names
def fit(self, X, y=None):
return self
def transform(self, X):
encoded_fea = []
for c in self.names:
encoded = OneHotEncoder().fit_transform(X[c].values.reshape(-1, 1)).toarray()
n = X[c].nunique()
cols = ['{}_{}'.format(c, n)for n in range(1, n + 1)]
encoded_df = pd.DataFrame(encoded, columns=cols)
 encoded_df.index = X.index
encoded_fea.append(encoded_df)
 return pd.concat([X, *encoded_fea[:5]], axis=1)
 process_pipeline = Pipeline([ 
("process", TaitanicProcessing()), 
("cat_pipeline", DummyCategory(["Embarked", "Name_map","Cabin","Family"])) ,
])
 full_pipeline = Pipeline([ 
("imputer", imputer_pipeline), 
("transform", transform_pipeline), 
("process", process_pipeline),
] )<concatenate>
train.fillna(0,inplace=True)
 train.replace(np.inf, 0, inplace=True)
 test.fillna(0,inplace=True)
 test.replace(np.inf, 0, inplace=True )
df = pd.concat([train.drop("Survived",axis=1),test],axis=0,sort=False)
 df.set_index("PassengerId",drop=True, inplace=True)
 df = full_pipeline.fit_transform(df,train_y)
 df.reset_index(drop=True,inplace=True)
 df.info()<drop_column>
train.count()
df.drop(["Embarked", "Name_map","Cabin","Family",'SibSp','Parch'],axis=1,inplace=True)
 df.head()<split>
train.drop(columns=['killPoints','rankPoints','winPoints','maxPlace'],inplace=True)
 test.drop(columns=['killPoints','rankPoints','winPoints','maxPlace'],inplace=True )
train = df.iloc[:len_train,:].reset_index(drop=True)
 test = df.iloc[len_train:,:].reset_index(drop=True )<split>
def feature(df):
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 condition='False'
if 'winPlacePerc' in df.columns:
y = np.array(df.groupby(['matchId','groupId'])['winPlacePerc'].agg('mean'), dtype=np.float64)
 features.remove("winPlacePerc")
 condition='True'
print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = agg.reset_index() [['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 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'])
 df_id=df_out[["matchId", "groupId"]].copy()
df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 del df, agg, agg_rank
gc.collect()
if condition=='True':
return df_out,pd.DataFrame(y),df_id
else:
return df_out,df_id
def rf_cv(n_estimators, max_depth, min_samples_split, min_samples_leaf, ccp_alpha, x_data=None, y_data=None, n_splits=5, output='score'):
score = 0
kf = StratifiedKFold(n_splits=n_splits, random_state=5, shuffle=True)
 models = []
for train_index, valid_index in kf.split(x_data, y_data):
x_train, y_train = x_data.iloc[train_index], y_data[train_index]
x_valid, y_valid = x_data.iloc[valid_index], y_data[valid_index]
model = RandomForestClassifier(
 criterion='gini',
max_features='auto',
n_estimators = int(n_estimators),
max_depth = int(max_depth),
min_samples_split = int(min_samples_split),
min_samples_leaf = int(min_samples_leaf),
ccp_alpha = ccp_alpha,
random_state = 123,
oob_score=True,
n_jobs=-1 
)
 model.fit(x_train, y_train)
 models.append(model)
 pred = model.predict_proba(x_valid)[:, 1]
true = y_valid
score += roc_auc_score(true, pred)/n_splits
if output == 'score':
return score
if output == 'model':
return models
func_fixed = partial(rf_cv, x_data=train, y_data=train_y, n_splits=5, output='score')
 rf_ba = BayesianOptimization(
 func_fixed,
{
'n_estimators':(1000, 2000),
'max_depth' :(5,13),
'min_samples_split' :(4,8),
'min_samples_leaf' :(4,8),
'ccp_alpha' :(0.0001, 0.01)
 },
random_state=4321 
)
 rf_ba.maximize(init_points=5, n_iter=20 )<predict_on_test>
x,y,id_train=feature(reduce_mem_usage(train))
params = rf_ba.max['params']
rf_model = rf_cv(
 params['n_estimators'],
params['max_depth'],
params['min_samples_split'],
params['min_samples_leaf'],
params['ccp_alpha'],
x_data=train, y_data=train_y, n_splits=5, output='model')
 importances = pd.DataFrame(np.zeros(( train.shape[1], 5)) , columns=['Fold_{}'.format(i)for i in range(1, 6)], index=train.columns)
 preds = []
for i, model in enumerate(rf_model):
importances.iloc[:, i] = model.feature_importances_
pred = model.predict(test)
 preds.append(pred)
 pred = np.mean(preds, axis=0 )<save_to_csv>
x_test,id_test=feature(reduce_mem_usage(test))
y_pred = pred
y_pred[y_pred >= 0.5] = 1
y_pred = y_pred.astype(int)
 submission = pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 submission["Survived"] = y_pred
submission.to_csv("submission.csv",index = False )<train_model>
del train,test
gc.collect()
rfc_model = RandomForestClassifier(criterion='gini',n_estimators=1800,max_depth=7, min_samples_split=6,min_samples_leaf=6,
max_features='auto', oob_score=True, random_state=123,n_jobs=-1)
 oob = 0
probs = pd.DataFrame(np.zeros(( len(test),10)) , columns=['Fold_{}_Sur_{}'.format(i, j)for i in range(1, 6)for j in range(2)])
 kf = StratifiedKFold(n_splits=5, random_state=5, shuffle=True)
 for fold,(trn_idx, val_idx)in enumerate(kf.split(train, train_y), 1):
rfc_model.fit(train.iloc[trn_idx,:], train_y[trn_idx])
 probs.loc[:, ['Fold_{}_Sur_0'.format(fold),'Fold_{}_Sur_1'.format(fold)]] = rfc_model.predict_proba(test)
 oob += rfc_model.oob_score_ / 5
print('Fold {} OOB : {}'.format(fold, rfc_model.oob_score_))
print('Average Score: {}'.format(oob))<prepare_x_and_y>
x['matchId']=id_train['matchId']
x['groupId']=id_train['groupId']
x_train,x_val,y_train,y_val=train_test_split(reduce_mem_usage(x),y,test_size=.1)
 x_test=reduce_mem_usage(x_test)
 id_val=x_val[['matchId','groupId']]
x_val.drop(['matchId','groupId'],axis=1,inplace=True)
 x_train.drop(['matchId','groupId'],axis=1,inplace=True)
 x.drop(['matchId','groupId'],axis=1,inplace=True)
 del y
gc.collect()
survived =[col for col in probs.columns if col.endswith('Sur_1')]
probs['survived'] = probs[survived].mean(axis=1)
 probs['unsurvived'] = probs.drop(columns=survived ).mean(axis=1)
 probs['pred'] = 0
sub = probs[probs['survived'] >= 0.5].index
probs.loc[sub, 'pred'] = 1
y_pred = probs['pred'].astype(int )<save_to_csv>
params = {
"objective" : "regression",
"metric" : "mae",
"num_leaves" : 149,
"learning_rate" : 0.03,
"bagging_fraction" : 0.9,
"bagging_seed" : 0,
"num_threads" : 4,
"colsample_bytree" : 0.5,
'min_data_in_leaf':1900,
'min_split_gain':0.00011,
'lambda_l2':9
}
submission = pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 submission["Survived"] = y_pred
submission.to_csv("submission.csv",index = False )<import_modules>
lgb_train = lgb.Dataset(x_train, y_train,
free_raw_data=False)
 lgb_eval = lgb.Dataset(x_val, y_val, reference=lgb_train,
free_raw_data=False )
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RepeatedKFold
import numpy as np
import re<load_from_csv>
model = lgb.train(params,
lgb_train,
num_boost_round=22000,
valid_sets=lgb_eval,
early_stopping_rounds=10,
verbose_eval=1000 )
df = pd.DataFrame(pd.read_csv('.. /input/titanic/train.csv'))
df_test = pd.DataFrame(pd.read_csv('.. /input/titanic/test.csv'))<count_missing_values>
y_pred_val = model.predict(x, num_iteration=model.best_iteration)
 id_train['win_pred']=y_pred_val
id_train.set_index(['matchId','groupId'])
 train = reduce_mem_usage(pd.read_csv(".. /input/train_V2.csv"))
df=pd.merge(train,id_train,on=['matchId','groupId'],how='right')
 df
df.isnull().sum()<groupby>
print('The mae score is {}'.format(mean_absolute_error(df['winPlacePerc'],df['win_pred'])))
 df = df[["Id", "matchId", "groupId", "maxPlace", "numGroups",'winPlacePerc', 'win_pred']]
print(df.groupby(['Pclass'] ).mean() ['Age'])
 print('
')
 print(df.groupby(['Sex'] ).mean() ['Age'] )<feature_engineering>
df_grouped = df.groupby(["matchId", "groupId"] ).first().reset_index()
df_grouped["team_place"] = df_grouped.groupby(["matchId"])["win_pred"].rank()
df_grouped
def age_nan(df):
for i in df.Sex.unique() :
for j in df.Pclass.unique() :
x = df.loc[(( df.Sex == i)&(df.Pclass == j)) , 'Age'].mean()
df.loc[(( df.Sex == i)&(df.Pclass == j)) , 'Age'] = df.loc[(( df.Sex == i)&(df.Pclass == j)) , 'Age'].fillna(x)
 age_nan(df)
 age_nan(df_test )<data_type_conversions>
df_grouped["win_perc"] =(df_grouped["team_place"] - 1)/(df_grouped["numGroups"]-1)
 df = df.merge(df_grouped[["win_perc","matchId", "groupId"]], on=["matchId", "groupId"], how="left" )
df['Embarked'] = df['Embarked'].fillna('S')
 df_test['Embarked'] = df_test['Embarked'].fillna('S' )<data_type_conversions>
df.loc[df['maxPlace'] == 0, "win_perc"] = 0
df.loc[df['maxPlace'] == 1, "win_perc"] = 1
df.loc[(df['maxPlace'] > 1)&(df['numGroups'] == 1), "win_perc"] = 0
df.loc[df['win_perc'] < 0,"win_perc"] = 0
df.loc[df['win_perc'] > 1,"win_perc"] = 1
df['win_perc'].fillna(df['win_pred'],inplace=True )
df['Cabin_NaN'] = df['Cabin'].isnull().astype(int)
 df_test['Cabin_NaN'] = df_test['Cabin'].isnull().astype(int)
 countplot('Cabin_NaN' )<count_missing_values>
df_grouped[df_grouped['maxPlace']>1][['winPlacePerc','win_perc','maxPlace','numGroups','team_place']]
df_test.isnull().sum()<correct_missing_values>
subset = df.loc[df['maxPlace'] > 1]
gap = 1 /(subset['maxPlace'].values-1)
 new_perc = np.around(subset['win_perc'].values / gap)* gap
df.loc[df.maxPlace > 1, "win_perc"] = new_perc
df_test.Fare = df_test.Fare.fillna(-1 )<find_best_model_class>
print('The new mae score is {}'.format(mean_absolute_error(df['winPlacePerc'],df['win_perc'])) )
def reg_cross_val(variables):
X = df[variables]
y = df['Survived']
rkfold = RepeatedKFold(n_splits = 2, n_repeats = 10, random_state = 10)
 result = []
for treino, teste in rkfold.split(X):
X_train, X_test = X.iloc[treino], X.iloc[teste]
y_train, y_test = y.iloc[treino], y.iloc[teste]
reg = LogisticRegression(max_iter = 500)
 reg.fit(X_train, y_train)
 result.append(reg.score(X_test, y_test))
return np.mean(result )<categorify>
del x,train,df
gc.collect()
def is_female(x):
if x == 'female':
return 1
else:
return 0
df['Sex_bin'] = df['Sex'].map(is_female)
 df_test['Sex_bin'] = df_test['Sex'].map(is_female )<categorify>
y_pred = model.predict(x_test, num_iteration=model.best_iteration)
 id_test['win_pred']=y_pred
id_test.set_index(['matchId','groupId'])
 del x_train,x_val,y_train,y_val,x_test
gc.collect()
test = reduce_mem_usage(pd.read_csv(".. /input/test_V2.csv"))
df=pd.merge(test,id_test,on=['matchId','groupId'],how='right')
 del id_test,test
gc.collect()
df
def embarked_s(x):
if x == 'S':
return 1
else:
return 0
df['Embarked_S'] = df['Embarked'].map(embarked_s)
 df_test['Embarked_S'] = df_test['Embarked'].map(embarked_s)
 def embarked_c(x):
if x == 'C':
return 1
else:
return 0
df['Embarked_C'] = df['Embarked'].map(embarked_c)
 df_test['Embarked_C'] = df_test['Embarked'].map(embarked_c )<feature_engineering>
df = df[["Id", "matchId", "groupId", "maxPlace", "numGroups",'win_pred']]
df['Family'] = df.SibSp + df.Parch
df_test['Family'] = df_test.SibSp + df_test.Parch<count_values>
df_grouped = df.groupby(["matchId", "groupId"] ).first().reset_index()
df_grouped["team_place"] = df_grouped.groupby(["matchId"])["win_pred"].rank()
df_grouped
text_ticket = ''
for i in df.Ticket:
text_ticket += i
lista = re.findall('[a-zA-Z]+', text_ticket)
 print('Most repeated terms in Tickets:
')
 print(pd.Series(lista ).value_counts().head(10))<data_type_conversions>
df_grouped["win_perc"] =(df_grouped["team_place"] - 1)/(df_grouped["numGroups"]-1)
 df = df.merge(df_grouped[["win_perc", "matchId", "groupId"]], on=["matchId", "groupId"], how="left" )
df['CA'] = df['Ticket'].str.contains('CA|C.A.' ).astype(int)
 df['SOTON'] = df['Ticket'].str.contains('SOTON|STON' ).astype(int)
 df['PC'] = df['Ticket'].str.contains('PC' ).astype(int)
 df['SC'] = df['Ticket'].str.contains('SC|S.C' ).astype(int)
 df['C'] = df['Ticket'].str.contains('C' ).astype(int)
 df_test['CA'] = df_test['Ticket'].str.contains('CA|C.A.' ).astype(int)
 df_test['SOTON'] = df_test['Ticket'].str.contains('SOTON|STON' ).astype(int)
 df_test['PC'] = df_test['Ticket'].str.contains('PC' ).astype(int)
 df_test['SC'] = df_test['Ticket'].str.contains('SC|S.C' ).astype(int)
 df_test['C'] = df_test['Ticket'].str.contains('C' ).astype(int )<count_values>
df.loc[df.maxPlace == 0, "win_perc"] = 0
df.loc[df.maxPlace == 1, "win_perc"] = 1
df.loc[(df.maxPlace > 1)&(df.numGroups == 1), "win_perc"] = 0
df.loc[df['win_perc'] < 0,"win_perc"] = 0
df.loc[df['win_perc'] > 1,"win_perc"] = 1
df['win_perc'].fillna(df['win_pred'],inplace=True )
text_name = ''
for i in df.Name:
text_name += i
lista = re.findall('[a-zA-Z]+', text_name)
 print('Most repeated words in Name column:
')
 print(pd.Series(lista ).value_counts().head(10))<data_type_conversions>
subset = df.loc[df['maxPlace'] > 1]
gap = 1 /(subset['maxPlace'].values-1)
 new_perc = np.around(subset['win_perc'].values / gap)* gap
df.loc[df.maxPlace > 1, "win_perc"] = new_perc
df['winPlacePerc']=df['win_perc']
df['Master'] = df['Name'].str.contains('Master' ).astype(int)
 df['Mr'] = df['Name'].str.contains('Mr' ).astype(int)
 df['Miss'] = df['Name'].str.contains('Miss' ).astype(int)
 df['Mrs'] = df['Name'].str.contains('Mrs' ).astype(int)
 df_test['Master'] = df_test['Name'].str.contains('Master' ).astype(int)
 df_test['Mr'] = df_test['Name'].str.contains('Mr' ).astype(int)
 df_test['Miss'] = df_test['Name'].str.contains('Miss' ).astype(int)
 df_test['Mrs'] = df_test['Name'].str.contains('Mrs' ).astype(int )<save_to_csv>
df=df[['Id','winPlacePerc']]
df.to_csv("submission_final.csv", index=False )