Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
np.random.seed(0)
 rn.seed(0)
 colors = ["red", "blue", "orange", "pink"]
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
tfa = np.vstack(
 df_all.timestamp_first_active.astype(str ).apply(
 lambda x: list(map(int, [x[:4], x[4:6], x[6:8],
x[8:10], x[10:12],
x[12:14]]))
).values 
)
 df_all['tfa_year'] = tfa[:,0]
df_all['tfa_month'] = tfa[:,1]
df_all['tfa_day'] = tfa[:,2]
df_all = df_all.drop(['timestamp_first_active'], axis=1 )<categorify>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
ohe_feats = ['gender', 'signup_method', 'signup_flow', 'language',
'affiliate_channel', 'affiliate_provider',
'first_affiliate_tracked', 'signup_app',
'first_device_type', 'first_browser']
for f in ohe_feats:
df_all_dummy = pd.get_dummies(df_all[f], prefix=f)
 df_all = df_all.drop([f], axis=1)
 df_all = pd.concat(( df_all, df_all_dummy), axis=1 )<split>
print("Train data missing value count for each feature")
 print(train_data.isnull().sum())
 print("Test data missing value count for each feature")
 print(test_data.isnull().sum() )
X = df_all.iloc[:piv_train,:]
le = LabelEncoder()
y = le.fit_transform(labels)
 X_test = df_all.iloc[piv_train:,:]<train_model>
missing_value_fill_method = "2.3"
params = {'eta': 0.1,
'max_depth': 8,
'nround': 100,
'subsample': 0.7,
'colsample_bytree': 0.8,
'seed': 1,
'objective': 'multi:softprob',
'eval_metric':'ndcg',
'num_class': 12,
'nthread':3}
num_boost_round = 10
dtrain = xgb.DMatrix(X, y)
 clf1 = xgb.train(params=params, dtrain=dtrain, num_boost_round=num_boost_round )<create_dataframe>
if missing_value_fill_method == "2.2":
train_data.fillna(train_data.mean() , inplace=True)
 print(train_data.isnull().sum() )
importance = clf1.get_fscore()
importance_df = pd.DataFrame(
 sorted(importance.items() , key=operator.itemgetter(1)) ,
columns=['feature','fscore']
)<save_to_csv>
if missing_value_fill_method == "2.3":
train_data["Age"] = train_data.groupby(["Pclass", "Sex"])["Age"].apply(lambda x: x.fillna(x.mean()))
test_data["Age"] = test_data.groupby(["Pclass", "Sex"])["Age"].apply(lambda x: x.fillna(x.mean()))
ids = []
cts = []
for i in range(len(id_test)) :
idx = id_test[i]
ids += [idx] * 5
cts += le.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist()
sub = pd.DataFrame(np.column_stack(( ids, cts)) , columns=['id', 'country'])
 sub.to_csv('sub.csv',index=False )<import_modules>
if missing_value_fill_method == "2.3":
train_data["Cabin"] = train_data["Cabin"].apply(lambda s: s[0] if pd.notnull(s)and s[0]!= "T" else "N")
 test_data["Cabin"] = test_data["Cabin"].apply(lambda s: s[0] if pd.notnull(s)and s[0]!= "T" else "N" )
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from xgboost.sklearn import XGBClassifier<define_variables>
if missing_value_fill_method == "2.3":
print(train_data.groupby(["Pclass"])["Cabin"].value_counts() )
np.random.seed(0 )<load_from_csv>
if missing_value_fill_method == "2.3":
train_data.loc[(train_data["Pclass"] == 1)&(train_data["Cabin"] == "N"), "Cabin"]= "C"
train_data.loc[(( train_data["Pclass"] == 2)|(train_data["Pclass"] == 3)) &(train_data["Cabin"] == "N"), "Cabin"]= "F"
test_data.loc[(test_data["Pclass"] == 1)&(test_data["Cabin"] == "N"), "Cabin"]= "C"
test_data.loc[(( test_data["Pclass"] == 2)|(test_data["Pclass"] == 3)) &(test_data["Cabin"] == "N"), "Cabin"]= "F"
df_train = pd.read_csv('.. /input/train_users_2.csv')
 df_test = pd.read_csv('.. /input/test_users.csv')
 labels = df_train['country_destination'].values
