Kaggle competition: Titanic - Machine Learning from Disaster
X_submit = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv' )<drop_column>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv" )
test.drop(columns=['winPlacePerc'], inplace = True )<predict_on_test>
def missing_percentage(df):

 total = df.isnull().sum().sort_values(ascending = False)
 percent = round(df.isnull().sum().sort_values(ascending = False)/len(df)*100,2)
 return pd.concat([total, percent], axis=1, keys=['Total','Percent'] )
y_pred_submit=model_full.predict(test )<concatenate>
%timeit -r2 -n10 missing_percentage(train )
submission = pd.concat([X_submit,pd.Series(y_pred_submit, name='winPlacePerc')], axis=1 )<predict_on_test>
missing_percentage(train )
submission['pred_winPlacePerc'] = submission.iloc[:,-1]
def adjust_pred(x):
space = 1/(x.maxPlace-1)
 return round(x.pred_winPlacePerc / space)* space
submission['adj_winPlacePerc'] = adjust_pred(submission)
 submission.head()<prepare_output>
%%timeit -r2 -n10
missing_percentage(test )
submission = submission.loc[:,['Id','adj_winPlacePerc']]
submission.columns = ['Id','winPlacePerc']
submission.head()<split>
missing_percentage(test )
<train_model>
def percent_value_counts(df, feature):

 percent = pd.DataFrame(round(df.loc[:,feature].value_counts(dropna=False, normalize=True)*100,2))
total = pd.DataFrame(df.loc[:,feature].value_counts(dropna=False))
total.columns = ["Total"]
percent.columns = ['Percent']
return pd.concat([total, percent], axis = 1)
<save_to_csv>
percent_value_counts(train, 'Embarked' )
submission.to_csv('submission.csv', index=False )<save_to_csv>
percent_value_counts(train, 'Embarked' )
submission.to_csv('submission.csv', index=False )<load_from_csv>
train[train.Embarked.isnull() ]
%matplotlib inline
warnings.filterwarnings("ignore")
 train = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 test = pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv')
 y_t = 'winPlacePerc'
