Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
pd.set_option("display.precision", 4)
 sns.set(style="darkgrid")
 warnings.filterwarnings('ignore')
 TRAIN_LEN = 891
RNG_SEED = 343
COLS_TO_DROP = []
CHILD_AGE_END = 18
DECKS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'N']
DEFAULT_SURVIVAL = 0.5
def get_deck_class_count(df, T_deck):

 deck_count = {'A': {}, 'B': {}, 'C': {}, 'D': {}, 'E': {}, 'F': {},
'G': {}, 'N': {}, 'T': {}}
if not T_deck:
deck_count.pop('T', None)
 deck_percent = {}
decks = df.transpose().columns.levels[0]
for deck in decks:
for pclass in range(1, 4):
try:
count = int(df.loc[deck, pclass])
 deck_count[deck][pclass] = count
except KeyError:
deck_count[deck][pclass] = 0
deck_percent[deck] = [(count / sum(deck_count[deck].values()))
* 100 for count in deck_count[deck].values() ]
return deck_count, deck_percent
def get_surv_prop(deck, pclass):

 return deck_class_surv_prop[deck][pclass]
def get_corr(df):

 corr = df.corr().abs().unstack().sort_values(kind="quicksort",
ascending=False ).reset_index()
cols_map = {"level_0": "Feature 1",
"level_1": "Feature 2",
0: 'Correlation Coefficient'}
corr.drop(corr.iloc[1::2].index, inplace=True)
 corr.rename(columns=cols_map, inplace=True)
 return corr.drop(corr[corr['Correlation Coefficient'] == 1.0].index)
 def combine_df(df1, df2):

 return pd.concat([df1, df2], sort=True ).reset_index(drop=True)
 def divide_df(df, first_len):

 return df.loc[:first_len - 1], df.loc[first_len:].drop(['Survived'], axis=1)
 def drop_cols(cols):

 for col in cols:
train.drop([col], inplace=True, axis=1)
 test.drop([col], inplace=True, axis=1)
 return
def display_class_dist(percentages, y_label, title):

 df_percent = pd.DataFrame(percentages ).transpose()
deck_names = percentages.keys()
bar_count = np.arange(len(deck_names))
bar_width = 0.75
plt.figure(figsize=(16, 8))
plt.bar(bar_count, df_percent[0],
color='red', edgecolor='black', width=bar_width,
label='Passenger Class 1')
 plt.bar(bar_count, df_percent[1], bottom=df_percent[0],
color='lime', edgecolor='black', width=bar_width,
label='Passenger Class 2')
 plt.bar(bar_count, df_percent[2], bottom=df_percent[0] + df_percent[1],
color='blue', edgecolor='black', width=bar_width,
label='Passenger Class 3')
 plt.xlabel('Deck', size=25)
 plt.ylabel(y_label, size=25)
 plt.xticks(bar_count, deck_names)
 plt.tick_params(axis='x', labelsize=15)
 plt.tick_params(axis='y', labelsize=15)
 plt.legend(loc='upper right', prop={'size': 15})
 plt.title(title, size=30, y=1)
 plt.show()
return
def group_survivors(df, group, new_feature_name):

 df[new_feature_name] = DEFAULT_SURVIVAL
for _, group_df in df.groupby(group):
if len(group_df)> 1:
surv_max = group_df['Survived'].max()
surv_min = group_df['Survived'].min()
if isnan(surv_max)and isnan(surv_min):
continue
for _, row in group_df.iterrows() :
passId = row['PassengerId']
if(surv_max == 1.0):
df.loc[df['PassengerId'] == passId, new_feature_name] = 1.0
elif(surv_min==0.0):
df.loc[df['PassengerId'] == passId, new_feature_name] = 0.0
return df
y = train['type']
y_mind = y.apply(lambda x: 0 if x[0] == 'I' else 1)
 y_energy = y.apply(lambda x: 0 if x[1] == 'S' else 1)
 y_nature = y.apply(lambda x: 0 if x[2] == 'F' else 1)
 y_tactics = y.apply(lambda x: 0 if x[3] == 'P' else 1 )<import_modules>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 combined = combine_df(train, test )
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from mlxtend.classifier import StackingCVClassifier
from sklearn.model_selection import cross_val_score, GridSearchCV<choose_model_class>
train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
LogR_model = LogisticRegression()
RFC_model = RandomForestClassifier()
SVC_model = SVC(probability = True)
 XGB_model = XGBClassifier()
category = [(y_mind, "Mind"),(y_energy, "Energy"),(y_nature, "Nature"),(y_tactics, "Tactics")]
models = [(LogR_model, "LOG"),(RFC_model, "RFC"),(SVC_model, 'SVC'),(XGB_model, 'XGB')]<choose_model_class>
adults = train[train['Age'] >= CHILD_AGE_END]
children = train[train['Age'] < CHILD_AGE_END]
print('Proportion of passengers <{} who survived: {:.4f}'.format(CHILD_AGE_END, children['Survived'].mean()))
print('Proportion of passengers >={} who survived: {:.4f}'.format(CHILD_AGE_END, adults['Survived'].mean()))
LogR_model = LogisticRegression(C = 10 )<choose_model_class>
train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
SVC_model = SVC(gamma = 5, probability = True )<choose_model_class>
combined[combined['Fare'].isnull() ]
XGB_model = XGBClassifier(n_estimators=150, learning_rate=0.15,gamma= 0.5 )<choose_model_class>
combined['Fare'] = combined['Fare'].fillna(med_fare[3][0][0] )
Meta_model = XGBClassifier()
Stack_model = StackingCVClassifier(classifiers = [SVC_model, XGB_model, LogR_model],
meta_classifier = Meta_model )<save_to_csv>
combined[combined['Embarked'].isnull() ]
models = [(LogR_model, "log_submission.csv"),(SVC_model, 'svc_submission.csv'),(XGB_model, 'xgb_submission.csv')]
for model, file_name in models:
submission_file(model, file_name )<load_from_csv>
combined['Embarked'] = combined['Embarked'].fillna('S' )
train_data=pd.read_csv("/kaggle/input/predice-el-futuro/train_csv.csv")
 display(train_data.head())
 test_data=pd.read_csv("/kaggle/input/predice-el-futuro/test_csv.csv")
 display(test_data.head())
<feature_engineering>
train, test = divide_df(combined, TRAIN_LEN)
 corr_train = get_corr(train)
 corr_train[(corr_train['Feature 1'] == 'Age')| 
(corr_train['Feature 2'] == 'Age')]
train_data['time'] = pd.to_datetime(train_data['time'],format='%Y-%m-%d %H:%M:%S')
 train_data['year']=train_data['time'].dt.year
train_data['month']=train_data['time'].dt.month
train_data['day']=train_data['time'].dt.day
train_data['dayofweek_num']=train_data['time'].dt.dayofweek
train_data['dayofweek_name']=train_data['time'].dt.weekday_name
train_data['Hour'] = train_data['time'].dt.hour
train_data['minute'] = train_data['time'].dt.minute
train_data['second'] = train_data['time'].dt.second
train_data.head()<feature_engineering>
age_pclass_sex = train.groupby(['Pclass', 'Sex'] ).median() ['Age']
print('Median ages for the following groups(training data):')
 for pclass in range(1, train['Pclass'].nunique() + 1):
for sex in ['female', 'male']:
print('Pclass {} {}s: {}'.format(pclass, sex, age_pclass_sex[pclass][sex]))
print('All passengers: {}'.format(train['Age'].median()))
train_data['lag_1'] = train_data['feature'].shift(1)
 train_data = train_data[['time', 'lag_1', 'feature']]
train_data.head()<feature_engineering>
combined['Age'] = combined.groupby(['Pclass', 'Sex'])['Age'].apply(lambda x: x.fillna(x.median()))
train_data['lag_1'] = train_data['feature'].shift(1)
 train_data['lag_2'] = train_data['feature'].shift(2)
 train_data['lag_3'] = train_data['feature'].shift(3)
 train_data['lag_4'] = train_data['feature'].shift(4)
 train_data['lag_5'] = train_data['feature'].shift(5)
 train_data['lag_6'] = train_data['feature'].shift(6)
 train_data['lag_7'] = train_data['feature'].shift(7)
 data = train_data[['time', 'lag_1', 'lag_2', 'lag_3', 'lag_4', 'lag_5', 'lag_6', 'lag_7', 'feature']]
data.head(10 )<train_model>
combined['Deck'] = combined['Cabin'].apply(lambda s: s[0] if pd.notnull(s)else 'N')
 train, test = divide_df(combined, TRAIN_LEN)
 deck_class_count = train.groupby(['Deck', 'Pclass'] ).count().rename(columns={'Name': 'Count'})
 deck_class_count = deck_class_count[['Count']]