df_train = df_train.drop(['country_destination'], axis=1)
 id_test = df_test['id']
piv_train = df_train.shape[0]<concatenate>
if missing_value_fill_method == "2.3":
print(test_data.loc[test_data["Fare"].isnull() ] )
df_all = pd.concat(( df_train, df_test), axis=0, ignore_index=True)
 df_all = df_all.drop(['id', 'date_first_booking'], axis=1)
 df_all = df_all.fillna(-1 )<feature_engineering>
if missing_value_fill_method == "2.3":
sample_fare = test_data.loc[(test_data["Pclass"] == 3)&(test_data["SibSp"] == 0)&(test_data["Embarked"] == "S")][
"Fare"].mean()
print(sample_fare)
 test_data.loc[test_data["Fare"].isnull() , "Fare"] = sample_fare
dac = np.vstack(df_all.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 df_all['dac_year'] = dac[:,0]
df_all['dac_month'] = dac[:,1]
df_all['dac_day'] = dac[:,2]
df_all = df_all.drop(['date_account_created'], axis=1 )<feature_engineering>
if missing_value_fill_method == "2.3":
print(train_data.loc[train_data["Embarked"].isnull() ])
 print(test_data.loc[train_data["Embarked"].isnull() ] )
tfa = np.vstack(df_all.timestamp_first_active.astype(str ).apply(lambda x: list(map(int, [x[:4],x[4:6],x[6:8],x[8:10],x[10:12],x[12:14]])) ).values)
 df_all['tfa_year'] = tfa[:,0]
df_all['tfa_month'] = tfa[:,1]
df_all['tfa_day'] = tfa[:,2]
df_all = df_all.drop(['timestamp_first_active'], axis=1 )<feature_engineering>
if missing_value_fill_method == "2.3":
print(train_data.loc[train_data["Embarked"].isnull() ].head())
 print(train_data.loc[(train_data["Pclass"] == 1)&(train_data["Fare"] <= 80.0)&(train_data['Cabin'] == "B")][
"Embarked"].value_counts())
 train_data.loc[train_data["Embarked"].isnull() , "Embarked"] = "S"
av = df_all.age.values
df_all['age'] = np.where(np.logical_or(av<14, av>100), -1, av )<categorify>
print(train_data.isnull().sum())
 print(test_data.isnull().sum() )
ohe_feats = ['gender', 'signup_method', 'signup_flow', 'language', 'affiliate_channel', 'affiliate_provider', 'first_affiliate_tracked', 'signup_app', 'first_device_type', 'first_browser']
for f in ohe_feats:
df_all_dummy = pd.get_dummies(df_all[f], prefix=f)
 df_all = df_all.drop([f], axis=1)
 df_all = pd.concat(( df_all, df_all_dummy), axis=1 )<categorify>
ser_age, bin_age = pd.cut(train_data["Age"].astype(int), 5, retbins=True, labels=False)
 test_data["Age"] = pd.cut(test_data["Age"], bins=bin_age, labels=False, include_lowest=True)
 train_data["Age"] = ser_age
train_data["Age"] = train_data["Age"].astype("category")
 test_data["Age"] = test_data["Age"].astype("category")
 ser, bins = pd.qcut(train_data["Fare"], 5, retbins=True, labels=False)
 train_data["Fare"] = ser
ser_tst = pd.cut(test_data["Fare"], bins=bins, labels=False, include_lowest=True)
 test_data["Fare"] = ser_tst
train_data["Fare"] = train_data["Fare"].astype("category")
 test_data["Fare"] = test_data["Fare"].astype("category" )
vals = df_all.values
X = vals[:piv_train]
le = LabelEncoder()
y = le.fit_transform(labels)
 X_test = vals[piv_train:]<train_model>
train_data["Family_Size"] = train_data["SibSp"] + train_data["Parch"]
test_data["Family_Size"] = test_data["SibSp"] + test_data["Parch"]
family_size_surv_mean = train_data.groupby(by="Family_Size")["Survived"].mean()
print(family_size_surv_mean )
xgb = XGBClassifier(max_depth = 8, learning_rate = 0.3, n_estimators = 25,
objective='multi:softprob', subsample=0.5, colsample_bytree=0.5, seed=0)
 xgb.fit(X, y)
 y_pred = xgb.predict_proba(X_test )<save_to_csv>
train_data["Travelled_Together"] = train_data.groupby(by='Ticket')['Ticket'].transform('count')
 test_data["Travelled_Together"] = test_data.groupby(by='Ticket')['Ticket'].transform('count' )
ids = []
cts = []
for i in range(len(id_test)) :
idx = id_test[i]
ids += [idx] * 5
cts += le.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist()
sub = pd.DataFrame(np.column_stack(( ids, cts)) , columns=['id', 'country'])
 sub.to_csv('subXgb.csv',index=False )<import_modules>
train_data["Title"] = train_data["Name"].str.split(",", expand=True)[1].str.split(".", expand=True)[0]
test_data["Title"] = test_data["Name"].str.split(",", expand=True)[1].str.split(".", expand=True)[0]
title_counts = train_data["Title"].value_counts() < 10
train_data["Title"] = train_data["Title"].apply(lambda x: "Gen" if title_counts.loc[x] else x)
 title_counts_tst = test_data["Title"].value_counts() < 10
test_data["Title"] = test_data["Title"].apply(lambda x: "Gen" if title_counts_tst.loc[x] else x )
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import gc<load_from_csv>
train_data["Marry_Status"] = 0
train_data["Marry_Status"].loc[train_data["Title"].str.strip() == "Mrs"] = 1
train_data["Marry_Status"] = train_data["Marry_Status"].astype("category")
 test_data["Marry_Status"] = 0
test_data["Marry_Status"].loc[test_data["Title"].str.strip() == "Mrs"] = 1
test_data["Marry_Status"] = test_data["Marry_Status"].astype("category" )
train_identity = pd.read_csv('.. /input/train_identity.csv')
 train_transaction = pd.read_csv('.. /input/train_transaction.csv' )<count_values>
train_data["Pclass"] = train_data["Pclass"].astype("category")
 test_data["Pclass"] = test_data["Pclass"].astype("category")
 y = train_data["Survived"]
train_transaction['isFraud'].value_counts()<merge>
features = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Cabin", "Embarked", "Family_Size",
"Travelled_Together", "Title", "Marry_Status"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features] )
train = train_transaction.merge(train_identity, on='TransactionID', how='left')
 del train_identity, train_transaction
