Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.filterwarnings("ignore",category=DeprecationWarning)
 print("
baseline = DummyClassifier(strategy='most_frequent')
 model = RandomForestClassifier()<train_model>
dir_contents = os.listdir('.. /input/')
 print("Dataset contents : {}".format(dir_contents))
print("Size of Training dataset : " + str(round(os.path.getsize('.. /input/train.csv')/(1024*2), 2)) +" KB")
 print("Size of Testing dataset : " + str(round(os.path.getsize('.. /input/test.csv')/(1024*2), 2)) +" KB" )
baseline.fit(x_train, y_train)
 model.fit(x_train, y_train )<predict_on_test>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv')
 passengerIds = test_data['PassengerId']
print("Shape of Train dataset : {}".format(train_data.shape))
print("Shape of Test dataset : {}".format(test_data.shape))
baseline_predictions = baseline.predict(x_test)
 model_predictions = model.predict(x_test )<compute_test_metric>
train_data['Survived'].value_counts(normalize=True)* 100
print(classification_report(y_test, baseline_predictions))<compute_test_metric>
train_data[(train_data['Age'] < 10)&(train_data['SibSp'] == 0)&(train_data['Parch'] == 0)]
print(classification_report(y_test, model_predictions))<load_from_csv>
print("
print(train_data['Sex'].value_counts())
 print("*************************")
 print("Percentage of Males who survived : {}%".format(round(train_data[(train_data['Sex']=='male')& 
(train_data['Survived']==1)].shape[0] * 100
/ train_data.shape[0], 2)))
 print("Percentage of Females who survived : {}%".format(round(train_data[(train_data['Sex']=='female')& 
(train_data['Survived']==1)].shape[0] * 100
/ train_data.shape[0], 2)) )
test_df = pd.read_csv('.. /input/titanic/test.csv' )<choose_model_class>
full_data = [train_data , test_data]
len(full_data )
sub_model = RandomForestClassifier()
sub_model.fit(features_df, target)
 test_features = preprocessor.transform(test_df[train_df.drop('Survived', axis=1 ).columns])
 predictions = sub_model.predict(test_features )<prepare_output>
def getTitleFromName(nameText):
title = str(nameText.split(', ')[1])
 title_search = re.search('([A-Za-z]+)\.', title ).group(1)
 return title_search
for data in full_data:
imp = SimpleImputer(missing_values=np.nan, strategy='median')
 data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data['IsAlone'] = 0
data.loc[data['FamilySize']==1, 'IsAlone'] = 1
data['CabinAllotment'] = 0
data.loc[data['Cabin'].notnull() , 'CabinAllotment'] = 1
data['Embarked'] = data['Embarked'].fillna('S')
 data['Sex'] = data['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 data['Embarked'] = data['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 data['Age'] = imp.fit_transform(data[['Age', 'Sex', 'FamilySize']])[:,0].astype(int)
 data['Fare'] = imp.fit_transform(data[['Fare', 'FamilySize', 'CabinAllotment']])
 data['PerTicket'] = data['Fare'] / data['FamilySize']
data.loc[ data['PerTicket'] <= 7.25, 'PerTicket'] = 0
data.loc[(data['PerTicket'] > 7.25)&(data['PerTicket'] <= 8.3), 'PerTicket'] = 1
data.loc[(data['PerTicket'] > 8.3)&(data['PerTicket'] <= 23.667), 'PerTicket'] = 2
data.loc[ data['PerTicket'] > 23.667, 'PerTicket'] = 3
data['PerTicket'] = data['PerTicket'].astype(int)
 data.loc[(data['Age'] >=0)&(data['Age'] <= 14), 'Age'] = 0
data.loc[(data['Age'] >=15)&(data['Age'] <= 24), 'Age'] = 1
data.loc[(data['Age'] >=25)&(data['Age'] <= 64), 'Age'] = 2
data.loc[data['Age'] >=65, 'Age'] = 3
data['Age'] = data['Age'].astype(int)
 data['Title'] = data['Name'].apply(getTitleFromName)
 data['Title'] = data['Title'].replace('Ms', 'Miss')
 data['Title'] = data['Title'].replace('Mlle', 'Miss')
 data['Title'] = data['Title'].replace('Mme', 'Mrs')
 data['Status'] = "General"
data.loc[data['Title'] == 'Capt','Status'] = 'Military'
data.loc[data['Title'] == 'Col','Status'] = 'Military'
data.loc[data['Title'] == 'Countess','Status'] = 'Political'
data.loc[data['Title'] == 'Don','Status'] = 'Military'
data.loc[data['Title'] == 'Dr','Status'] = 'General'
data.loc[data['Title'] == 'Jonkheer','Status'] = 'Political'
data.loc[data['Title'] == 'Lady','Status'] = 'Political'
data.loc[data['Title'] == 'Major','Status'] = 'Military'
data.loc[data['Title'] == 'Master','Status'] = 'General'
data.loc[data['Title'] == 'Rev','Status'] = 'Political'
data.loc[data['Title'] == 'Sir','Status'] = 'Military'
data['Rank'] = 0
data.loc[data['Title'] == 'Capt', 'Rank'] = 1
data.loc[data['Title'] == 'Col', 'Rank'] = 1
data.loc[data['Title'] == 'Major', 'Rank'] = 2
data.loc[data['Title'] == 'Don', 'Rank'] = 2
data.loc[data['Title'] == 'Sir', 'Rank'] = 0
data.loc[data['Title'] == 'Dr', 'Rank'] = 1
data.loc[data['Title'] == 'Master', 'Rank'] = 0
data.loc[data['Title'] == 'Miss', 'Rank'] = 0
data.loc[data['Title'] == 'Mr', 'Rank'] = 0
data.loc[data['Title'] == 'Mrs', 'Rank'] = 0
data.loc[data['Title'] == 'Countess', 'Rank'] = 2
data.loc[data['Title'] == 'Jonkheer', 'Rank'] = 0
data.loc[data['Title'] == 'Lady', 'Rank'] = 1
data.loc[data['Title'] == 'Rev', 'Rank'] = 1
data['Status'] = data['Status'].map({ 'General': 0, 'Military': 1, 'Political': 2})
 remove_features = ['PassengerId', 'Ticket', 'Cabin', 'SibSp', 'Parch', 'Name', 'Title', 'Fare']
train_data = train_data.drop(remove_features, axis=1)
 test_data = test_data.drop(remove_features, axis=1)
 train_data.head(5 )
sub_df = pd.DataFrame({'PassengerId' : test_df.PassengerId, 'Survived': predictions})
 sub_df.head()<save_to_csv>
y_train = train_data['Survived'].values
X_train = train_data.drop('Survived', axis=1)
 X_test = test_data.values
sub_df.to_csv('titanic_gender_submission.csv', index=False )<load_from_csv>
encoding_clf = OneHotEncoder()
train_data_new = encoding_clf.fit_transform(X_train ).astype('int')
 test_data_new = encoding_clf.transform(X_test ).astype('int' )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
y_train_new = tf.keras.utils.to_categorical(y_train, num_classes=2 )
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<feature_engineering>
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(35, input_shape=(35,), activation=tf.nn.relu, kernel_initializer='he_uniform'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu, kernel_initializer='he_uniform'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(40, activation=tf.nn.relu,kernel_initializer='he_uniform'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.relu,kernel_initializer='he_uniform'))
model.add(tf.keras.layers.Dense(2, activation=tf.nn.softmax))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 relu_model = model.fit(train_data_new, y_train_new, epochs=500, batch_size=7, verbose=1, validation_split=0.20 )
train_data['titles'] = train_data.Name.str.split().str[1]
test_data['titles'] = test_data.Name.str.split().str[1]<filter>
pred = model.predict_classes(test_data_new)
 pred
train_data[train_data.Age >12][:3]<feature_engineering>
DL_Submission = pd.DataFrame({ 'PassengerId': passengerIds,
'Survived': pred })
 DL_Submission.to_csv("DL_Submission.csv", index=False )
train_data['age_fill'] = train_data['Age']
test_data['age_fill'] = test_data['Age']
def fill_age(df):
for i, title in enumerate(df.titles):
if df.loc[i,'age_fill'] != df.loc[i,'age_fill']:
if df.loc[i,'titles'] == 'Master.':
df.loc[i,'age_fill']= round(random.uniform(0,12),1)
 if df.loc[i,'titles'] == 'Miss.':
df.loc[i,'age_fill']= round(random.uniform(0.0,df.Age.max()),1)
 else:
df.loc[i,'age_fill'] = round(random.uniform(13.0,df.Age.max()),1)
 fill_age(train_data)
 fill_age(test_data)
<count_values>
train_data['relatives'] = train_data['SibSp'] + train_data['Parch']
test_data['relatives'] = train_data['SibSp'] + train_data['Parch']
train_data.relatives.value_counts()<categorify>
alpha_range = list([10** i for i in range(-10, 6, 1)] + [2**i for i in range(-5, -1, 1)])
 loss_range = list(['hinge', 'log', 'modified_huber', 'squared_hinge'])
 penalty_range = list(['l1', 'l2', 'elasticnet'])
 parameters = {'loss': loss_range, 'penalty': penalty_range, 'alpha': alpha_range}
model = SGDClassifier()
g_clf = GridSearchCV(model, parameters, cv=10, n_jobs=-1, scoring="accuracy")
 g_clf.fit(g_xtrain, g_ytrain)
 print("Model fitted perfectly.")
 print("Best Score(TRAIN): {}".format(g_clf.best_score_))
print("Best Params : {}".format(g_clf.best_params_))
def combine_fam(df):
for i, r in enumerate(df['relatives']):
if df.loc[i,'relatives'] == 0:
df.loc[i,'relatives_code'] = 0
if df.loc[i,'relatives'] > 0 & df.loc[i,'relatives'] <= 3:
df.loc[i,'relatives_code'] = 1
if df.loc[i,'relatives'] > 3 & df.loc[i,'relatives'] <= 5:
df.loc[i,'relatives_code'] = 2
if df.loc[i,'relatives'] > 5:
df.loc[i,'relatives_code'] = 3
combine_fam(train_data)
 train_data.relatives_code = train_data.relatives_code.astype(int)
 combine_fam(test_data)
 test_data.relatives_code = test_data.relatives_code.astype(int )<feature_engineering>
train_data['Fare_new'] = train_data['Fare']
test_data['Fare_new'] = test_data['Fare']
def find_price(df):
freqs = {}
for ticket in df.Ticket:
freqs[ticket]= freqs.get(ticket, 0)+ 1
combined_ticket = {k:v for k,v in freqs.items() if v > 1}
for i, ticket in enumerate(df['Ticket']):
if ticket in combined_ticket:
df.loc[i,'Fare_new'] = df.loc[i,'Fare']/combined_ticket[ticket]
train_data['Fare_new'] = train_data['Fare'].astype(int)
 test_data['Fare_new'] = test_data['Fare_new'].fillna(test_data.Fare_new.mean() ).astype(int )<data_type_conversions>
<data_type_conversions><EOS>