print('Passenger counts for each Deck, Pclass combination(training data)where N deck denotes null values:',
deck_class_count, sep='
' )
model = Prophet()
model.fit(train_data.reset_index() \ 
.rename(columns={'time':'ds',
'feature':'y'}))<predict_on_test>
combined = combine_df(train, test)
 i = combined[combined['Deck'] == 'T'].index
combined.loc[i, 'Deck'] = 'A'
deck_class_count.pop('T', None)
 deck_class_count['A'][1] += 1
COLS_TO_DROP.extend(['Cabin'] )
test_fcst = model.predict(df=test_data.reset_index() \ 
.rename(columns={'time':'ds'}))<split>
train, test = divide_df(combined, TRAIN_LEN)
 corr_train = get_corr(train)
 high_corr_train = corr_train['Correlation Coefficient'] > 0.1
print('Training set correlations(coefficient > 0.1):')
 display(corr_train[high_corr_train])
 corr_test = get_corr(test)
 high_corr_test = corr_test['Correlation Coefficient'] > 0.1
print('Testing set correlations(coefficient > 0.1):')
 display(corr_test[high_corr_test] )
data=test_fcst["yhat_upper"]
data.loc[1:40]<predict_on_test>
deck_class_surv_count = train[['Deck', 'Pclass', 'Survived']]
deck_class_surv_count = deck_class_surv_count.groupby(['Deck', 'Pclass'] ).sum()
deck_class_surv_count, _ = get_deck_class_count(deck_class_surv_count, False)
 deck_class_surv_prop = deck_class_surv_count.copy()
for deck in DECKS:
for pclass in deck_class_count[deck].keys() :
try:
deck_class_surv_prop[deck][pclass] = round(( deck_class_surv_count[deck][pclass] /
deck_class_count[deck][pclass]), 2)
 except ZeroDivisionError:
pass
print('Decimal percent of passengers survived for each 'Deck', 'Pclass' combination(training set)':')
 display(deck_class_surv_prop )
train_fcst = model.predict(df=train_data.reset_index() \ 
.rename(columns={'time':'ds'}))<data_type_conversions>
combined = combine_df(train, test)
 combined['DeckPclassSurvProp'] = combined.apply(lambda x: get_surv_prop(x['Deck'], x['Pclass']),axis=1 )
def parserToTimeDatatype(time):
return datetime.strptime(time,"%Y-%m-%d %H:%M:%S" )<load_from_csv>
combined['FamilySize'] = combined['SibSp'] + combined['Parch'] + 1
COLS_TO_DROP.extend(['SibSp', 'Parch'])
 train, test = divide_df(combined, TRAIN_LEN )
df= pd.read_csv('/kaggle/input/predice-el-futuro/train_csv.csv',
parse_dates = [1],
index_col = 1,
date_parser=parserToTimeDatatype)
<filter>
combined = combine_df(train, test)
 combined['Title'] = combined['Name'].apply(lambda name: name.split(',')[1].split('.')[0].strip())
 print('Count of passenger titles aboard the Titanic:')
 combined['Title'].value_counts()
df= df.iloc[:,[1]]
df<correct_missing_values>
normalized_titles = {
"Capt": "Officer",
"Col": "Officer",
"Don": "Royalty",
"Dona": "Royalty",
"Dr": "Officer",
"Jonkheer": "Royalty",
"Lady" : "Royalty",
"Major": "Officer",
"Master" : "Master",
"Miss" : "Miss",
"Mlle": "Miss",
"Mme": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Ms": "Mrs",
"Rev": "Officer",
"Sir" : "Royalty",
"the Countess": "Royalty"}
combined['Title'] = combined['Title'].map(normalized_titles)
 print('Count of updated passenger titles aboard the Titanic:')
 combined['Title'].value_counts()
modifiedDF = df.diff(periods=1)
 modifiedDF.dropna(inplace=True)
 modifiedDF.head()<train_model>
print('Decimal percentages for survival based on passenger title:')
 combined[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean() \ 
.sort_values(by='Survived', kind="quicksort", ascending=False )
model = Prophet()
model.fit(modifiedDF.reset_index() \ 
.rename(columns={'time':'ds',
'feature':'y'}))<drop_column>
combined['Surname'] = combined['Name'].apply(lambda x: str.split(x, ",")[0] )
train_data=train_data.drop(index = 79 )<predict_on_test>
display(combined.loc[combined['Surname'] == 'Davies'] )
test_fcst = model.predict(df=modifiedDF.reset_index() \ 
.rename(columns={'time':'ds'}))<drop_column>
combined.loc[combined['FamilySize'] == 11]
del test_data['time']<compute_test_metric>
combined = group_survivors(combined, ['Surname', 'Fare', 'FamilySize'], 'FamilySurvival')
 print('Count of passengers with family survival data: ',
combined.loc[combined['FamilySurvival']!=0.5].shape[0] )
mean_squared_error(y_true=modifiedDF['feature'],
y_pred=test_fcst['yhat'] )<compute_test_metric>
combined = group_survivors(combined, ['Ticket'], 'GroupSurvival')
 print('Count of passenger with group survival data: ',
combined[combined['GroupSurvival']!=0.5].shape[0] )
mean_absolute_error(y_true=modifiedDF['feature'],
y_pred=test_fcst['yhat'] )<compute_test_metric>
ord_cols = ['Deck', 'Title', 'FamilySurvival', 'GroupSurvival']
deck_cat = ['N', 'G', 'F', 'E', 'D', 'C', 'B', 'A']
title_cat = ['Mr', 'Officer', 'Master', 'Royalty', 'Miss', 'Mrs']
family_cat = group_cat = [0.0, 0.5, 1.0]
ord_enc = OrdinalEncoder(categories=[deck_cat, title_cat, family_cat, group_cat])
 combined[ord_cols] = ord_enc.fit_transform(combined[ord_cols] )
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
 return np.mean(np.abs(( y_true - y_pred)/ y_true)) * 100<compute_test_metric>
oh_cols = ['Embarked', 'FamilySizeCat', 'Sex']
combined = pd.get_dummies(data=combined, columns=oh_cols)
 train, test = divide_df(combined, TRAIN_LEN )
mean_absolute_percentage_error(y_true=modifiedDF['feature'],
y_pred=test_fcst['yhat'] )<compute_test_metric>
passengerid_test = test.PassengerId
COLS_TO_DROP.extend(['PassengerId', 'Name', 'Ticket', 'Surname'])
 drop_cols(COLS_TO_DROP)
 display(train.head() )
y_true = np.random.randn(100)
 y_pred = y_true * 3.5
print(mean_absolute_percentage_error(y_true, y_pred))<compute_test_metric>
y = train[['Survived']].copy()
train.drop(columns='Survived', inplace=True)
 X_train, X_valid, y_train, y_valid = train_test_split(train, y, train_size=0.8, random_state=RNG_SEED)
 X_test = test.copy()
print('rmse:', np.sqrt(mean_squared_error(y_true, y_pred)) )<save_to_csv>
WANT_HYPERPARAMETERS = False
if WANT_HYPERPARAMETERS:
params = dict(max_depth = [n for n in range(3, 9)],
min_samples_split = [n for n in range(2, 4)],
min_samples_leaf = [n for n in range(2, 4)],
n_estimators = [20, 40, 60, 80],)
 model = GridSearchCV(RandomForestClassifier(random_state=RNG_SEED),
params, cv=5, scoring='accuracy')
 model.fit(X_train, y_train)
 print(f'Best parameters {model_random_forest.best_params_}')
 print(f'Mean cross-validated accuracy of the best parameters: {model.best_score_:.4f}')
 else:
model = RandomForestClassifier(n_estimators=50, max_depth=5,
min_samples_leaf=2, min_samples_split=2,
random_state=RNG_SEED)
 model.fit(X_train, y_train )
test_data["feature"]=data
test_data.to_csv("submission.csv", index=None )<load_from_csv>
predictions_valid = model.predict(X_valid)
 score = mean_absolute_error(y_valid, predictions_valid)
 print('MAE for validation set prediction:', round(score, 4))
folds = 5
scores = -1 * cross_val_score(model, X_valid, y_valid, cv=folds, scoring='neg_mean_absolute_error')
 print('Average MAE score(across experiments using {} folds):'.format(folds))
print(round(scores.mean() , 4))
<load_from_csv><EOS> 
predictions_test = model.predict(X_test)
 predictions_test = predictions_test.astype(int)
 output = pd.DataFrame({'PassengerId': passengerid_test,
'Survived': predictions_test})
 output.to_csv('submission.csv', index=False )