Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
pd.options.mode.chained_assignment = None
train_df['label'] = train_df['label'].map(lambda x: 1 if x else 0)
 dev_df['label'] = dev_df['label'].map(lambda x: 1 if x else 0 )<define_variables>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 train.head()
train_sentences = [list(d)for d in train_df['comments'].to_list() ]
dev_sentences = [list(d)for d in dev_df['comments'].to_list() ]<categorify>
test=pd.read_csv('/kaggle/input/titanic/test.csv')
 pid=test.PassengerId
train_labels = train_df['label'].to_list()
dev_labels = dev_df['label'].to_list()<count_unique_values>
print('Train Set')
 print(train[train.isnull() ])
 print('
Test Set')
 print(test.isnull().sum() )
def make_dictionary(sentences, vocabulary_size=None, initial_words=['<UNK>', '<PAD>', '<SOS>', '<EOS>']):

 counter = Counter()
for words in sentences:
counter.update(words)
 if vocabulary_size is None:
vocabulary_size = len(counter.keys())
 vocab_words = counter.most_common(vocabulary_size)
 for initial_word in initial_words:
vocab_words.insert(0,(initial_word, 0))
word2idx = {word:idx for idx,(word, count)in enumerate(vocab_words)}
idx2word = {idx:word for word, idx in word2idx.items() }
return word2idx, idx2word
def process_sentences(sentences, word2idx, sentence_length=20, padding='<PAD>'):

 sentences_processed = []
for sentence in sentences:
if len(sentence)> sentence_length:
fixed_sentence = sentence[:sentence_length]
else:
fixed_sentence = sentence + [padding]*(sentence_length - len(sentence))
sentence_idx = [word2idx[word] if word in word2idx.keys() else word2idx['<UNK>'] for word in fixed_sentence]
sentences_processed.append(sentence_idx)
 return sentences_processed
def make_mask(sentences, sentence_length):
masks = []
for sentence in sentences:
words_count = len(sentence[:sentence_length])
 sentence_mask = np.concatenate([np.ones(words_count-1), np.ones(1), np.zeros(sentence_length-words_count)])
 masks.append(sentence_mask)
 mask = np.array(masks)
 return mask<categorify>
train['Age']=train['Age'].fillna(train['Age'].median())
 train['Embarked']=train['Embarked'].fillna(train['Embarked'].mode() [0])
 test['Age']=test['Age'].fillna(test['Age'].median())
 test['Fare']=test['Fare'].fillna(test['Fare'].mean() )
word2idx, idx2word = make_dictionary(train_sentences, initial_words=['<UNK>', '<PAD>'] )<string_transform>
print('Train Set')
 print(train.isnull().sum())
 print('
Test Set')
 print(test.isnull().sum() )
SENTENCE_LENGTH = 150
train_sentences_processed = process_sentences(train_sentences, word2idx, sentence_length=SENTENCE_LENGTH)
 dev_sentences_processed = process_sentences(dev_sentences, word2idx, sentence_length=SENTENCE_LENGTH )<categorify>
for data in [train,test]:
data['FamilySize']=data['SibSp']+data['Parch']+1
data['IsAlone']=1
data['IsAlone'].loc[data['FamilySize']>1]=0
data['Title'] = data['Name'].str.split(", ", expand=True)[1].str.split(".", expand=True)[0]
train_mask = make_mask(train_sentences, sentence_length=SENTENCE_LENGTH)
 dev_mask = make_mask(dev_sentences, sentence_length=SENTENCE_LENGTH )<import_modules>
train['Title'].value_counts()
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
from torch.autograd import Variable
from torch.optim import Adam<feature_engineering>
train['Title']=train['Title'].replace('Ms','Miss')
 train['Title']=train['Title'].replace('Mlle','Miss')
 train['Title']=train['Title'].replace('the Countess','Mrs')
 train['Title']=train['Title'].replace('Mme','Mrs')
 test['Title']=test['Title'].replace('Dona','Mrs')
 test['Title']=test['Title'].replace('Ms','Miss')
 names=(train['Title'].value_counts() < 10)
 train['Title']=train['Title'].apply(lambda x: 'Misc' if names.loc[x] == True else x)
 names=(test['Title'].value_counts() < 10)
 test['Title']=test['Title'].apply(lambda x: 'Misc' if names.loc[x] == True else x)
 print('Train Set
',train['Title'].value_counts())
 print('
Test Set
',test['Title'].value_counts() )
class DatasetLoader(Dataset):
def __init__(self, sentences_processed, labels):
assert len(sentences_processed)== len(labels)
 self.sentences_processed = sentences_processed
self.labels = torch.LongTensor(labels)
 def __getitem__(self, index):
return torch.LongTensor(self.sentences_processed[index]), self.labels[index]
def __len__(self):
return len(self.sentences_processed )<create_dataframe>
columns=['PassengerId','Cabin','Ticket','Name']
train=train.drop(columns,axis=1)
 test=test.drop(columns,axis=1 )
train_dataset = DatasetLoader(train_sentences_processed, train_labels)
 dev_dataset = DatasetLoader(dev_sentences_processed, dev_labels )<load_pretrained>
label=LabelEncoder()
for data in [train,test]:
data['Sex']=label.fit_transform(data['Sex'])
 data['Embarked']=label.fit_transform(data['Embarked'])
 data['Title']=label.fit_transform(data['Title'])
 data['Age']=data['Age'].astype('int64')
 train.head()
train_dataloader = DataLoader(train_dataset, batch_size=128)
 dev_dataloader = DataLoader(dev_dataset, batch_size=256 )<import_modules>
target=['Survived']
selected=['Sex','Pclass','Embarked','Title','SibSp','Parch','FamilySize','IsAlone']
for x in selected:
print('Survival Percentage By',x)
 print(train[[x, target[0]]].groupby(x,as_index=False ).mean() ,'
')
import torch
import torch.nn as nn
import torch.nn.functional as F<choose_model_class>
target=train['Survived']
train.drop(['Survived'],axis=1, inplace=True )
class CharCNN(nn.Module):
def __init__(self, vocab_size, embedding_size, output_size, kernel_num, kernel_sizes):
super().__init__()
self.embedding = nn.Embedding(
 vocab_size, embedding_size, padding_idx=0 
)
 self.convs = nn.ModuleList([
nn.Conv1d(embedding_size, kernel_num, kernel_size=kernel_size)
 for kernel_size in kernel_sizes
])
 self.maxpools = nn.ModuleList([
nn.MaxPool1d(kernel_size)
 for kernel_size in kernel_sizes
])
 self.linear = nn.Linear(1140, output_size)
 self.softmax = nn.LogSoftmax(dim=1)
 self.sigmoid = nn.Sigmoid()
self.dropout = nn.Dropout(p=0.5)
 def forward(self, x):
batch_size = x.size(0)
 embedded = self.embedding(x)
 embedded = embedded.transpose(1,2)
 pools = []
for conv, maxpool in zip(self.convs, self.maxpools):
feature_map = conv(embedded)
 pooled = maxpool(feature_map)
 pools.append(pooled)
 conv_concat = torch.cat(pools, dim=-1 ).view(batch_size, -1)
 conv_concat = self.dropout(conv_concat)
 logits = self.linear(conv_concat)
 return self.softmax(logits )<choose_model_class>
X_train, X_val, y_train, y_val = train_test_split(train, target, test_size=0.2, random_state=1, stratify=target)
 print('Mean Absolute Errors:')
 model = RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,
criterion='gini', max_depth=4, max_features='auto',
max_leaf_nodes=5, max_samples=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=15,
min_weight_fraction_leaf=0.0, n_estimators=350,
n_jobs=None, oob_score=True, random_state=1, verbose=0,
warm_start=False)
 model.fit(X_train, y_train)
 predict = model.predict(X_val)
 print('Random Forrest: ' + str(mean_absolute_error(predict, y_val)))
 model = XGBRegressor(learning_rate=0.01, n_estimators=3460, max_depth=3, min_child_weight=0,
gamma=0, subsample=0.7,colsample_bytree=0.7,objective='reg:squarederror',
nthread=-1,scale_pos_weight=1, seed=27, reg_alpha=0.00006)
 model.fit(X_train, y_train)
 predict = model.predict(X_val)
 print('XGBoost: ' + str(mean_absolute_error(predict, y_val)))
 model = LassoCV(max_iter=1e7, random_state=14, cv=10)
 model.fit(X_train, y_train)
 predict = model.predict(X_val)
 print('Lasso: ' + str(mean_absolute_error(predict, y_val)))
 model = GradientBoostingRegressor(n_estimators=300, learning_rate=0.05, max_depth=4, random_state=5)
 model.fit(X_train, y_train)
 predict = model.predict(X_val)
 print('GradientBoosting: ' + str(mean_absolute_error(predict, y_val)) )
model = CharCNN(
 vocab_size=len(word2idx),
embedding_size=300,
output_size=2,
kernel_num=10,
kernel_sizes=[3,4,5]
)<set_options>
model=RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,
criterion='gini', max_depth=4, max_features='auto',
max_leaf_nodes=5, max_samples=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=15,
min_weight_fraction_leaf=0.0, n_estimators=350,
n_jobs=None, oob_score=True, random_state=1, verbose=0,
warm_start=False)
 model.fit(X_train, y_train)
 predict = model.predict(X_val)
 print('Random Forest MAE: ' + str(mean_absolute_error(predict, y_val)))
 print("Out of Bag Score: %.4f" % model.oob_score_)
 y_pred_train = model.predict(X_train)
 y_pred_test = model.predict(X_val)
 print("Training accuracy: ", accuracy_score(y_train, y_pred_train))
print("Testing accuracy: ", accuracy_score(y_val, y_pred_test))
print("
Confusion Matrix
")
 print('[[True Positive False Positive]
[False Negative True Negative]]
')
 print(confusion_matrix(y_val, y_pred_test))
fpr, tpr, _ = roc_curve(y_val, y_pred_test)
 roc_auc = auc(fpr, tpr)
 print("
ROC AUC on evaluation set",roc_auc)
 plt.figure()
plt.plot(fpr, tpr, label='ROC curve(area = %0.2f)' % roc_auc)
 plt.plot([0, 1], [0, 1], 'k--')
 plt.xlim([0.0, 1.0])
 plt.ylim([0.0, 1.05])
 plt.xlabel('False Positive Rate')
 plt.ylabel('True Positive Rate')
 plt.title('ROC Curve')
 plt.legend(loc="lower right")
 plt.show()
if torch.cuda.is_available() :
model.to('cuda' )<choose_model_class>
prediction=model.predict(test)
 prediction
<choose_model_class><EOS> 
output = pd.DataFrame({'PassengerId': pid, 'Survived': prediction})
 output.to_csv('my_submission.csv', index=False)
 print("Submission successfully saved!" )