Kaggle competition: Titanic - Machine Learning from Disaster
for stat in match_stats:
concat['matchRel_' + stat] = concat[stat] / concat.groupby('matchId')[stat].transform('mean' )<define_variables>
df = pd.read_csv('.. /input/train.csv')
 df.head()
drop_features = ["winPlacePerc", "Id", "groupId", "matchId"]
feats = [c for c in concat.columns if c not in drop_features]<define_variables>
d = df[['Survived','Pclass','Sex', 'Age', 'SibSp',
'Parch','Embarked']]
d = d.dropna()
aggs = {
'grpSizeMult' : ['mean'],
'groupId_count' : ['mean'],
'matchId_count' : ['mean'],
'winPlacePerc' : ['mean'],
}
for c in feats:
if c not in aggs:
aggs[c] = ['mean', 'min', 'max', 'std']
new_cols = [k + '_' + agg for k in aggs.keys() for agg in aggs[k]]<groupby>
from sklearn import preprocessing
groups = concat.groupby('groupId' ).agg(aggs)
<rename_columns>
import seaborn as sns
import matplotlib.pyplot as plt
groups.columns = new_cols<set_options>
df['title'] = df.title.replace(['Lady', 'Countess','Capt', 'Col',\
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['title'] = df['title'].replace('Mlle', 'Miss')
 df['title'] = df['title'].replace('Ms', 'Miss')
 df['title'] = df['title'].replace('Mme', 'Mrs')
 df[['title','Survived']].groupby('title' ).mean()
del concat
gc.collect()<groupby>
df.title = df.title.fillna(0)
 df['title'] = df.title.map({'Rare':0, 'Master':1, 'Miss':2, 'Mr':3, 'Mrs':4})
 df.Sex = df.Sex.map({'female':0, 'male':1} ).astype(int )
groups = reduce_mem_usage(groups )<init_hyperparams>
df.Age = df.Age.fillna(df.Age.mean())
 df.Embarked = df.Embarked.fillna('S' )
params = {
'num_leaves': 144,
'learning_rate': 0.1,
'n_estimators': 800,
'max_depth':13,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9
}<define_search_model>
df = df.drop(columns = ['PassengerId', 'Name', 'Ticket', 'Cabin'])
 df['Embarked'] = df.Embarked.map({'S':0,'C':1,'Q':2} ).astype(int)
 df.Age = round(df.Age ).astype(int)
 df.head()
def kfold_lightgbm(df, num_folds, stratified = False, debug= False):
train_df = df[df['winPlacePerc_mean'].notnull() ]
test_df = df[df['winPlacePerc_mean'].isnull() ]
print("Starting LightGBM.Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
del df
gc.collect()
if stratified:
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=1001)
 else:
folds = KFold(n_splits= num_folds, shuffle=True, random_state=1001)
 oof_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
drop_features = ["winPlacePerc_mean", "Id", "groupId", "matchId"]
feats = [f for f in train_df.columns if f not in drop_features]
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df[feats], train_df['winPlacePerc_mean'])) :
train_x, train_y = train_df[feats].iloc[train_idx], train_df['winPlacePerc_mean'].iloc[train_idx]
valid_x, valid_y = train_df[feats].iloc[valid_idx], train_df['winPlacePerc_mean'].iloc[valid_idx]
reg = LGBMRegressor(num_leaves=params['num_leaves'], learning_rate=params['learning_rate'],
n_estimators=params['n_estimators'], max_depth=params['max_depth'],
max_bin = params['max_bin'], bagging_fraction = params['bagging_fraction'],
bagging_freq = params['bagging_freq'], feature_fraction = params['feature_fraction'] 
)
 reg.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)],
eval_metric= 'mae', verbose= 50, early_stopping_rounds= 200)
 oof_preds[valid_idx] = reg.predict(valid_x)
 sub_preds += reg.predict(test_df[feats])/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = reg.feature_importances_
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('Fold %2d MAE : %.6f' %(n_fold + 1, mean_absolute_error(valid_y, oof_preds[valid_idx])))
 del reg, train_x, train_y, valid_x, valid_y
