Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<prepare_output>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
from sklearn.preprocessing import MinMaxScaler,StandardScaler
from sklearn.model_selection import train_test_split
import seaborn as sns
from catboost import Pool, CatBoostRegressor
submission = pd.DataFrame({'PassengerId':result[:, 0],'Survived':result[:, 1]})
 submission.head()<save_to_csv>
data=pd.read_csv('.. /input/train_V2.csv' )
filename = 'Titanic_Survival_Trial_9.csv'
submission.to_csv(filename, index=False)
 print('Saved file: ' + filename )<set_options>
gamer_counts=data.groupby(['matchId'] ).size().reset_index(name='counts' )
%matplotlib inline 
<load_from_csv>
train_data=pd.merge(data,gamer_counts, on='matchId' )
data_train = pd.read_csv('.. /input/train.csv')
 data_test = pd.read_csv('.. /input/test.csv' )<feature_engineering>
mtype_val=['squad-fpp','duo-fpp','squad','solo-fpp','duo','solo','normal-squad-fpp','crashfpp','normal-duo-fpp','flaretpp','normal-solo-fpp','flarefpp','normal-squad','crashtpp','normal-solo','normal-duo']
mtype_s=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
mtype_typ=dict(zip(mtype_val,mtype_s))
train_data=train_data.replace({'matchType':mtype_typ} )
def simplify_ages(df):
unknow = df['Age'].isnull()
master_title = df['Title'] == 'Master'
mr_title = df['Title'] == 'Mr'
mrs_title = df['Title'] == 'Mrs'
miss_title = df['Title'] == 'Miss'
dr_title = df['Title'] == 'Dr'
df.loc[master_title & unknow,'Age'] = df[master_title]['Age'].mean()
df.loc[mr_title & unknow,'Age'] = df[mr_title]['Age'].mean()
df.loc[mrs_title & unknow,'Age'] = df[mrs_title]['Age'].mean()
df.loc[miss_title & unknow,'Age'] = df[miss_title]['Age'].mean()
df.loc[dr_title & unknow,'Age'] = df[dr_title]['Age'].mean()
df.Age = df.Age.fillna(-0.5)
 return df
def simplify_cabins(df):
df.Cabin = df.Cabin.fillna('N')
 df.Cabin = df.Cabin.apply(lambda x: x[0])
 return df
def simplify_fares(df):
df.Fare = df.Fare.fillna(-0.5)
 return df
def format_name(df):
df['Lname'] = df.Name.apply(lambda x: x.split(',')[0])
 df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 return df
def simplify_family(df):
df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
return df
def simplify_embarks(df):
df.Embarked = df.Embarked.fillna('N')
 df.Embarked = df.Embarked.apply(lambda x: x[0])
 return df
def simplify_titles(df):
df['Title'] = df['Title'].replace(['Col','Dr','Major'], 'MidNoble')
 df['Title'] = df['Title'].replace(['Mlle','Ms','Mme','Countess','Sir', 'Lady'], 'Noble')
 df['Title'] = df['Title'].replace(['Don','Jonkheer'], 'Mr')
 df['Title'] = df['Title'].replace(['Capt','Rev'], 'Worker')
 return df
def create_sex_class(df):
male = df['Sex']=='male'
female = df['Sex']=='female'
Class1 = df['Pclass']==1
Class2 = df['Pclass']==2
Class3 = df['Pclass']==3
df.loc[female & Class1,'Class'] =(0)
 df.loc[female & Class2,'Class'] =(1)
 df.loc[female & Class3,'Class'] =(2)
 df.loc[male & Class1,'Class'] =(3)
 df.loc[male & Class2,'Class'] =(4)
 df.loc[male & Class3,'Class'] =(5)
 return df
def drop_features(df):
return df.drop(['Ticket', 'SibSp', 'Parch', 'Name', 'Lname', 'Pclass'], axis=1)
 def transform_features(df):
df = format_name(df)
 df = simplify_ages(df)
 df = simplify_cabins(df)
 df = simplify_fares(df)
 df = simplify_family(df)
 df = simplify_embarks(df)
 df = simplify_titles(df)
 df = create_sex_class(df)
 df = drop_features(df)
 return df
def encode_features(df_train, df_test):
features = ['Cabin', 'Title', 'Embarked', 'Sex']
df_combined = pd.concat([df_train[features], df_test[features]])
 for feature in features:
le = preprocessing.LabelEncoder()
le = le.fit(df_combined[feature])
 df_train[feature] = le.transform(df_train[feature])
 df_test[feature] = le.transform(df_test[feature])
 return df_train, df_test<categorify>
train_data['winPlacePerc']=train_data['winPlacePerc'].fillna(train_data['winPlacePerc'].mean() )
data_train = transform_features(data_train)
 data_test = transform_features(data_test )<categorify>
train_sub=train_data['winPlacePerc']
train_data=train_data.drop(['winPlacePerc','Id','groupId','matchId'],axis=1 )
data_train, data_test = encode_features(data_train, data_test )<prepare_x_and_y>
scaler = MinMaxScaler()
scaler.fit(train_data)
 train=scaler.transform(train_data )
X_all = data_train.drop(['Survived', 'PassengerId'], axis=1)
 y_all = data_train['Survived']<compute_test_metric>
train_X, test_X, train_y, test_y = train_test_split(train, train_sub, test_size=0.5, random_state=42 )
def MultivariateGaussian(x,y):
k = 2
d =(x.shape)[1]
mu = np.zeros(( k,d))
sigma = np.zeros(( k,d,d))
pi = np.zeros(k)
 for label in range(2):