train.drop(2744604, inplace=True)
 train['playersJoined'] = train.groupby('matchId')['matchId'].transform('count')
 train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['maxPlaceNorm'] = train['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['matchDurationNorm'] = train['matchDuration']*(( 100-train['playersJoined'])/100 + 1)
 train['healsANDboosts'] = train['heals']+train['boosts']
train['totalDistance'] = train['walkDistance']+train['rideDistance']+train['swimDistance']
train['killsWithoutMoving'] =(( train['kills'] > 0)&(train['totalDistance'] == 0))
train['headshot_rate'] = train['headshotKills'] / train['kills']
train['headshot_rate'] = train['headshot_rate'].fillna(0)
 test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['maxPlaceNorm'] = test['maxPlace']*(( 100-test['playersJoined'])/100 + 1)
 test['matchDurationNorm'] = test['matchDuration']*(( 100-test['playersJoined'])/100 + 1)
 test['healsANDboosts'] = test['heals']+test['boosts']
test['totalDistance'] = test['walkDistance']+test['rideDistance']+test['swimDistance']
test['killsWithoutMoving'] =(( test['kills'] > 0)&(test['totalDistance'] == 0))
test['headshot_rate'] = test['headshotKills'] / test['kills']
test['headshot_rate'] = test['headshot_rate'].fillna(0)
 train.drop(train[train['killsWithoutMoving'] == True].index, inplace=True)
 train.drop(train[train['roadKills'] > 10].index, inplace=True)
 train.drop(train[train['kills'] > 30].index, inplace=True)
 train.drop(train[train['longestKill'] >= 1000].index, inplace=True)
 train.drop(train[train['walkDistance'] >= 10000].index, inplace=True)
 train.drop(train[train['rideDistance'] >= 20000].index, inplace=True)
 train.drop(train[train['swimDistance'] >= 2000].index, inplace=True)
 train.drop(train[train['weaponsAcquired'] >= 80].index, inplace=True)
 train.drop(train[train['heals'] >= 40].index, inplace=True)
 train = pd.get_dummies(train, columns=['matchType'])
 test = pd.get_dummies(test, columns=['matchType'] )<split>
train.Embarked.fillna("C", inplace=True )
s = 500000
train = train.sample(s)
 k = list(train.columns)
 k.remove("Id")
 k.remove("matchId")
 k.remove("groupId")
 y_ = np.array(train[y_t])
 k.remove(y_t)
 x_ = train[k]
x_test = test[k]
random_seed=1
x_train, x_train_test, y_train, y_train_test = train_test_split(x_, y_, test_size = 0.1, random_state=random_seed)
 md = RandomForestRegressor(n_estimators=80, min_samples_leaf=3, max_features=0.5, n_jobs=-1 )<predict_on_test>
print("Train Cabin missing: " + str(train.Cabin.isnull().sum() /len(train.Cabin)))
 print("Test Cabin missing: " + str(test.Cabin.isnull().sum() /len(test.Cabin)) )
%%time
md.fit(x_train, y_train)
 print('train_MAE: ', mean_absolute_error(md.predict(x_train), y_train))
print('test_MAE: ', mean_absolute_error(md.predict(x_train_test), y_train_test))<create_dataframe>
survivers = train.Survived
train.drop(["Survived"],axis=1, inplace=True)
 all_data = pd.concat([train,test], ignore_index=False)
 all_data.Cabin.fillna("N", inplace=True )
importance = pd.DataFrame({'cols':x_train.columns, 'importance':md.feature_importances_} ).sort_values('importance', ascending=False)
 print(importance[:20] )<save_to_csv>
all_data.Cabin = [i[0] for i in all_data.Cabin]
%%time
p = md.predict(x_test)
 test['winPlacePerc'] = p
submission = test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<import_modules>
percent_value_counts(all_data, "Cabin" )
import os
import warnings
import gc
import time
from tqdm import tqdm
import pandas as pd
import numpy as np
from sklearn.metrics import log_loss
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import LabelEncoder
from itertools import product
from sklearn.model_selection import train_test_split
import lightgbm as lgb<categorify>
all_data.groupby("Cabin")['Fare'].mean().sort_values()
train = pd.get_dummies(train,columns=['matchType'] )<correct_missing_values>
def cabin_estimator(i):

 a = 0
if i<16:
a = "G"
elif i>=16 and i<27:
a = "F"
elif i>=27 and i<38:
a = "T"
elif i>=38 and i<47:
a = "A"
elif i>= 47 and i<53:
a = "E"
elif i>= 53 and i<54:
a = "D"
elif i>=54 and i<116:
a = 'C'
else:
a = "B"
return a
train =train.dropna()<categorify>
with_N = all_data[all_data.Cabin == "N"]
without_N = all_data[all_data.Cabin != "N"]
test = pd.get_dummies(test,columns=['matchType'] )<prepare_x_and_y>
with_N['Cabin'] = with_N.Fare.apply(lambda x: cabin_estimator(x))
all_data = pd.concat([with_N, without_N], axis=0)
 all_data.sort_values(by = 'PassengerId', inplace=True)
 train = all_data[:891]
test = all_data[891:]
train['Survived'] = survivers
y_train =train['winPlacePerc']
x_train =train.drop(['Id','groupId','matchId','winPlacePerc'],axis=1)
<prepare_x_and_y>
test[test.Fare.isnull() ]
X_train = x_train.values
Y_train = y_train.values<split>
missing_value = test[(test.Pclass == 3)& 
(test.Embarked == "S")& 
(test.Sex == "male")].Fare.mean()
test.Fare.fillna(missing_value, inplace=True )
validation_size = 0.20
seed = 1000
X_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size=validation_size, random_state=seed )<train_model>
print("Train age missing value: " + str(( train.Age.isnull().sum() /len(train)) *100)+str("%"))
print("Test age missing value: " + str(( test.Age.isnull().sum() /len(test)) *100)+str("%"))
train_data = lgb.Dataset(data=X_train, label=Y_train)
 valid_data = lgb.Dataset(data=X_valid, label=Y_valid)
 params = {
'num_leaves': 144,
"metric" : "mae",
'learning_rate': 0.1,
'n_estimators': 800,
'max_depth':13,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9
}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1 )<drop_column>
train[train.Fare > 280]
X_test = test.drop(["Id", "groupId", "matchId"], axis = 1 )<save_to_csv>
train['Sex'] = train.Sex.apply(lambda x: 0 if x == "female" else 1)
 test['Sex'] = test.Sex.apply(lambda x: 0 if x == "female" else 1 )
Y_test = lgb_model.predict(X_test)
 submission = pd.DataFrame({
"Id": test['Id'],
"winPlacePerc": Y_test
})
 submission.to_csv('submission.csv', index=False )<save_to_csv>
survived_summary = train.groupby("Survived")
 survived_summary.mean().reset_index()
submission = pd.read_csv(".. /input/submission/submission.csv")
 submission.to_csv('submission.csv', index=False )<define_variables>
survived_summary = train.groupby("Sex")
 survived_summary.mean().reset_index()
def get_sample(df,n):

 idxs = sorted(np.random.permutation(len(df)) [:n])
 return df.iloc[idxs].copy()
def proc_df(df, y_fld, skip_flds=None, do_scale=False, na_dict=None,
preproc_fn=None, max_n_cat=None, subset=None, mapper=None):

 if not skip_flds: skip_flds=[]
if subset: df = get_sample(df,subset)
 df = df.copy()
if preproc_fn: preproc_fn(df)
 y = df[y_fld].values
df.drop(skip_flds+[y_fld], axis=1, inplace=True)
 if na_dict is None: na_dict = {}
for n,c in df.items() : na_dict = fix_missing(df, c, n, na_dict)
 if do_scale: mapper = scale_vars(df, mapper)
 for n,c in df.items() : numericalize(df, c, n, max_n_cat)
 res = [pd.get_dummies(df, dummy_na=True), y, na_dict]
if do_scale: res = res + [mapper]
return res
def rf_feat_importance(m, df):
return pd.DataFrame({'cols':df.columns, 'imp':m.feature_importances_}
).sort_values('imp', ascending=False)
 def set_rf_samples(n):

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n))
def reset_rf_samples() :

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n_samples))<set_options>
survived_summary = train.groupby("Pclass")
 survived_summary.mean().reset_index()
%load_ext autoreload
%autoreload 2
%matplotlib inline
mpl.rc('axes', labelsize=14)
 mpl.rc('xtick', labelsize=12)
 mpl.rc('ytick', labelsize=12)
<load_from_csv>
pd.DataFrame(abs(train.corr() ['Survived'] ).sort_values(ascending = False))
train = pd.read_csv(".. /input/pubg-finish-placement-prediction/train_V2.csv")
 test = pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv" )<filter>
male_mean = train[train['Sex'] == 1].Survived.mean()
female_mean = train[train['Sex'] == 0].Survived.mean()
print("Male survival mean: " + str(male_mean))
print("female survival mean: " + str(female_mean))
print("The mean difference between male and female survival rate: " + str(female_mean - male_mean))
train[train['winPlacePerc'].isnull() ]<drop_column>
male = train[train['Sex'] == 1]
female = train[train['Sex'] == 0]
m_mean_samples = []
f_mean_samples = []
for i in range(50):
m_mean_samples.append(np.mean(random.sample(list(male['Survived']),50,)))
 f_mean_samples.append(np.mean(random.sample(list(female['Survived']),50,)))
 print(f"Male mean sample mean: {round(np.mean(m_mean_samples),2)}")
 print(f"Male mean sample mean: {round(np.mean(f_mean_samples),2)}")
 print(f"Difference between male and female mean sample mean: {round(np.mean(f_mean_samples)- np.mean(m_mean_samples),2)}" )
train.drop(2744604, inplace=True )<filter>
train['name_length'] = [len(i)for i in train.Name]
test['name_length'] = [len(i)for i in test.Name]
def name_length_group(size):
a = ''
if(size <=20):
a = 'short'
elif(size <=35):
a = 'medium'
elif(size <=45):
a = 'good'
else:
a = 'long'
return a
train['nLength_group'] = train['name_length'].map(name_length_group)
 test['nLength_group'] = test['name_length'].map(name_length_group)
train[train['winPlacePerc'].isnull() ]<feature_engineering>
train["title"] = [i.split('.')[0] for i in train.Name]
train["title"] = [i.split(',')[1] for i in train.title]
train['totalDistance'] = train['walkDistance'] + train['rideDistance'] + train['swimDistance']
train['healsAndBoosts'] = train['heals']+train['boosts']<categorify>
test['title'] = [i.split('.')[0].split(',')[1].strip() for i in test.Name]
train['team'] = [1 if i>50 else 2 if(i>25 & i<=50)else 4 for i in train['numGroups']]
train['playersJoined'] = train.groupby('matchId')['matchId'].transform('count' )<feature_engineering>
train["title"] = [i.replace('Ms', 'Miss')for i in train.title]
train["title"] = [i.replace('Mlle', 'Miss')for i in train.title]
train["title"] = [i.replace('Mme', 'Mrs')for i in train.title]
train["title"] = [i.replace('Dr', 'rare')for i in train.title]
train["title"] = [i.replace('Col', 'rare')for i in train.title]
train["title"] = [i.replace('Major', 'rare')for i in train.title]
train["title"] = [i.replace('Don', 'rare')for i in train.title]
train["title"] = [i.replace('Jonkheer', 'rare')for i in train.title]
train["title"] = [i.replace('Sir', 'rare')for i in train.title]
train["title"] = [i.replace('Lady', 'rare')for i in train.title]
train["title"] = [i.replace('Capt', 'rare')for i in train.title]
train["title"] = [i.replace('the Countess', 'rare')for i in train.title]
train["title"] = [i.replace('Rev', 'rare')for i in train.title]
train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['maxPlaceNorm'] = train['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['matchDurationNorm'] = train['matchDuration']*(( 100-train['playersJoined'])/100 + 1)
 to_show = ['Id', 'kills','killsNorm','damageDealt', 'damageDealtNorm', 'maxPlace', 'maxPlaceNorm', 'matchDuration', 'matchDurationNorm']
train[to_show][0:11]<feature_engineering>
train['family_size'] = train.SibSp + train.Parch+1
test['family_size'] = test.SibSp + test.Parch+1
train['headshot_rate'] = train['headshotKills'] / train['kills']
train['headshot_rate'] = train['headshot_rate'].fillna(0 )<drop_column>
train['family_group'] = train['family_size'].map(family_group)
 test['family_group'] = test['family_size'].map(family_group )
train['killsWithoutMoving'] =(( train['kills'] > 0)&(train['totalDistance'] == 0))
train.drop(train[train['killsWithoutMoving'] == True].index, inplace=True )<filter>
train['is_alone'] = [1 if i<2 else 0 for i in train.family_size]
test['is_alone'] = [1 if i<2 else 0 for i in test.family_size]
train[train['roadKills'] > 10]<drop_column>
train.Ticket.value_counts().sample(10 )
train.drop(train[train['roadKills'] > 10].index, inplace=True)
<drop_column>
train.drop(['Ticket'], axis=1, inplace=True)
 test.drop(['Ticket'], axis=1, inplace=True )
train.drop(train[train['kills'] > 30].index, inplace=True )<drop_column>
train['calculated_fare'] = train.Fare/train.family_size
test['calculated_fare'] = test.Fare/test.family_size
train.drop(train[train['longestKill'] >= 1000].index, inplace=True )<drop_column>
def fare_group(fare):

 a= ''
if fare <= 4:
a = 'Very_low'
elif fare <= 10:
a = 'low'
elif fare <= 20:
a = 'mid'
elif fare <= 45:
a = 'high'
else:
a = "very_high"
return a
train['fare_group'] = train['calculated_fare'].map(fare_group)
 test['fare_group'] = test['calculated_fare'].map(fare_group)
train.drop(train[train['walkDistance'] >= 10000].index, inplace=True )<drop_column>
train.drop(['PassengerId'], axis=1, inplace=True)
 test.drop(['PassengerId'], axis=1, inplace=True )
train.drop(train[train['rideDistance'] >= 20000].index, inplace=True )<filter>
train = pd.get_dummies(train, columns=['title',"Pclass", 'Cabin','Embarked','nLength_group', 'family_group', 'fare_group'], drop_first=False)
 test = pd.get_dummies(test, columns=['title',"Pclass",'Cabin','Embarked','nLength_group', 'family_group', 'fare_group'], drop_first=False)
 train.drop(['family_size','Name', 'Fare','name_length'], axis=1, inplace=True)
 test.drop(['Name','family_size',"Fare",'name_length'], axis=1, inplace=True )
train[train['swimDistance'] >= 2000]<drop_column>
train = pd.concat([train[["Survived", "Age", "Sex","SibSp","Parch"]], train.loc[:,"is_alone":]], axis=1)
 test = pd.concat([test[["Age", "Sex"]], test.loc[:,"SibSp":]], axis=1 )
train.drop(train[train['swimDistance'] >= 2000].index, inplace=True )<drop_column>
def completing_age(df):
age_df = df.loc[:,"Age":]
temp_train = age_df.loc[age_df.Age.notnull() ]
temp_test = age_df.loc[age_df.Age.isnull() ]
y = temp_train.Age.values
x = temp_train.loc[:, "Sex":].values
rfr = RandomForestRegressor(n_estimators=1500, n_jobs=-1)
 rfr.fit(x, y)
 predicted_age = rfr.predict(temp_test.loc[:, "Sex":])
 df.loc[df.Age.isnull() , "Age"] = predicted_age
return df
completing_age(train)
 completing_age(test);
train.drop(train[train['weaponsAcquired'] >= 80].index, inplace=True )<drop_column>
def age_group_fun(age):

 a = ''
if age <= 1:
a = 'infant'
elif age <= 4:
a = 'toddler'
elif age <= 13:
a = 'child'
elif age <= 18:
a = 'teenager'
elif age <= 35:
a = 'Young_Adult'
elif age <= 45:
a = 'adult'
elif age <= 55:
a = 'middle_aged'
elif age <= 65:
a = 'senior_citizen'
else:
a = 'old'
return a
train['age_group'] = train['Age'].map(age_group_fun)
 test['age_group'] = test['Age'].map(age_group_fun)
 train = pd.get_dummies(train,columns=['age_group'], drop_first=True)
 test = pd.get_dummies(test,columns=['age_group'], drop_first=True);
train.drop(train[train['heals'] >= 40].index, inplace=True )<count_unique_values>
X = train.drop(['Survived'], axis = 1)
 y = train["Survived"]
print('There are {} different Match types in the dataset.'.format(train['matchType'].nunique()))<categorify>
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size =.33, random_state=0 )
train = pd.get_dummies(train, columns=['matchType'])
 matchType_encoding = train.filter(regex='matchType')
 matchType_encoding.head()<data_type_conversions>
