Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<load_from_csv>
warnings.filterwarnings('ignore')
 matplotlib.rcParams['axes.unicode_minus'] = False
plt.style.use('ggplot')
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 train.info()<categorify>
def print_expand() :
pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', 100)
 pd.set_option('display.max_colwidth', -1)
 def print_basic() :
pd.set_option('display.max_columns', 30)
 pd.set_option('display.max_rows', 30)
 pd.set_option('display.max_colwidth', 50)
 print_expand()
def substrings_in_string(big_string, substrings):
for substring in substrings:
if big_string.find(substring)!= -1:
return substring
print(big_string)
 return np.nan

 def phase1clean(df):
df.Fare = df.Fare.map(lambda x: np.nan if x==0 else x)
 df.Cabin = df.Cabin.fillna('Unknown')
 cabin_list = ['A', 'B', 'C', 'D', 'E', 'F', 'T', 'G', 'Unknown']
df['Deck']=df['Cabin'].map(lambda x: substrings_in_string(x, cabin_list))
title_list=['Mrs', 'Mr', 'Master', 'Miss', 'Major', 'Rev',
'Dr', 'Ms', 'Mlle','Col', 'Capt', 'Mme', 'Countess',
'Don', 'Jonkheer']
df['Title']=df['Name'].map(lambda x: substrings_in_string(x, title_list))
def replace_titles(x):
title=x['Title']
if title in ['Countess','Don', 'Major', 'Capt', 'Jonkheer', 'Rev', 'Col']:
return 'Rare'
elif title in ['Countess', 'Mme']:
return 'Mrs'
elif title in ['Mlle', 'Ms']:
return 'Miss'
elif title =='Dr':
if x['Sex']=='Male':
return 'Mr'
else:
return 'Mrs'
else:
return title
df['Title']=df.apply(replace_titles, axis=1)
 df['Family_Size']=df['SibSp']+df['Parch']
return df

 def fare_grouping(x):
bins = [-1, 7.91, 14.454, 31, 99, 250, np.inf]
names = ['a', 'b','c', 'd', 'e', 'f']
names = [1, 2, 3, 4, 5, 6]
x['Fare_Bin'] = pd.cut(x['Fare'], bins ,labels=names ).astype('int')
 dict_age={1 : 'a' , 2 : 'b' , 3 : 'c' , 4 : 'd' , 5: 'e' , 6 : 'f'}
x['Fare_Bin']=x['Fare_Bin'].map(dict_age)

return x

 def fill_nan(x):

 null_ind=x.loc[x['Age'].isnull() ,:].index
null_count=x.loc[x['Age'].isnull() ,]['PassengerId'].count()
num_ages=x.groupby('Title')['Age'].mean().to_dict()
x.loc[x['Age'].isnull() ,'Age']=x.loc[x['Age'].isnull() ,'Title'].map(num_ages)

null_ind=x.loc[x['Fare'].isnull() ,:].index
null_count=x.loc[x['Fare'].isnull() ,]['PassengerId'].count()
num_fare=x.groupby('Pclass')['Fare'].mean().to_dict()
x.loc[x['Fare'].isnull() ,'Fare']=x.loc[x['Fare'].isnull() ,'Pclass'].map(num_fare)
 fare_grouping(x)

f_index=x[x['Embarked'].isnull() ].index
x=x.drop(f_index,axis=0)
 return x

 def age_grouping(x):

 k_data=pd.concat([x['Age'],x['Survived']],axis=1)
 k_data.rename(columns={ 0 :'Age' , 1 :'Survived'}, inplace=True)

model = KMeans()
sil = []
for k in range(2, 9):
kmeans = KMeans(n_clusters = k ).fit(k_data)
 labels = kmeans.labels_
sil.append(silhouette_score(k_data, labels, metric = 'euclidean'))
plt_data=pd.concat([pd.Series(range(2,9)) ,pd.Series(sil)],axis=1)
 plt_data.rename(columns={ 0 :'clusters' , 1 :'silhouette scores'}, inplace=True)

kmeans = KMeans(n_clusters=4, random_state=0 ).fit(k_data)
 labels_=kmeans.labels_
k_data['age_grp']=labels_+1
k_data['Passenger_Id']=x['PassengerId']



 agelist=[]
for i in range(0, 15):
agelist.append('a')
 for i in range(15, 29):
agelist.append('b')
 for i in range(29, 45):