gc.collect()
print('Full MAE score %.6f' % mean_absolute_error(train_df['winPlacePerc_mean'], oof_preds))
if not debug:
test_df['winPlacePerc'] = sub_preds
return feature_importance_df, test_df[['winPlacePerc']]<concatenate>
df.loc[(round(df['Age'])<=16),'Age'] = 0
df.loc[(round(df['Age'])>16)&(round(df['Age'])<=32),'Age'] = 1
df.loc[(round(df['Age'])>32)&(round(df['Age'])<=48),'Age'] = 2
df.loc[(round(df['Age'])>48)&(round(df['Age'])<=64),'Age'] = 3
df.loc[(round(df['Age'])>64)&(round(df['Age'])<=80),'Age'] = 4
feat_importances, test_df = kfold_lightgbm(groups, num_folds=5, stratified=False, debug=False )<compute_test_metric>
df[['Age','Survived']].groupby('Age',as_index = False ).mean()
display_importances(feat_importances )<sort_values>
df.loc[(round(df['Fare'])<=7.9),'Fare'] = 0
df.loc[(round(df['Fare'])>7.9)&(round(df['Fare'])<=14.45),'Fare'] = 1
df.loc[(round(df['Fare'])>14.45)&(round(df['Fare'])<=31.0),'Fare'] = 2
df.loc[(round(df['Fare'])>31.0),'Fare'] = 3
feat_gb = feat_importances.groupby('feature' ).mean().sort_values(by="importance", ascending=False )<save_to_csv>
df[['Fare','Survived']].groupby('Fare' ).mean()
feat_gb.to_csv("feature_importance.csv" )<load_from_csv>
df['Familysize'] = df['SibSp'] + df['Parch'] + 1
df[['Familysize','Survived']].groupby('Familysize' ).mean()
test = pd.read_csv(".. /input/test.csv" )<merge>
df['Familysize'] = df['SibSp'] + df['Parch'] + 1
df.loc[df['Familysize'] == 1,'Familysize'] = 0
df.loc[df.Familysize >1,'Familysize'] = 1
test = test.merge(test_df, right_index=True, left_on='groupId' )<save_to_csv>
df[['Familysize','Survived']].groupby('Familysize' ).mean()
test[['Id', 'winPlacePerc']].to_csv("submission.csv", index=False )<set_options>
df = df.drop(columns = ['Parch','SibSp'] )
warnings.filterwarnings('ignore')
 %matplotlib inline
py.init_notebook_mode(connected=True)
<load_from_csv>
df = df.drop(columns = ['AgeBand','FareBand'])
 df.head()
debug = False
if debug == True:
df_train = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 df_test = pd.read_csv('.. /input/test_V2.csv')
 else:
df_train = pd.read_csv('.. /input/train_V2.csv')
 df_test = pd.read_csv('.. /input/test_V2.csv' )<filter>
df.Embarked = df.Embarked.fillna(0)
 df['Embarked'] = round(df['Embarked'] ).astype(int)
 df.Fare = df.Fare.astype(int)
 df.info()
df_train[df_train['groupId']=='4d4b580de459be']<filter>
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
len(df_train[df_train['matchId']=='a10357fd1a4a91'] )<count_values>
DTC = DecisionTreeClassifier()
RFC = RandomForestClassifier(n_estimators = 500)
 LR = LogisticRegression()
GNB = GaussianNB()
MLPC = MLPClassifier()
svc = SVC(kernel = 'linear', C = 0.1, gamma = 'scale' )
df_train['roadKills'].value_counts()<count_values>
X = np.asanyarray(df.drop(columns = ['Survived']))
y = np.asanyarray(df.Survived)
 x_train,x_test, y_train, y_test = train_test_split(X,y , test_size = 0.2,random_state = 0 )
df_train['teamKills'].value_counts()<count_values>
DTC.fit(x_train,y_train)
 y_hat1 = DTC.predict(x_test)
 RFC.fit(x_train,y_train)
 y_hat2 = RFC.predict(x_test)
 LR.fit(x_train,y_train)
 y_hat3 = LR.predict(x_test)
 GNB.fit(x_train,y_train)
 y_hat4 = GNB.predict(x_test)
 MLPC.fit(x_train,y_train)
 y_hat5 = MLPC.predict(x_test)
 svc.fit(x_train,y_train)
 y_hat6 = svc.predict(x_test )
df_train['headshotKills'].value_counts()<count_values>
print('Accuracy_yhat1_DTC=',accuracy_score(y_test,y_hat1))
print('Accuracy_yhat2_RFC=',accuracy_score(y_test,y_hat2))
print('Accuracy_yhat3_LR=',accuracy_score(y_test,y_hat3))
print('Accuracy_yhat4_GNB=',accuracy_score(y_test,y_hat4))
print('Accuracy_yhat5_MLPC=',accuracy_score(y_test,y_hat5))
print('Accuracy_yhat6_SVC=',accuracy_score(y_test,y_hat6))
df_train['vehicleDestroys'].value_counts()<filter>
DTC.score(x_train,y_train )
df_train = df_train[df_train['Id']!='f70c74418bb064']<feature_engineering>
RFC.score(x_train,y_train )
headshot = df_train[['kills','winPlacePerc','headshotKills']]
headshot['headshotrate'] = headshot['kills'] / headshot['headshotKills']<drop_column>
ETC = ExtraTreesClassifier(n_estimators = 500)
 ETC.fit(x_train,y_train)
 y_hat7 = ETC.predict(x_test)
 ETC.score(x_train,y_train)
 print('Accuracy_yhat7_ETC=',accuracy_score(y_test,y_hat7))