std_scale = StandardScaler()
X_train = std_scale.fit_transform(X_train)
 X_test = std_scale.transform(X_test)
train['groupId'] = train['groupId'].astype('category')
 train['matchId'] = train['matchId'].astype('category')
 train['groupId_cat'] = train['groupId'].cat.codes
train['matchId_cat'] = train['matchId'].cat.codes
train.drop(columns=['groupId', 'matchId'], inplace=True)
 train[['groupId_cat', 'matchId_cat']].head()<drop_column>
logreg = LogisticRegression(solver='liblinear',
penalty= 'l1',random_state = 42 
)
 logreg.fit(X_train,y_train)
 y_pred = logreg.predict(X_test)
train.drop(columns = ['Id'], inplace=True )<feature_engineering>
pd.DataFrame(confusion_matrix(y_test,y_pred),\
columns=["Predicted Not-Survived", "Predicted Survived"],\
index=["Not-Survived","Survived"] )
test['totalDistance'] = test['rideDistance'] + test['walkDistance'] + test['swimDistance']
test['healsAndBoosts'] = test['heals'] + test['boosts']
test['team'] = [1 if i>50 else 2 if(i>25 & i<=50)else 4 for i in test['numGroups']]
test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['maxPlaceNorm'] = test['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 test['matchDurationNorm'] = test['matchDuration']*(( 100-test['playersJoined'])/100 + 1)
 test['headshot_rate'] = test['headshotKills'] / test['kills']
test['headshot_rate'] = test['headshot_rate'].fillna(0)
 test['killsWithoutMoving'] =(( test['kills'] > 0)&(test['totalDistance'] == 0))
test = pd.get_dummies(test, columns=['matchType'])
 test['groupId'] = test['groupId'].astype('category')
 test['matchId'] = test['matchId'].astype('category')
 test['groupId_cat'] = test['groupId'].cat.codes
test['matchId_cat'] = test['matchId'].cat.codes
test.drop(columns=['groupId', 'matchId'], inplace=True )<prepare_x_and_y>
y_pred = logreg.predict(X_test)
 cm = confusion_matrix(y_test, y_pred)
 cm_display = ConfusionMatrixDisplay(cm ).plot()
X = train_sample.drop(columns=['winPlacePerc'])
 y = train_sample['winPlacePerc']<split>
accuracy_score(y_test, y_pred )
def split_vals(a, n : int):
return a[:n].copy() , a[n:].copy()
val_perc = 0.12
n_valid = int(val_perc * sample)
 n_trn = len(X)- n_valid
raw_train, raw_valid = split_vals(train_sample, n_trn)
 X_train, X_valid = split_vals(X, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape )<compute_test_metric>
recall_score(y_test, y_pred )
def print_score(m : xgb):
res = ['mae train: ', mean_absolute_error(m.predict(X_train), y_train),
'mae val: ', mean_absolute_error(m.predict(X_valid), y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )<compute_test_metric>
precision_score(y_test, y_pred )
<choose_model_class>
print(classification_report(y_test, y_pred))
rnd_mod_1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt', n_jobs=-1)
<train_model>
cv = StratifiedShuffleSplit(n_splits = 10, test_size =.25, random_state = 0)
 column_names = X.columns
X = std_scale.fit_transform(X)
 accuracies = cross_val_score(LogisticRegression(solver='liblinear'), X,y, cv = cv)
 print("Cross-Validation accuracy scores:{}".format(accuracies))
print("Mean Cross-Validation accuracy score: {}".format(round(accuracies.mean() ,5)) )
rnd_mod_1.fit(X_train, y_train)
 print_score(rnd_mod_1)
<features_selection>
C_vals = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
cv = StratifiedShuffleSplit(n_splits = 10, test_size =.25)
 param = {'C': C_vals}
logreg = LogisticRegression()
grid = GridSearchCV(
 estimator=LogisticRegression() ,
param_grid = param,
scoring = 'accuracy',
n_jobs =-1,
cv = cv 
)
 grid.fit(X, y )
to_keep = fi[fi.imp>0.005].cols
print('Significant features: ', len(to_keep))
to_keep<split>
print(grid.best_score_)
 print(grid.best_params_)
 print(grid.best_estimator_)
X_keep = X[to_keep].copy()
X_train, X_valid = split_vals(X_keep, n_trn )<train_model>
logreg_grid = grid.best_estimator_
logreg_grid.score(X,y )
rnd_mod_2.fit(X_train, y_train)
 print_score(rnd_mod_2 )<split>
knn = KNeighborsClassifier(metric='minkowski', p=2)
 cv = StratifiedShuffleSplit(n_splits=10, test_size=.25, random_state=2)
 accuracies = cross_val_score(knn, X,y, cv = cv, scoring='accuracy')
 print("Cross-Validation accuracy scores:{}".format(accuracies))
print("Mean Cross-Validation accuracy score: {}".format(round(accuracies.mean() ,3)) )
val_perc_full = 0.12
n_valid_full = int(val_perc_full * len(train))
n_trn_full = len(train)-n_valid_full
X_full = train.drop(columns = ['winPlacePerc'])
 y = train['winPlacePerc']
X_full = X_full[to_keep]
X_train, X_valid = split_vals(X_full, n_trn_full)
 y_train, y_valid = split_vals(y, n_trn_full)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape )<train_model>
k_range = range(1,31)
 k_scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
 scores = cross_val_score(knn, X,y, cv = cv, scoring = 'accuracy')
 k_scores.append(scores.mean())
 print("Accuracy scores are: {}
".format(k_scores))
print("Mean accuracy score: {}".format(np.mean(k_scores)))
rnd_mod_3.fit(X_train, y_train)
 print_score(rnd_mod_3 )<prepare_output>
k_range = range(1,31)
 weights_options=['uniform','distance']
param = {'n_neighbors':k_range, 'weights':weights_options}
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)
 grid = GridSearchCV(KNeighborsClassifier() , param,cv=cv,verbose = False, n_jobs=-1)
 grid.fit(X,y )
test_pred = test[to_keep].copy()
test_pred.fillna(0, inplace=True)
 test_pred.head()<save_to_csv>
print(grid.best_score_)
 print(grid.best_params_)
 print(grid.best_estimator_ )
predictions = np.clip(a = rnd_mod_3.predict(test_pred), a_min = 0.0, a_max = 1.0)
 pred_df = pd.DataFrame({'Id' : test['Id'], 'winPlacePerc' : predictions})
 pred_df.to_csv("submission.csv", index=False )<string_transform>
knn_grid= grid.best_estimator_
knn_grid.score(X,y )
def take_part_of_data(df, part):
match_ids = df['matchId'].unique()
match_ids_part = np.random.choice(match_ids, int(part * len(match_ids)))
 df = df[df['matchId'].isin(match_ids_part)]
del match_ids
del match_ids_part<feature_engineering>
k_range = range(1,31)
 weights_options=['uniform','distance']
param = {'n_neighbors':k_range, 'weights':weights_options}
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30)
 grid = RandomizedSearchCV(KNeighborsClassifier() , param,cv=cv,verbose = False, n_jobs=-1, n_iter=40)
 grid.fit(X,y )