gc.collect()<load_from_csv>
chosed_model = "rf"
if chosed_model == "dp":
model = Sequential()
model.add(Dense(10, input_dim=36, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.fit(X, y, epochs=100, batch_size=5, verbose=0)
 predictions = model.predict_classes(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions.flatten() })
 if(chosed_model=="rf")|(chosed_model == "xboost"):
model = RandomForestClassifier(n_estimators=200, max_depth=7, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('/kaggle/working/my_submission_rf.csv', index=False)
 if(chosed_model == "xboost")|(chosed_model =="rf"):
gbm = xgb.XGBClassifier(
 n_estimators=2000,
max_depth=4,
min_child_weight=2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective='binary:logistic',
nthread=-1,
scale_pos_weight=1 ).fit(X, y)
 xgb_predictions = gbm.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('/kaggle/working/my_submission_xboost.csv', index=False)
 if(chosed_model == 'knn'):
knn = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=1, n_neighbors=18, p=2,
weights='distance')
 knn.fit(X, y)
 knn_predictions = knn.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': knn_predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!")
<data_type_conversions><EOS> 
if(chosed_model=="rf")|(chosed_model == "xboost"):
df_xboost = pd.read_csv('/kaggle/working/my_submission_xboost.csv')
 df_rf = pd.read_csv('/kaggle/working/my_submission_rf.csv')
 df_ensemble = df_xboost.copy()
k = 'Survived'
df_ensemble[k] = 0.5 * df_xboost[k] + 0.7 * df_rf[k]
df_ensemble[k] = df_ensemble[k].apply(lambda f: 1 if f>=0.5 else 0)
 df_ensemble.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )