Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
import numpy as np
import pandas as pd
model3.compile(
 loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'],
)<train_model>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
history3 = model3.fit(train_pad,Y,
batch_size=64,
epochs=10,
validation_split=0.2
)<choose_model_class>
women = train_data[train_data['Sex'] == 'female']['Survived']
rate_women = sum(women)/len(women)
 print('% of women who survived:', rate_women )
es = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',mode='max',verbose=1,patience=3 )<train_model>
men = train_data[train_data.Sex == 'male']['Survived']
rate_men = sum(men)/len(men)
 print('% of men who survived:', rate_men )
history = model3.fit(train_pad,Y,
batch_size=64,
epochs=30,
validation_split=0.2,
callbacks=[es]
)<predict_on_test>
train_data[['Sex', 'Survived']].groupby(['Sex'] ).mean()
submit = pd.DataFrame(test['id'],columns=['id'])
 predictions = model3.predict(test_pad)
 submit['target_prob'] = predictions
submit.head()<data_type_conversions>
train_data[['Pclass', 'Survived']].groupby(['Pclass'] ).mean()
target = [None]*len(submit)
 for i in range(len(submit)) :
target[i] = np.round(submit['target_prob'][i] ).astype(int)
 submit['target'] = target
submit.head()<save_to_csv>
women_count = 0
women_survived_count = 0
for idx, row in train_data.iterrows() :
if row['Sex'] == 'female':
women_count += 1
if row['Survived'] == 1:
women_survived_count += 1
women_survived_count / women_count
submit = submit.drop('target_prob',axis=1)
 submit.to_csv('real-nlp_lstm.csv',index=False )<load_from_csv>
count1w = 0
count1m = 0
count2w = 0
count2m = 0
count3w = 0
count3m = 0
countm = 0
countw = 0
for idx, row in train_data.iterrows() :
if row['Pclass'] == 1:
if row['Sex'] == 'female':
count1w += 1
else:
count1m += 1
elif row['Pclass'] == 2:
if row['Sex'] == 'female':
count2w += 1
else:
count2m += 1
else:
if row['Sex'] == 'female':
count3w += 1
else:
count3m += 1
if row['Sex'] == 'male':
countm += 1
else:
countw += 1
print("Class 1 Men:",count1m/countm)
 print("Class 1 Women:",count1w/countw)
 print("Class 2 Men:",count2m/countm)
 print("Class 2 Women:",count2w/countw)
 print("Class 3 Men:",count3m/countm)
 print("Class 3 Women:",count3w/countw)
train = pd.read_csv(r'/kaggle/input/nlp-getting-started/train.csv')
 test = pd.read_csv(r'/kaggle/input/nlp-getting-started/test.csv' )<prepare_x_and_y>
predictions = []
for idx, row in test_data.iterrows() :
if row['Sex'] == 'female':
if row['Pclass'] == 1 or row['Pclass'] == 2 or row['Age'] < 25.0:
predictions.append(1)
 else:
predictions.append(0)
 else:
if row['Age'] < 18.0 and row['Pclass'] == 1:
predictions.append(1)
 else:
predictions.append(0)
 print(predictions[:10] )
Y = train['target']
train = train.drop('target',axis=1)
 text_data_train = train['text']
text_data_test = test['text']<count_values>
test_data['Survived'] = predictions
<load_pretrained><EOS> 
test_data[['PassengerId', 'Survived']].to_csv('submission.csv', index=False )