Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_pretrained>
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RepeatedKFold
import numpy as np
import re
paragram_vectors = torchtext.vocab.Vectors('.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt')
 for file in os.listdir('./.vector_cache/'):
os.remove(f'./.vector_cache/{file}' )<feature_engineering>
df = pd.DataFrame(pd.read_csv('.. /input/titanic/train.csv'))
df_test = pd.DataFrame(pd.read_csv('.. /input/titanic/test.csv'))
%%time
mean_vectors = torch.zeros(( len(question_field.vocab.stoi), 300))
for word, i in tqdm_notebook(question_field.vocab.stoi.items() , total=len(question_field.vocab.stoi)) :
glove_vector = glove_vectors[word]
paragram_vector = paragram_vectors[word]
vector = torch.stack([glove_vector, paragram_vector])
 vector = torch.sum(vector, dim=0 ).reshape(1, -1)/ 2
mean_vectors[i] = vector
del glove_vectors, paragram_vectors
gc.collect()<train_model>
df.isnull().sum()
val_preds, y_val, _, _, _, message = train(train_dataset, val_dataset, train_dataloader, val_dataloader, x_train_meta, x_val_meta, net, question_field, mean_vectors, question_field.vocab.stoi )<save_to_csv>
print(df.groupby(['Pclass'] ).mean() ['Age'])
 print('
')
 print(df.groupby(['Sex'] ).mean() ['Age'] )
preds =(preds > best_threshold ).astype(int)
 sample_submission['prediction'] = preds
mlc.kaggle.save_sub(sample_submission, 'submission.csv')
 sample_submission.head()<save_to_csv>
def age_nan(df):
for i in df.Sex.unique() :
for j in df.Pclass.unique() :
x = df.loc[(( df.Sex == i)&(df.Pclass == j)) , 'Age'].mean()
df.loc[(( df.Sex == i)&(df.Pclass == j)) , 'Age'] = df.loc[(( df.Sex == i)&(df.Pclass == j)) , 'Age'].fillna(x)
 age_nan(df)
 age_nan(df_test )
x_test['target'] = preds
pseudo_df = pd.concat([x_train, x_val, x_test] ).reset_index().drop('level_0', axis=1)
 pseudo_df.to_csv('x_pseudo.csv' )<create_dataframe>
df['Embarked'] = df['Embarked'].fillna('S')
 df_test['Embarked'] = df_test['Embarked'].fillna('S' )
pseudo_meta = np.concatenate(( x_train_meta, x_val_meta, test_meta))
pseudo_dataset = data.TabularDataset('./x_pseudo.csv', format='CSV', skip_header=True, fields=train_fields)
 pseudo_dataloader = data.BucketIterator(pseudo_dataset, 512, sort_key=lambda x: len(x.question_text), sort_within_batch=True )<train_model>
df['Cabin_NaN'] = df['Cabin'].isnull().astype(int)
 df_test['Cabin_NaN'] = df_test['Cabin'].isnull().astype(int)
 countplot('Cabin_NaN' )
pseudo_val_preds, pseudo_y_val, _, _, _, message = train(pseudo_dataset, val_dataset, pseudo_dataloader, val_dataloader, pseudo_meta, x_val_meta, net, question_field, mean_vectors, question_field.vocab.stoi )<set_options>
df_test.isnull().sum()
del mean_vectors
gc.collect()<prepare_output>
df_test.Fare = df_test.Fare.fillna(-1 )
pseudo_preds =(pseudo_preds > pseudo_best_threshold ).astype(int)
 sample_submission['prediction'] = pseudo_preds
mlc.kaggle.save_sub(sample_submission, 'submission.csv')
 sample_submission.head()<import_modules>
def reg_cross_val(variables):
X = df[variables]
y = df['Survived']
rkfold = RepeatedKFold(n_splits = 2, n_repeats = 10, random_state = 10)
 result = []
for treino, teste in rkfold.split(X):
X_train, X_test = X.iloc[treino], X.iloc[teste]
y_train, y_test = y.iloc[treino], y.iloc[teste]
reg = LogisticRegression(max_iter = 500)
 reg.fit(X_train, y_train)
 result.append(reg.score(X_test, y_test))
return np.mean(result )
import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Input, Embedding, Dense, Dropout, Concatenate, Lambda, Flatten
from keras.layers import GlobalMaxPool1D
from keras.models import Model
import tqdm 
<define_variables>
def is_female(x):
if x == 'female':
return 1
else:
return 0
df['Sex_bin'] = df['Sex'].map(is_female)
 df_test['Sex_bin'] = df_test['Sex'].map(is_female )
MAX_SEQUENCE_LENGTH = 60
MAX_WORDS = 45000
EMBEDDINGS_TRAINED_DIMENSIONS = 100
EMBEDDINGS_LOADED_DIMENSIONS = 300<compute_test_metric>
def embarked_s(x):
if x == 'S':
return 1
else:
return 0
df['Embarked_S'] = df['Embarked'].map(embarked_s)
 df_test['Embarked_S'] = df_test['Embarked'].map(embarked_s)
 def embarked_c(x):
if x == 'C':
return 1
else:
return 0
df['Embarked_C'] = df['Embarked'].map(embarked_c)
 df_test['Embarked_C'] = df_test['Embarked'].map(embarked_c )
def load_embeddings(file):
embeddings = {}
with open(file)as f:
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings = dict(get_coefs(*line.split(" ")) for line in f)
 print('Found %s word vectors.' % len(embeddings))
return embeddings<load_pretrained>
df['Family'] = df.SibSp + df.Parch
df_test['Family'] = df_test.SibSp + df_test.Parch
pretrained_embeddings = load_embeddings(".. /input/embeddings/glove.840B.300d/glove.840B.300d.txt" )<load_from_csv>
text_ticket = ''
for i in df.Ticket:
text_ticket += i
lista = re.findall('[a-zA-Z]+', text_ticket)
 print('Most repeated terms in Tickets:
')
 print(pd.Series(lista ).value_counts().head(10))
df_train = pd.read_csv(".. /input/train.csv")
 df_test = pd.read_csv(".. /input/test.csv" )<define_variables>
df['CA'] = df['Ticket'].str.contains('CA|C.A.' ).astype(int)
 df['SOTON'] = df['Ticket'].str.contains('SOTON|STON' ).astype(int)
 df['PC'] = df['Ticket'].str.contains('PC' ).astype(int)
 df['SC'] = df['Ticket'].str.contains('SC|S.C' ).astype(int)
 df['C'] = df['Ticket'].str.contains('C' ).astype(int)
 df_test['CA'] = df_test['Ticket'].str.contains('CA|C.A.' ).astype(int)
 df_test['SOTON'] = df_test['Ticket'].str.contains('SOTON|STON' ).astype(int)
 df_test['PC'] = df_test['Ticket'].str.contains('PC' ).astype(int)
 df_test['SC'] = df_test['Ticket'].str.contains('SC|S.C' ).astype(int)
 df_test['C'] = df_test['Ticket'].str.contains('C' ).astype(int )
BATCH_SIZE = 512
Q_FRACTION = 1
questions = df_train.sample(frac=Q_FRACTION)
 question_texts = questions["question_text"].values
question_targets = questions["target"].values
test_texts = df_test["question_text"].fillna("_na_" ).values
print(f"Working on {len(questions)} questions" )<train_model>
text_name = ''
for i in df.Name:
text_name += i
lista = re.findall('[a-zA-Z]+', text_name)
 print('Most repeated words in Name column:
')
 print(pd.Series(lista ).value_counts().head(10))
tokenizer = Tokenizer(num_words=MAX_WORDS)
 tokenizer.fit_on_texts(list(df_train["question_text"].values))<categorify>
df['Master'] = df['Name'].str.contains('Master' ).astype(int)
 df['Mr'] = df['Name'].str.contains('Mr' ).astype(int)
 df['Miss'] = df['Name'].str.contains('Miss' ).astype(int)
 df['Mrs'] = df['Name'].str.contains('Mrs' ).astype(int)
 df_test['Master'] = df_test['Name'].str.contains('Master' ).astype(int)
 df_test['Mr'] = df_test['Name'].str.contains('Mr' ).astype(int)
 df_test['Miss'] = df_test['Name'].str.contains('Miss' ).astype(int)
 df_test['Mrs'] = df_test['Name'].str.contains('Mrs' ).astype(int )
<load_pretrained><EOS> 
variables = ['Age', 'Sex_bin', 'Pclass', 'Fare','Family', 'Embarked_S','Embarked_C','Cabin_NaN',\
'CA', 'SOTON', 'PC', 'SC', 'Master', 'Mr', 'Miss', 'C', 'Mrs']
X = df[variables]
y = df['Survived']
reg = LogisticRegression(max_iter = 500)
 reg.fit(X,y)
 resp = reg.predict(df_test[variables])
 submit = pd.Series(resp, index=df_test['PassengerId'], name='Survived')
 submit.to_csv("model.csv", header=True )