def add_new_features_1(df):
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['healsAndBoosts'] = df['heals'] + df['boosts']
df['headshotKillsOverKills'] = df['headshotKills'] / df['kills']
df['headshotKillsOverKills'].fillna(0, inplace=True)
 df['killStreaksOverKills'] = df['killStreaks'] / df['kills']
df['killStreaksOverKills'].fillna(0, inplace=True)
 df['killsAndAssists'] = df['kills'] + df['assists']
df['assistsAndRevives'] = df['assists'] + df['revives']<feature_engineering>
print(grid.best_score_)
 print(grid.best_params_)
 print(grid.best_estimator_ )
def add_new_features_2(df):
df['playersJoined'] = df.groupby('matchId')['matchId'].transform('count')
 df['killsAndAssistsOverPlayersJoined'] = df['killsAndAssists'] *(( 100 - df['playersJoined'])/ 100 + 1)
 df['matchDurationOverPlayersJoined'] = df['matchDuration'] *(( 100 - df['playersJoined'])/ 100 + 1)
 df['damageDealtOverPlayersJoined'] = df['damageDealt'] *(( 100 - df['playersJoined'])/ 100 + 1 )<feature_engineering>
knn_ran_grid = grid.best_estimator_
knn_ran_grid.score(X,y )
def add_new_features_3(df):
df['totalDistanceOverKillsAndAssists'] = df['totalDistance'] / df['killsAndAssists']
df['totalDistanceOverKillsAndAssists'].fillna(0, inplace=True)
 df['totalDistanceOverKillsAndAssists'].replace(np.inf, 0, inplace=True)
 df['totalDistanceOverHealsAndBoosts'] = df['totalDistance'] / df['healsAndBoosts']
