Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
%matplotlib inline
def transform_dataset(ds):
transformed_dataset = ds.copy()
transformed_dataset['Age'].fillna(transformed_dataset['Age'].median() , inplace=True)
 transformed_dataset['Fare'].fillna(transformed_dataset['Fare'].median() , inplace=True)
 transformed_dataset['Sex'] = pd.factorize(transformed_dataset['Sex'])[0]
transformed_dataset=pd.concat([transformed_dataset, pd.get_dummies(transformed_dataset['Embarked'], prefix="Embarked")], axis=1)
 transformed_dataset['Childs'] = transformed_dataset.apply(lambda row: get_childs(row), axis=1)
 transformed_dataset['Parents'] = transformed_dataset.apply(lambda row: get_parents(row), axis=1)
 transformed_dataset = transformed_dataset.drop(['PassengerId', 'Name', 'Ticket', 'Cabin', 'Embarked', 'Parch'], axis=1)
 return transformed_dataset
def get_childs(row):
if(row.Parch>2 or row.Age>=18):
return row.Parch
return 0
def get_parents(row):
if(row.Parch <= 2 and row.Age<18):
return row.Parch
return 0
print("Embedding shape :({},{})".format(len(embedding_dict),
len(embedding_dict['the'])) )<string_transform>
dataset = pd.read_csv('.. /input/titanic/train.csv')
 dataset.info()
MAX_LEN = 50
tokenizer_obj = Tokenizer()
tokenizer_obj.fit_on_texts(corpus)
 sequences = tokenizer_obj.texts_to_sequences(corpus)
 tweet_pad = pad_sequences(sequences, maxlen=MAX_LEN, truncating='post', padding='post' )<count_unique_values>
transformed_dataset = transform_dataset(dataset)
 transformed_dataset.info()
word_index = tokenizer_obj.word_index
print("Number of unique words:", len(word_index))<sort_values>
X = transformed_dataset.drop(['Survived'], axis=1)
 Y = dataset['Survived']
top = sorted(word_index, key=lambda x: word_index[x], reverse=True)[:10]
unknown_index = []
for word in top:
scores =(word, word_index[word])
 unknown_index.append(scores)
 unknown_index<categorify>
X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size = 0.2, random_state = 21 )
num_words = len(word_index)+ 1
embedding_matrix = np.zeros(( num_words, 100))
for word, i in tqdm(word_index.items()):
if i > num_words:
continue
emb_vec = embedding_dict.get(word)
 if emb_vec is not None:
embedding_matrix[i] = emb_vec<split>
param_grid =[ {'n_estimators' : [10, 15, 20, 25, 30, 35, 40], 'max_depth' : [5,10,15, 20]},]
rf = ensemble.RandomForestClassifier(random_state=21, max_features= 3)
 model = GridSearchCV(rf,param_grid, cv = 5 )
train = tweet_pad[:train.shape[0]]
test = tweet_pad[train.shape[0]:]<choose_model_class>
model.fit(X_train,Y_train)
 print('train score = ', model.score(X_train,Y_train), '
test score = ', model.score(X_test,Y_test), '
', model.best_params_ )
model = Sequential()
embedding = Embedding(num_words, 100, embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN, trainable=False)
 model.add(embedding)
 model.add(Bidirectional(LSTM(128,
dropout=0.2,
recurrent_dropout=0.2)) 
)
 model.add(Dense(1, activation='sigmoid'))
optimizer = Adam(learning_rate=1e-4)
 model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 model.summary()<train_model>
test = pd.read_csv('.. /input/titanic/test.csv')
 test.info()
history = model.fit(train, target, batch_size=32, epochs=15,
validation_split=0.2, verbose=1 )<load_from_csv>
passenger_id = test['PassengerId']
sample_sub = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 sample_sub.shape<save_to_csv>
transformed_test = transform_dataset(test)
 transformed_test.info()
y_pred = model.predict(test)
 y_pred = np.round(y_pred ).astype(int ).reshape(3263)
 sub = pd.DataFrame({'id': sample_sub['id'].values.tolist() , 'target': y_pred})
 sub.to_csv("submission.csv", index=False )<import_modules>
Y_predict = model.predict(transformed_test)
 Y_p = pd.DataFrame(Y_predict, columns=['Survived'])
 Y_p
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import re
import string
from tqdm import tqdm
from gensim.parsing.preprocessing import remove_stopwords
from bs4 import BeautifulSoup
from nltk.stem.snowball import SnowballStemmer
from nltk.stem.wordnet import WordNetLemmatizer
from collections import OrderedDict
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer,TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split,RandomizedSearchCV
from sklearn.metrics import classification_report,f1_score
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow import keras
from keras import backend as K
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.optimizers import Adam
import torch
import transformers<load_from_csv>
res = pd.concat([passenger_id, Y_p], axis=1)
 res
train = pd.read_csv(r'/kaggle/input/nlp-getting-started/train.csv')
 test = pd.read_csv(r'/kaggle/input/nlp-getting-started/test.csv' )<categorify>
res.to_csv('res.csv', index=None )