indices =(y == label)
 mu[label] = np.mean(x[indices,:], axis=0)
 sigma[label] = np.cov(x[indices,:], rowvar=0, bias=1)
 pi[label] = float(sum(indices)) /float(len(y))
return mu, sigma, pi<compute_test_metric>
train_pool = Pool(train_X, train_y)
 test_pool = Pool(test_X, test_y.values )
mu, sigma, pi = MultivariateGaussian(X_all.values,y_all.values )<predict_on_test>
model = CatBoostRegressor(
 iterations=5000,
depth=10,
learning_rate=0.1,
l2_leaf_reg= 2,
loss_function='RMSE',
eval_metric='MAE',
random_strength=0.1,
bootstrap_type='Bernoulli',
leaf_estimation_method='Gradient',
leaf_estimation_iterations=1,
boosting_type='Plain'
,task_type = "GPU"
,feature_border_type='GreedyLogSum'
,random_seed=1234
)
def test_model(mu, sigma, pi, features, tx, ty):
preds = []
errors = 0
for x,y in zip(tx,ty):
piP_list = []
for label in range(2):
S = np.linalg.inv(sigma[label])
 dS = np.linalg.det(sigma[label])
 xTsigmax=0
for i in features:
for j in features:
xTsigmax += S[i][j]*(x[i]-mu[label][i])*(x[j]-mu[label][j])
 piP_list.append(pi[label]*np.exp(-0.5*xTsigmax)/np.sqrt(dS))
predict = np.argmax(piP_list)
 preds.append(predict)
 if predict != y:
errors+=1
return errors, preds<train_model>
model.fit(train_pool, eval_set=test_pool, plot=True )
errors, preds = test_model(mu, sigma, pi, [0,1,2,3,4,5,6,7], X_all.values, y_all.values)
 print('Accuracy for training data:',(1-errors/len(y_all)) *100, '%' )<predict_on_test>
test_data=pd.read_csv('.. /input/test_V2.csv' )
def write_predictions(mu, sigma, pi, features, tx):
preds = []
for x in tx:
piP_list = []
for label in range(2):
S = np.linalg.inv(sigma[label])
 dS = np.linalg.det(sigma[label])
 xTsigmax=0
for i in features:
for j in features:
xTsigmax += S[i][j]*(x[i]-mu[label][i])*(x[j]-mu[label][j])
 piP_list.append(pi[label]*np.exp(-0.5*xTsigmax)/np.sqrt(dS))
predict = np.argmax(piP_list)
 preds.append(predict)
 return preds<predict_on_test>
gamer_counts=test_data.groupby(['matchId'] ).size().reset_index(name='counts' )
preds = write_predictions(mu, sigma, pi, [0,1,2,3,4,5,6,7], data_test.drop('PassengerId',axis=1 ).values )<create_dataframe>
test=pd.merge(test_data,gamer_counts, on='matchId' )
data_pred = pd.DataFrame()
data_pred['PassengerId']=data_test['PassengerId']
data_pred['Survived']=preds<save_to_csv>
test=test.drop(['Id','groupId','matchId'],axis=1 )
data_pred.to_csv('predictions_generative_multGauss.csv', index = False )<set_options>
test=test.replace({'matchType':mtype_typ} )
%matplotlib inline
GradientBoostingClassifier, ExtraTreesClassifier)
 warnings.filterwarnings('ignore')
 df_train=pd.read_csv('.. /input/train.csv',sep=',')
 df_test=pd.read_csv('.. /input/test.csv',sep=',')
 df_data = df_train.append(df_test)
 PassengerId = df_test['PassengerId']
Submission=pd.DataFrame()
Submission['PassengerId'] = df_test['PassengerId']
print('Components imported' )<feature_engineering>
test=scaler.transform(test )
df_data["Title"] = df_data.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df_data["Title"] = df_data["Title"].replace('Mlle', 'Miss')
 df_data["Title"] = df_data["Title"].replace('Master', 'Master')
 df_data["Title"] = df_data["Title"].replace(['Mme', 'Dona', 'Ms'], 'Mrs')
 df_data["Title"] = df_data["Title"].replace(['Jonkheer','Don'],'Mr')
 df_data["Title"] = df_data["Title"].replace(['Capt','Major', 'Col','Rev','Dr'], 'Millitary')
 df_data["Title"] = df_data["Title"].replace(['Lady', 'Countess','Sir'], 'Honor')
 df_train["Title"] = df_data['Title'][:891]
df_test["Title"] = df_data['Title'][891:]
titledummies=pd.get_dummies(df_train[['Title']], prefix_sep='_')
 df_train = pd.concat([df_train, titledummies], axis=1)
 ttitledummies=pd.get_dummies(df_test[['Title']], prefix_sep='_')
 df_test = pd.concat([df_test, ttitledummies], axis=1)
 print('Title Feature created' )<feature_engineering>
pred_test=model.predict(test )
df_data["Embarked"]=df_data["Embarked"].fillna('S')
 df_train["Embarked"] = df_data['Embarked'][:891]
df_test["Embarked"] = df_data['Embarked'][891:]
print('Missing Embarkations Added' )<categorify>
res=test_data.filter(['Id'],axis=1)
 res['winPlacePerc']=pred_test
<feature_engineering><EOS> 
res.to_csv('submission.csv', index=False )