Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<count_unique_values>
import numpy as np
import pandas as pd
word2index = tokenizer.word_index
print("Number of unique tokens : ",len(word2index))<prepare_x_and_y>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
train_pad = data_padded[:train.shape[0]]
test_pad = data_padded[train.shape[0]:]<categorify>
women = train_data[train_data['Sex'] == 'female']['Survived']
rate_women = sum(women)/len(women)
 print('% of women who survived:', rate_women )
embedding_matrix = np.zeros(( len(word2index)+1,200))
embedding_vec=[]
for word, i in tqdm(word2index.items()):
embedding_vec = word2vec.get(word)
 if embedding_vec is not None:
embedding_matrix[i] = embedding_vec<choose_model_class>
men = train_data[train_data.Sex == 'male']['Survived']
rate_men = sum(men)/len(men)
 print('% of men who survived:', rate_men )
model1 = keras.models.Sequential([
keras.layers.Embedding(len(word2index)+1,200,weights=[embedding_matrix],input_length=100,trainable=False),
keras.layers.LSTM(100,return_sequences=True),
keras.layers.LSTM(200),
keras.layers.Dropout(0.5),
keras.layers.Dense(1,activation='sigmoid')
 ] )<choose_model_class>
train_data[['Sex', 'Survived']].groupby(['Sex'] ).mean()
model1.compile(
 loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'],
)<train_model>
train_data[['Pclass', 'Survived']].groupby(['Pclass'] ).mean()
history1 = model1.fit(train_pad,Y,
batch_size=64,
epochs=10,
validation_split=0.2
)<choose_model_class>
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
model2 = keras.models.Sequential([
keras.layers.Embedding(len(word2index)+1,200,weights=[embedding_matrix],input_length=100,trainable=False),
keras.layers.GRU(100,return_sequences=True),
keras.layers.GRU(200),
keras.layers.Dropout(0.5),
keras.layers.Dense(1,activation='sigmoid')
 ] )<choose_model_class>
predictions = []
for idx, row in test_data.iterrows() :
if(row['Pclass'] == 1 or row['Pclass'] == 2)and row['Sex'] == 'female':
predictions.append(1)
 elif row['Age'] < 13 and row['Pclass'] != 3:
predictions.append(1)
 else:
predictions.append(0 )
model2.compile(
 loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'],
)<train_model>
test_data['Survived'] = predictions
<choose_model_class><EOS> 
test_data[['PassengerId', 'Survived']].to_csv('submission.csv', index=False )