df['totalDistanceOverHealsAndBoosts'].fillna(0, inplace=True)
 df['totalDistanceOverHealsAndBoosts'].replace(np.inf, 0, inplace=True )<feature_engineering>
gaussian = GaussianNB()
gaussian.fit(X, y)
 y_pred = gaussian.predict(X_test)
 gaussian_accy = round(accuracy_score(y_pred, y_test), 3)
 print(gaussian_accy )
def add_new_features_4(df):
df['headshotRate'] = df['kills'] / df['headshotKills']
df['killStreakRate'] = df['killStreaks'] / df['kills']
df['healsAndBoosts'] = df['heals'] + df['boosts']
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['killPlaceOverMaxPlace'] = df['killPlace'] / df['maxPlace']
df['headshotKillsOverKills'] = df['headshotKills'] / df['kills']
df['distanceOverWeapons'] = df['totalDistance'] / df['weaponsAcquired']
df['walkDistanceOverHeals'] = df['walkDistance'] / df['heals']
df['walkDistanceOverKills'] = df['walkDistance'] / df['kills']
df['killsPerWalkDistance'] = df['kills'] / df['walkDistance']
df["skill"] = df['headshotKills'] + df['roadKills']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
df.fillna(0, inplace=True )<prepare_x_and_y>
Cs = [0.001, 0.01, 0.1, 1,1.5,2,2.5,3,4,5, 10]
gammas = [0.0001,0.001, 0.01, 0.1, 1]
param_grid = {'C': Cs, 'gamma' : gammas}
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)
 grid_search = GridSearchCV(SVC(kernel = 'rbf', probability=True), param_grid, cv=cv)
 grid_search.fit(X,y )
def feature_engineering(df, is_train=True):
df['rankPoints'] = np.where(df['rankPoints'] <= 0, 0, df['rankPoints'])
 features = list(df.columns)
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 if 'winPlacePerc' in features:
features.remove('winPlacePerc')
 y = None
if is_train:
y = df.groupby(['matchId','groupId'])['winPlacePerc'].agg('mean')
 elif 'winPlacePerc' in df.columns:
y = df['winPlacePerc']
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'])
 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'])
 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'])
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 del agg, agg_rank
return df_out, y<predict_on_test>
print(grid_search.best_score_)
 print(grid_search.best_params_)
 print(grid_search.best_estimator_ )
class Estimator(object):
def fit(self, x_train, y_train, x_valid, y_valid):
raise NotImplementedException
def predict(self, x):
raise NotImplementedException<train_model>
svm_grid = grid_search.best_estimator_
svm_grid.score(X,y )
class ScikitLearnEstimator(Estimator):
def __init__(self, estimator):
self.estimator = estimator
def fit(self, x_train, y_train, x_valid, y_valid):
self.estimator.fit(x_train, y_train)
 def predict(self, x):
return self.estimator.predict(x )<train_model>
max_depth = range(1,30)
 max_feature = [21,22,23,24,25,26,28,29,30,'auto']
criterion=["entropy", "gini"]
param = {'max_depth':max_depth,
'max_features':max_feature,
'criterion': criterion}
grid = GridSearchCV(DecisionTreeClassifier() ,
param_grid = param,
verbose=False,
cv=StratifiedShuffleSplit(n_splits=20, random_state=15),
n_jobs = -1)
 grid.fit(X, y )
