Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
sns.set_style('whitegrid')
 %matplotlib inline
df_sub=df_sub.reset_index().drop('index',axis=1 )<save_to_csv>
df_train=pd.read_csv('.. /input/train.csv')
 df_test=pd.read_csv('.. /input/test.csv')
 df_titanic=pd.concat([df_train,df_test] ).reset_index(drop=True)
 df_titanic.info()
df_sub.to_csv('submission.csv', index=False )<string_transform>
df_age=df_titanic["Age"].dropna()
mean_age=int(df_titanic["Age"].dropna().mean())
 std_age=int(df_titanic["Age"].dropna().std())
 df_titanic["Age"]=df_titanic["Age"].apply(lambda x: np.random.randint(int(mean_age-std_age),int(mean_age+std_age)) if pd.isnull(x)else x )
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>
print(df_titanic[pd.isnull(df_titanic["Embarked"])] )
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>
df_titanic["Embarked"]=df_titanic["Embarked"].fillna("C" )
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>
print(df_titanic[pd.isnull(df_titanic["Fare"])])
 mean=df_titanic[df_titanic["Embarked"]=='S']["Fare"].mean()
std=df_titanic[df_titanic["Embarked"]=='S']["Fare"].std()
df_titanic["Fare"]=df_titanic["Fare"].fillna(random.randint(int(mean-std),int(mean+std)) )
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>
df_titanic.loc[df_titanic["Age"]<=16,"Sex"]="Child"
df_titanic.loc[(df_titanic["Age"]>16)&(df_titanic["Parch"]>0)&(df_titanic["Sex"]=="female"),"Sex"]="Mother"
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>
df_titanic["Parch"].value_counts()
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>
df_titanic["SibSp"].value_counts()
class Estimator(object):
def fit(self, x_train, y_train, x_valid, y_valid):
raise NotImplementedException
def predict(self, x):
raise NotImplementedException<train_model>
df_titanic["family"]=df_titanic["Parch"]+df_titanic["SibSp"]
df_titanic.loc[(df_titanic["family"]>=1)&(df_titanic["family"]<=3),"family"]=1
df_titanic.loc[df_titanic["family"] >3 |(df_titanic["family"]==0),"family"]=0
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>
df_name=pd.DataFrame(df_titanic["Name"].str.extract('([A-Za-z]+\.) '))
print(df_name["Name"].value_counts() )
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>
VIP=["Master.","Rev.","Dr.","Col.","Major.","Jonkheer.","Dona.","Capt.","Don.","Sir.","Lady.","Countess."]
df_name.loc[df_name["Name"].isin(VIP),"Name"]="VIP"
df_name.loc[df_name["Name"].isin(["Mlle.","Ms."]),"Name"]="Miss."
df_name.loc[df_name["Name"]=="Mme.","Name"]="Mrs."
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>
df_titanic["Name"]=df_name["Name"]
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>
df_cabin=pd.DataFrame(df_titanic[["Cabin","Pclass","Fare"]].dropna())
 df_cabin["Cabin"]=df_cabin["Cabin"].astype(str ).str[0]
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(df_cabin["Cabin"].value_counts() )
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>
df_train=df_titanic.loc[0:890,]
df_test=df_titanic.loc[891:1308,]
def pipeline_fit(estimator, df_train, scaler=None):
add_new_features_1(df_train)
 add_new_features_2(df_train)
 add_new_features_3(df_train)
 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<predict_on_test>
X=df_train.drop(["PassengerId","Cabin","Ticket","Survived","SibSp","Parch"],axis=1)
 y=df_train["Survived"]
Submission_set=df_test.drop(["PassengerId","Cabin","Ticket","Survived","SibSp","Parch"],axis=1 )
def pipeline_predict(trained_estimators, df_test, scaler=None):
add_new_features_1(df_test)
 add_new_features_2(df_test)
 add_new_features_3(df_test)
 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>
X=pd.get_dummies(X)
 Submission_set=pd.get_dummies(Submission_set)
 X.info()
X.describe()
df_train = pd.read_csv('.. /input/train_V2.csv', index_col='Id')
 df_train.shape<feature_engineering>
X=scale(X)
 Submission_set=scale(Submission_set )
df_train = reduce_mem_usage(df_train )<set_options>
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2, random_state=42 )
gc.collect()<drop_column>
Neighbors=KNeighborsClassifier()
n_neighbors = np.arange(1,50)
 param_grid = {'n_neighbors': n_neighbors,
'metric':["cityblock","euclidean"]}
df_train.drop(df_train[df_train['winPlacePerc'].isnull() ].index, inplace=True )<import_modules>
Neighbors_cv = GridSearchCV(Neighbors,param_grid, cv=5)
 Neighbors_cv.fit(X_train,y_train)
 print("Tuned k-NN classifier Parameters: {}".format(Neighbors_cv.best_params_))
print("Best score is {}".format(Neighbors_cv.best_score_))
from sklearn.linear_model import LinearRegression<set_options>
y_pred=Neighbors_cv.predict(X_test )
del df_train
del df_oof
gc.collect()<load_from_csv>
print("Confusion Matrix :")
 print(confusion_matrix(y_pred,y_test))
print("Classification report :")
 print(classification_report(y_pred,y_test))
df_test = pd.read_csv('.. /input/test_V2.csv', index_col = 'Id')
 df_test.shape<feature_engineering>
Sub_pred=Neighbors_cv.predict(Submission_set ).astype(int )
<create_dataframe><EOS> 
submission = pd.DataFrame({
"PassengerId": df_test["PassengerId"],
"Survived": Sub_pred
})
 submission.to_csv('titanic.csv', index=False )