agelist.append('c')
 for i in range(45, 90):
agelist.append('d')
 age_dict={v: k for v, k in enumerate(agelist)}
x['Age_Grp']=x['Age'].astype(int ).map(age_dict)
 return x,age_dict

 def survived_fams(df):
df['Last_Name'] = df['Name'].apply(
 lambda df: str.split(df, ",")[0])
 default_survival_rate = 0.5
df['Family_Survival'] = default_survival_rate
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
return df

 def is_alone(x):
fam_list={False : 0 , True : 1}
x['is_alone'] =(x['Family_Size']==0 ).map(fam_list)

return x

 def has_cabin(x):
fam_list={False : 0 , True : 1}
x['has_cabin'] =(x['Deck']!='Unknown' ).map(fam_list)

return x

 def is_3stclass(x):
class_list={False : 0 , True : 1}
x['is_3rdclass'] =(x['Pclass']== 3 ).map(class_list)

return x

 def class_categorizer(x):
dict_class={1 : 'a' , 2 : 'b' , 3 : 'c' }
x['Pclass']=x['Pclass'].map(dict_class)
 return x

 def phase2clean(train, test):
train=fill_nan(train)
 train,age_dict=age_grouping(train)
 test=fill_nan(test)
 test['Age_Grp']=test['Age'].astype(int ).map(age_dict)
 for df in [train, test]:
df['Fare_Per_Person']=df['Fare']/(df['Family_Size']+1)
 for df in [train, test]:
df['Age*Class']=df['Age']*df['Pclass']
combined=pd.concat([train,test])
 combined=survived_fams(combined)
 combined=is_alone(combined)
 combined=has_cabin(combined)
 combined=is_3stclass(combined)
 train=combined.iloc[:len(train),:]
test=combined.iloc[len(train):,:]
test=test.drop(['Survived'],axis=1)
 return [train,test]


def get_data() :
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 submit_x =pd.read_csv('/kaggle/input/titanic/test.csv')
 original_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 original_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 x=phase1clean(train_data)
 pred_set=phase1clean(submit_x)
 x,pred_set=phase2clean(x, pred_set)
 x=x.drop(['PassengerId','Name','Ticket','Cabin','Last_Name'],axis=1)
 pred_set=pred_set.drop(['PassengerId','Name','Ticket','Cabin','Last_Name'],axis=1)
 y = x.Survived
x = x.loc[:,x.columns!='Survived']
return x,y,pred_set,original_train,original_test 
<prepare_output>
train = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 test = pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv')
 sub = pd.read_csv('.. /input/pubg-finish-placement-prediction/sample_submission_V2.csv' )
x,y,pred_set,original_train,pred_set_original=get_data()
x.info()
x.head()<categorify>
train.isna().sum()
class_cat=FunctionTransformer(class_categorizer)
 quantile_transformer = FunctionTransformer(quantile_transform)

cat_features=['Title', 'Deck' ,'Pclass' , 'Sex' , 'Embarked' , 'Age_Grp' , 'Fare_Bin']
num_features = list(x.select_dtypes(include=['int64','float64'] ).columns)
 categorical_transformer = pip.Pipeline(steps=[('class_cat',class_cat), 
('enc', CatBoostEncoder())
 ])
 numerical_transformer = pip.Pipeline([('just','passthrough')])
 preprocessor = ColumnTransformer(transformers=[('cat', categorical_transformer , cat_features), 
('num' , numerical_transformer , num_features)
 ])
 pipeline_xgb =pip.Pipeline(steps=[('preprocessor', preprocessor), 
('feature_select', SelectKBest(chi2 , k = 15)) , 
('classifier',XGBClassifier(learning_rate=0.01 ,
n_estimators=860,
max_depth=3,
subsample=1,
colsample_bytree=1,
gamma=6,
reg_alpha = 14,
reg_lambda = 3))
])

cv = StratifiedKFold(5, shuffle=True, random_state=42)
 accuracies = cross_val_score(pipeline_xgb, x , y , cv = cv)
 print("5 fold cross validation accuracies {}".format(accuracies))<train_on_grid>
test.isna().sum()
params=[{
'feature_select' : [SelectKBest(chi2)],
'feature_select__k' : [i for i in range(5,19)]
}]
cv = StratifiedKFold(5, shuffle=True, random_state=42)
 search=GridSearchCV(estimator=pipeline_xgb,
param_grid=params,
n_jobs=-1,
cv=cv)
 search.fit(x, y)
 print("best score : {} , best params : {} ".format(search.best_score_ , search.best_params_)) 