def fit_predict_step(estimator, x_train, y_train, train_idx, valid_idx, x_test, oof):
x_train_train = x_train[train_idx]
y_train_train = y_train[train_idx]
x_train_valid = x_train[valid_idx]
y_train_valid = y_train[valid_idx]
estimator.fit(x_train_train, y_train_train, x_train_valid, y_train_valid)
 oof_part = estimator.predict(x_train_valid)
 print('MAE:', mean_absolute_error(y_train_valid, oof_part))
oof[valid_idx] = oof_part
y_part = estimator.predict(x_test)
 return y_part<predict_on_test>
print(grid.best_params_)
 print(grid.best_score_)
 print(grid.best_estimator_ )
def fit_predict(estimator, x_train, y_train, x_test):
oof = np.zeros(x_train.shape[0])
 y = np.zeros(x_test.shape[0])
 kf = KFold(n_splits=5, random_state=42)
 for train_idx, valid_idx in kf.split(x_train):
y_part = fit_predict_step(estimator, x_train, y_train, train_idx, valid_idx, x_test, oof)
 y += y_part / kf.n_splits
print('Final MAE:', mean_absolute_error(y_train, oof))
return oof, y<train_model>
dectree_grid = grid.best_estimator_
dectree_grid.score(X,y )
def fit_step(estimator, x_train, y_train, train_idx, valid_idx, oof):
x_train_train = x_train[train_idx]
y_train_train = y_train[train_idx]
x_train_valid = x_train[valid_idx]
y_train_valid = y_train[valid_idx]
estimator.fit(x_train_train, y_train_train, x_train_valid, y_train_valid)
 oof_part = estimator.predict(x_train_valid)
 mae = mean_absolute_error(y_train_valid, oof_part)
 print('MAE:', mae)
 oof[valid_idx] = oof_part
return estimator, mae<train_model>
n_estimators = [140,145,150,155,160];
max_depth = range(1,10);
criterions = ['gini', 'entropy'];
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)
 parameters = {'n_estimators':n_estimators,
'max_depth':max_depth,
'criterion': criterions
}
grid = GridSearchCV(estimator=RandomForestClassifier(max_features='auto'),
param_grid=parameters,
cv=cv,
n_jobs = -1)
 grid.fit(X,y )
def fit(estimator, x_train, y_train):
oof = np.zeros(x_train.shape[0])
 kf = KFold(n_splits=5, random_state=42)
 trained_estimators = []
for train_idx, valid_idx in kf.split(x_train):
e, mae = fit_step(estimator, x_train, y_train, train_idx, valid_idx, oof)
 trained_estimators.append(deepcopy(e))
print('Final MAE:', mean_absolute_error(y_train, oof))
return oof, trained_estimators<predict_on_test>
print(grid.best_score_)
 print(grid.best_params_)
 print(grid.best_estimator_ )
def predict(trained_estimators, x_test):
y = np.zeros(x_test.shape[0])
 for estimator in trained_estimators:
y_part = estimator.predict(x_test)
 y += y_part / len(trained_estimators)
 return y<train_model>
rf_grid = grid.best_estimator_
rf_grid.score(X,y )
def pipeline_fit(estimator, df_train, scaler=None):
add_new_features_4(df_train)
 x_train, y_train = feature_engineering(df_train, is_train=True)
 x_train = reduce_mem_usage(x_train)
 gc.collect()
if not(scaler is None):
scaler.fit(x_train)
 scaled_x_train = scaler.transform(x_train)
 else:
scaled_x_train = x_train.values
oof, trained_estimators = fit(estimator, scaled_x_train, y_train.values)
 del x_train
del scaled_x_train
del y_train
gc.collect()
return oof, trained_estimators<find_best_model_class>
print(classification_report(y_test, y_pred, labels=rf_grid.classes_))
def pipeline_predict(trained_estimators, df_test, scaler=None):
add_new_features_4(df_test)
 x_test, _ = feature_engineering(df_test, is_train=False)
 x_test = reduce_mem_usage(x_test)
 gc.collect()
if not(scaler is None):
scaled_x_test = scaler.transform(x_test)
 else:
scaled_x_test = x_test.values
y = predict(trained_estimators, scaled_x_test)
 del x_test
del scaled_x_test
gc.collect()
return y<load_from_csv>
n_estimators = [10,30,50,70,80,150,160, 170,175,180,185];
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)
 parameters = {'n_estimators':n_estimators,
}
grid = GridSearchCV(BaggingClassifier(base_estimator= None,
bootstrap_features=False),
param_grid=parameters,
cv=cv,
n_jobs = -1)
 grid.fit(X,y )
df_train = pd.read_csv('.. /input/train_V2.csv', index_col='Id')
 df_train.shape<feature_engineering>
print(grid.best_score_)
 print(grid.best_params_)
 print(grid.best_estimator_ )
df_train = reduce_mem_usage(df_train )<set_options>
bagging_grid = grid.best_estimator_
bagging_grid.score(X,y )
gc.collect()<drop_column>
n_estimators = [100,140,145,150,160, 170,175,180,185];
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)
 learning_r = [0.1,1,0.01,0.5]
parameters = {'n_estimators':n_estimators,
'learning_rate':learning_r
}
grid = GridSearchCV(AdaBoostClassifier(base_estimator= None, 
),
param_grid=parameters,
cv=cv,
n_jobs = -1)
 grid.fit(X,y )