del headshot<feature_engineering>
df1 = pd.read_csv('.. /input/test.csv')
 df1.head()
df_train['headshotrate'] = df_train['kills']/df_train['headshotKills']
df_test['headshotrate'] = df_test['kills']/df_test['headshotKills']<feature_engineering>
df1['title'] = df1['Name'].str.extract('([A-Za-z]+)\.', expand = False)
 df1['title'] = df1.title.replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df1['title'] = df1['title'].replace('Mlle', 'Miss')
 df1['title'] = df1['title'].replace('Ms', 'Miss')
 df1['title'] = df1['title'].replace('Mme', 'Mrs' )
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()<drop_column>
df1.title = df1.title.fillna(0)
 df1['title'] = df1.title.map({'Rare':0, 'Master':1, 'Miss':2, 'Mr':3, 'Mrs':4})
 df1.Sex = df1.Sex.map({'female':0, 'male':1} ).astype(int )
del healthitems<feature_engineering>
df1 = df1.drop(columns = ['PassengerId', 'Name', 'Ticket', 'Cabin'])
 df1['Embarked'] = df1.Embarked.map({'S':0,'C':1,'Q':2} ).astype(int)
 df1.Age = df1.Age.fillna(df1.Age.mean())
 df1.Age = round(df1.Age ).astype(int)
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()<set_options>
df1.loc[(round(df1['Age'])<=16),'Age'] = 0
df1.loc[(round(df1['Age'])>16)&(round(df1['Age'])<=32),'Age'] = 1
df1.loc[(round(df1['Age'])>32)&(round(df1['Age'])<=48),'Age'] = 2
df1.loc[(round(df1['Age'])>48)&(round(df1['Age'])<=64),'Age'] = 3
df1.loc[(round(df1['Age'])>64)&(round(df1['Age'])<=80),'Age'] = 4
del df_train,df_test;
gc.collect()<load_from_csv>
df1.loc[(round(df1['Fare'])<=7.9),'Fare'] = 0
df1.loc[(round(df1['Fare'])>7.9)&(round(df1['Fare'])<=14.45),'Fare'] = 1
df1.loc[(round(df1['Fare'])>14.45)&(round(df1['Fare'])<=31.0),'Fare'] = 2
df1.loc[(round(df1['Fare'])>31.0),'Fare'] = 3
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 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 feature")
 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 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 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 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'])
 print("get match size feature")
 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)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
x_train, y_train, train_columns, _ = feature_engineering(True,False)
 x_test, _, _ , test_idx = feature_engineering(False,True )<feature_engineering>
df1['Familysize'] = df1['SibSp'] + df1['Parch'] + 1
df1.loc[df1['Familysize'] == 1,'Familysize'] = 0
df1.loc[df1.Familysize >1,'Familysize'] = 1
x_train['headshotrate'] = x_train['kills']/x_train['headshotKills']
x_test['headshotrate'] = x_test['kills']/x_test['headshotKills']
x_train['killStreakrate'] = x_train['killStreaks']/x_train['kills']
x_test['killStreakrate'] = x_test['killStreaks']/x_test['kills']
x_train['healthitems'] = x_train['heals'] + x_train['boosts']
x_test['healthitems'] = x_test['heals'] + x_test['boosts']
del x_train['heals'];del x_test['heals']
train_columns.append('headshotrate')
 train_columns.append('killStreakrate')
 train_columns.append('healthitems')
 train_columns.remove('heals' )<drop_column>
df1 = df1.drop(columns = ['Parch','SibSp'])
 df1.Fare = df1.Fare.fillna(0)
 df1.head()
x_train = reduce_mem_usage(x_train)
 x_test = reduce_mem_usage(x_test )<set_options>
predictions = RFC.predict(np.asanyarray(df1))
warnings.filterwarnings("ignore")
<split>
df3 = pd.read_csv('.. /input/test.csv')
 submissions = pd.DataFrame({'PassengerID':df3['PassengerId'],'Survived':predictions})
 submissions.to_csv('submission.csv',index = False, header = True )