<categorify>
train[train['winPlacePerc'].isna() ==True]
cat_features=['Title', 'Deck' ,'Pclass' , 'Sex' , 'Embarked' , 'Age_Grp' , 'Fare_Bin']
num_features = list(x.select_dtypes(include=['int64','float64'] ).columns)
 categorical_transformer = pip.Pipeline(steps=[('class_cat',class_cat), 
('enc', CatBoostEncoder())
 ])
 numerical_transformer = pip.Pipeline([('normal_trans',quantile_transformer)])
 preprocessor = ColumnTransformer(transformers=[('cat', categorical_transformer , cat_features), 
('num' , numerical_transformer , num_features)])
 pipeline_log =pip.Pipeline(steps=[('preprocessor', preprocessor), 
('feature_select',SelectKBest(chi2, k = 17)) , 
('classifier',LogisticRegression(penalty = 'l2',
solver = 'liblinear',
C = 0.25))
])

cv = StratifiedKFold(5, shuffle=True, random_state=42)
 accuracies = cross_val_score(pipeline_log, x , y , cv = cv)
 print("5 fold cross validation accuracies {}".format(accuracies)) 
<compute_train_metric>
train = train.dropna()
train.shape
classifier = VotingClassifier(estimators=[('XGB', pipeline_xgb),('LOG', pipeline_log)])

cv = StratifiedKFold(5, shuffle=True, random_state=42)
 accuracies = cross_val_score(classifier, x , y , cv = cv)
 print("5 fold cross validation accuracies {}".format(accuracies))<save_to_csv>
d = train['groupId'].value_counts()
d = d[d > 4]
print('length :', len(d), 'max :', np.max(d), 'min :', np.min(d))
d[:5]
classifier.fit(x,y)
 y_submit=pd.Series(( classifier.predict(pred_set)))
 y_submit=y_submit.astype(int)
 y_1=pred_set_original.PassengerId
y_submit_f=pd.concat([y_1,y_submit],axis=1)
 y_submit_f.rename(columns={ 0 :'Survived'}, inplace=True)
 y_submit_f.to_csv('submission.csv',index=False )<import_modules>
train[train['groupId'] == '23b79fb17aeaad']
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import toimage
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator<load_from_csv>
cols = train.columns[3:]
cols = cols.delete(12)
 for col in cols:
quan(train[col])
df_train = pd.read_csv(".. /input/train.csv", encoding = 'ISO-8859-1')
 df_subm = pd.read_csv(".. /input/test.csv", encoding = 'ISO-8859-1' )<count_missing_values>
7 + 33 + 35 + 28 + 17 + 6 + 2 + 2 + 1
df_train.isnull().sum().sum() , df_subm.isnull().sum().sum()<prepare_x_and_y>
train[train['numGroups'] == 100]['matchId'].nunique()
X_train = df_train[df_train.columns[1:]]
y_train = df_train['label']<split>
train[train['maxPlace'] == 100]['matchId'].nunique()
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train )<categorify>
del tt
gc.collect()
y_train = to_categorical(y_train)
 y_test = to_categorical(y_test )<feature_engineering>
train['killStreaks'].value_counts()
X_train = X_train/255
X_test = X_test/255<choose_model_class>
test['killStreaks'].value_counts()
model = Sequential()<choose_model_class>
train['matchType'].value_counts()
model.add(Conv2D(32,(3, 3), activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(32,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))<choose_model_class>
train['matchType'].value_counts()
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))<choose_model_class>
np.sum(test['maxPlace'] == 2 )
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True )<choose_model_class>
model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy'] )<choose_model_class>
headshot = train[['kills', 'headshotKills', 'winPlacePerc']]
headshot['headshotrate'] = 0
headshot['headshotrate'][headshot['kills'] > 0] = headshot['kills'] / headshot['headshotKills']
headshot.corr()
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
)<train_model>
datagen.fit(X_train )<train_model>
model.fit_generator(datagen.flow(X_train, y_train, batch_size=128),
steps_per_epoch=int(len(X_train)/ 128), epochs=30 )<prepare_x_and_y>
test_data = df_subm.values<train_model>
test_data = test_data.reshape(test_data.shape[0],28,28,1 )<feature_engineering>
Cross Validation Is Complete')