df_train.drop(df_train[df_train['winPlacePerc'].isnull() ].index, inplace=True )<import_modules>
print(grid.best_score_)
 print(grid.best_params_)
 print(grid.best_estimator_ )
import torch
from torch.utils.data import DataLoader, TensorDataset
import torch.nn as nn
from torch.nn.utils.weight_norm import weight_norm<set_options>
adaBoost_grid = grid.best_estimator_
adaBoost_grid.score(X,y )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 device<train_model>
gradient_boost = GradientBoostingClassifier()
gradient_boost.fit(X, y)
 y_pred = gradient_boost.predict(X_test)
 gradient_accy = round(accuracy_score(y_pred, y_test), 3)
 print(gradient_accy )
class PyTorch(Estimator):
def fit(self, x_train, y_train, x_valid, y_valid):
train_tensor = TensorDataset(
 torch.from_numpy(x_train.astype('float32')) ,
torch.from_numpy(y_train.astype('float32')))
 train_loader = DataLoader(train_tensor, batch_size=256, shuffle=True)
 self.model = nn.Sequential(
 weight_norm(nn.Linear(x_train.shape[1], 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 1)) ).to(device)
 for m in self.model:
if isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight_v)
 nn.init.kaiming_normal_(m.weight_g)
 nn.init.constant_(m.bias, 0)
 criterion = nn.L1Loss()
optimizer = torch.optim.Adam(self.model.parameters() , betas=(0.9, 0.999), lr=1e-3)
 self.model.train()
n_epochs = 50
for epoch in range(n_epochs):
epoch_loss = 0.0
for train_part, y_part in train_loader:
optimizer.zero_grad()
y_pred_part = self.model(train_part.to(device))
loss = criterion(y_pred_part.reshape(-1), y_part.to(device))
loss.backward()
optimizer.step()
epoch_loss += y_pred_part.shape[0] * loss.item()
print('Epoch %3d / %3d.Loss = %.5f' %(epoch + 1, n_epochs, epoch_loss / x_train.shape[0]))
def predict(self, x):
self.model.eval()
x_tensor = torch.from_numpy(x.astype('float32'))
x_loader = DataLoader(x_tensor, batch_size=256, shuffle=False)
 y_pred = np.empty(0)
 with torch.no_grad() :
for x_part in x_loader:
y_pred_part = self.model(x_part.to(device)).data.cpu().numpy().reshape(-1)
 y_pred = np.append(y_pred, y_pred_part)
 return y_pred<train_model>
from xgboost import XGBClassifier
%%time
scaler = StandardScaler()
oof, trained_estimators = pipeline_fit(PyTorch() , df_train, scaler )<set_options>
XGBClassifier = XGBClassifier()
XGBClassifier.fit(X, y)
 y_pred = XGBClassifier.predict(X_test)
 XGBClassifier_accy = round(accuracy_score(y_pred, y_test), 3)
 print(XGBClassifier_accy )
del df_train
gc.collect()<load_from_csv>
ExtraTreesClassifier = ExtraTreesClassifier()
ExtraTreesClassifier.fit(X, y)
 y_pred = ExtraTreesClassifier.predict(X_test)
 extraTree_accy = round(accuracy_score(y_pred, y_test), 3)
 print(extraTree_accy)
df_test = pd.read_csv('.. /input/test_V2.csv', index_col = 'Id')
 df_test.shape<feature_engineering>
GaussianProcessClassifier = GaussianProcessClassifier()
GaussianProcessClassifier.fit(X, y)
 y_pred = GaussianProcessClassifier.predict(X_test)
 gau_pro_accy = round(accuracy_score(y_pred, y_test), 3)
 print(gau_pro_accy )
df_test = reduce_mem_usage(df_test )<create_dataframe>
voting_classifier = VotingClassifier(estimators=[ 
('lr_grid', logreg_grid), 
('svc', svm_grid), 
('random_forest', rf_grid), 
('gradient_boosting', gradient_boost), 
('decision_tree_grid',dectree_grid), 
('knn_classifier', knn_grid), 
('XGB_Classifier', XGBClassifier), 
('bagging_classifier', bagging_grid), 
('adaBoost_classifier',adaBoost_grid), 
('ExtraTrees_Classifier', ExtraTreesClassifier), 
('gaussian_classifier',gaussian), 
('gaussian_process_classifier', GaussianProcessClassifier)
 ],voting='hard')
 voting_classifier = voting_classifier.fit(X,y )
df_test_id = pd.DataFrame(index=df_test.index )<set_options>
y_pred = voting_classifier.predict(X_test)
 voting_accy = round(accuracy_score(y_pred, y_test), 3)
 print(voting_accy )
gc.collect()<predict_on_test>
all_models = [logreg_grid,
knn_grid,
knn_ran_grid,
svm_grid,
dectree_grid,
rf_grid,
bagging_grid,
adaBoost_grid,
voting_classifier]
c = {}
for i in all_models:
a = i.predict(X_test)
 b = accuracy_score(a, y_test)
 c[i] = b
<set_options><EOS> 
test_prediction =(max(c, key=c.get)).predict(test)
 submission = pd.DataFrame({
"PassengerId": passengerid,
"Survived": test_prediction
})
 submission.PassengerId = submission.PassengerId.astype(int)
 submission.Survived = submission.Survived.astype(int)
 submission.to_csv("titanic1_submission.csv", index=False )