Kaggle competition: Titanic - Machine Learning from Disaster
def remove_punctuation(x):
return x.translate(str.maketrans('', '', string.punctuation))
def remove_stopwords(x):
return ' '.join([i for i in x.split() if i not in wordcloud.STOPWORDS])
 def remove_less_than(x):
return ' '.join([i for i in x.split() if len(i)> 3])
 def remove_non_alphabet(x):
return ' '.join([i for i in x.split() if i.isalpha() ])
 def strip_all_entities(x):
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x ).split() )<drop_column>
!pip install fastai2
strip_all_entities('@shawn Titanic
Times: Telegraph.co.ukTitanic tragedy could have been preve...http://bet.ly/tuN2wx' )<string_transform>
!pip install fastcore==0.1.35
!pip install autocorrect
def spell_check(x):
spell = Speller(lang='en')
 return " ".join([spell(i)for i in x.split() ])
 mispelled = 'Pleaze spelcheck this sentince'
spell_check(mispelled )<feature_engineering>
fastcore.__version__
PROCESS_TWEETS = False
if PROCESS_TWEETS:
total['text'] = total['text'].apply(lambda x: x.lower())
 total['text'] = total['text'].apply(lambda x: re.sub(r'https?://\S+|www\.\S+', '', x, flags = re.MULTILINE))
total['text'] = total['text'].apply(remove_punctuation)
 total['text'] = total['text'].apply(remove_stopwords)
 total['text'] = total['text'].apply(remove_less_than)
 total['text'] = total['text'].apply(remove_non_alphabet)
 total['text'] = total['text'].apply(spell_check )<define_variables>
fastai2.__version__
contractions = {
"ain't": "am not / are not / is not / has not / have not",
"aren't": "are not / am not",
"can't": "cannot",
"can't've": "cannot have",
"'cause": "because",
"could've": "could have",
"couldn't": "could not",
"couldn't've": "could not have",
"didn't": "did not",
"doesn't": "does not",
"don't": "do not",
"hadn't": "had not",
"hadn't've": "had not have",
"hasn't": "has not",
"haven't": "have not",
"he'd": "he had / he would",
"he'd've": "he would have",
"he'll": "he shall / he will",
"he'll've": "he shall have / he will have",
"he's": "he has / he is",
"how'd": "how did",
"how'd'y": "how do you",
"how'll": "how will",
"how's": "how has / how is / how does",
"I'd": "I had / I would",
"I'd've": "I would have",
"I'll": "I shall / I will",
"I'll've": "I shall have / I will have",
"I'm": "I am",
"I've": "I have",
"isn't": "is not",
"it'd": "it had / it would",
"it'd've": "it would have",
"it'll": "it shall / it will",
"it'll've": "it shall have / it will have",
"it's": "it has / it is",
"let's": "let us",
"ma'am": "madam",
"mayn't": "may not",
"might've": "might have",
"mightn't": "might not",
"mightn't've": "might not have",
"must've": "must have",
"mustn't": "must not",
"mustn't've": "must not have",
"needn't": "need not",
"needn't've": "need not have",
"o'clock": "of the clock",
"oughtn't": "ought not",
"oughtn't've": "ought not have",
"shan't": "shall not",
"sha'n't": "shall not",
"shan't've": "shall not have",
"she'd": "she had / she would",
"she'd've": "she would have",
"she'll": "she shall / she will",
"she'll've": "she shall have / she will have",
"she's": "she has / she is",
"should've": "should have",
"shouldn't": "should not",
"shouldn't've": "should not have",
"so've": "so have",
"so's": "so as / so is",
"that'd": "that would / that had",
"that'd've": "that would have",
"that's": "that has / that is",
"there'd": "there had / there would",
"there'd've": "there would have",
"there's": "there has / there is",
"they'd": "they had / they would",
"they'd've": "they would have",
"they'll": "they shall / they will",
"they'll've": "they shall have / they will have",
"they're": "they are",
"they've": "they have",
"to've": "to have",
"wasn't": "was not",
"we'd": "we had / we would",
"we'd've": "we would have",
"we'll": "we will",
"we'll've": "we will have",
"we're": "we are",
"we've": "we have",
"weren't": "were not",
"what'll": "what shall / what will",
"what'll've": "what shall have / what will have",
"what're": "what are",
"what's": "what has / what is",
"what've": "what have",
"when's": "when has / when is",
"when've": "when have",
"where'd": "where did",
"where's": "where has / where is",
"where've": "where have",
"who'll": "who shall / who will",
"who'll've": "who shall have / who will have",
"who's": "who has / who is",
"who've": "who have",
"why's": "why has / why is",
"why've": "why have",
"will've": "will have",
"won't": "will not",
"won't've": "will not have",
"would've": "would have",
"wouldn't": "would not",
"wouldn't've": "would not have",
"y'all": "you all",
"y'all'd": "you all would",
"y'all'd've": "you all would have",
"y'all're": "you all are",
"y'all've": "you all have",
"you'd": "you had / you would",
"you'd've": "you would have",
"you'll": "you shall / you will",
"you'll've": "you shall have / you will have",
"you're": "you are",
"you've": "you have"
}
contractions_re = re.compile('(%s)' % '|'.join(contractions.keys()))
def expand_contractions(s, contractions = contractions):
def replace(match):
return contractions[match.group(0)]
return contractions_re.sub(replace, s)
 expand_contractions("can't stop won't stop" )<feature_engineering>
from fastai2.tabular.all import *
total['text'] = total['text'].apply(expand_contractions )<categorify>
df_test= pd.read_csv('/kaggle/input/titanic-extended/test.csv')
 df_train= pd.read_csv('.. /input/titanic-extended/train.csv')
 df_train.head()
def clean(tweet):
tweet = re.sub(r"tnwx", "Tennessee Weather", tweet)
 tweet = re.sub(r"azwx", "Arizona Weather", tweet)
 tweet = re.sub(r"alwx", "Alabama Weather", tweet)
 tweet = re.sub(r"wordpressdotcom", "wordpress", tweet)
 tweet = re.sub(r"gawx", "Georgia Weather", tweet)
 tweet = re.sub(r"scwx", "South Carolina Weather", tweet)
 tweet = re.sub(r"cawx", "California Weather", tweet)
 tweet = re.sub(r"usNWSgov", "United States National Weather Service", tweet)
 tweet = re.sub(r"MH370", "Malaysia Airlines Flight 370", tweet)
 tweet = re.sub(r"okwx", "Oklahoma City Weather", tweet)
 tweet = re.sub(r"arwx", "Arkansas Weather", tweet)
 tweet = re.sub(r"lmao", "laughing my ass off", tweet)
 tweet = re.sub(r"amirite", "am I right", tweet)
 tweet = re.sub(r"w/e", "whatever", tweet)
 tweet = re.sub(r"w/", "with", tweet)
 tweet = re.sub(r"USAgov", "USA government", tweet)
 tweet = re.sub(r"recentlu", "recently", tweet)
 tweet = re.sub(r"Ph0tos", "Photos", tweet)
 tweet = re.sub(r"exp0sed", "exposed", tweet)
 tweet = re.sub(r"<3", "love", tweet)
 tweet = re.sub(r"amageddon", "armageddon", tweet)
 tweet = re.sub(r"Trfc", "Traffic", tweet)
 tweet = re.sub(r"WindStorm", "Wind Storm", tweet)
 tweet = re.sub(r"16yr", "16 year", tweet)
 tweet = re.sub(r"TRAUMATISED", "traumatized", tweet)
 tweet = re.sub(r"IranDeal", "Iran Deal", tweet)
 tweet = re.sub(r"ArianaGrande", "Ariana Grande", tweet)
 tweet = re.sub(r"camilacabello97", "camila cabello", tweet)
 tweet = re.sub(r"RondaRousey", "Ronda Rousey", tweet)
 tweet = re.sub(r"MTVHottest", "MTV Hottest", tweet)
 tweet = re.sub(r"TrapMusic", "Trap Music", tweet)
 tweet = re.sub(r"ProphetMuhammad", "Prophet Muhammad", tweet)
 tweet = re.sub(r"PantherAttack", "Panther Attack", tweet)
 tweet = re.sub(r"StrategicPatience", "Strategic Patience", tweet)
 tweet = re.sub(r"socialnews", "social news", tweet)
 tweet = re.sub(r"IDPs:", "Internally Displaced People :", tweet)
 tweet = re.sub(r"ArtistsUnited", "Artists United", tweet)
 tweet = re.sub(r"ClaytonBryant", "Clayton Bryant", tweet)
 tweet = re.sub(r"jimmyfallon", "jimmy fallon", tweet)
 tweet = re.sub(r"justinbieber", "justin bieber", tweet)
 tweet = re.sub(r"Time2015", "Time 2015", tweet)
 tweet = re.sub(r"djicemoon", "dj icemoon", tweet)
 tweet = re.sub(r"LivingSafely", "Living Safely", tweet)
 tweet = re.sub(r"FIFA16", "Fifa 2016", tweet)
 tweet = re.sub(r"thisiswhywecanthavenicethings", "this is why we cannot have nice things", tweet)
 tweet = re.sub(r"bbcnews", "bbc news", tweet)
 tweet = re.sub(r"UndergroundRailraod", "Underground Railraod", tweet)
 tweet = re.sub(r"c4news", "c4 news", tweet)
 tweet = re.sub(r"MUDSLIDE", "mudslide", tweet)
 tweet = re.sub(r"NoSurrender", "No Surrender", tweet)
 tweet = re.sub(r"NotExplained", "Not Explained", tweet)
 tweet = re.sub(r"greatbritishbakeoff", "great british bake off", tweet)
 tweet = re.sub(r"LondonFire", "London Fire", tweet)
 tweet = re.sub(r"KOTAWeather", "KOTA Weather", tweet)
 tweet = re.sub(r"LuchaUnderground", "Lucha Underground", tweet)
 tweet = re.sub(r"KOIN6News", "KOIN 6 News", tweet)
 tweet = re.sub(r"LiveOnK2", "Live On K2", tweet)
 tweet = re.sub(r"9NewsGoldCoast", "9 News Gold Coast", tweet)
 tweet = re.sub(r"nikeplus", "nike plus", tweet)
 tweet = re.sub(r"david_cameron", "David Cameron", tweet)
 tweet = re.sub(r"peterjukes", "Peter Jukes", tweet)
 tweet = re.sub(r"MikeParrActor", "Michael Parr", tweet)
 tweet = re.sub(r"4PlayThursdays", "Foreplay Thursdays", tweet)
 tweet = re.sub(r"TGF2015", "Tontitown Grape Festival", tweet)
 tweet = re.sub(r"realmandyrain", "Mandy Rain", tweet)
 tweet = re.sub(r"GraysonDolan", "Grayson Dolan", tweet)
 tweet = re.sub(r"ApolloBrown", "Apollo Brown", tweet)
 tweet = re.sub(r"saddlebrooke", "Saddlebrooke", tweet)
 tweet = re.sub(r"TontitownGrape", "Tontitown Grape", tweet)
 tweet = re.sub(r"AbbsWinston", "Abbs Winston", tweet)
 tweet = re.sub(r"ShaunKing", "Shaun King", tweet)
 tweet = re.sub(r"MeekMill", "Meek Mill", tweet)
 tweet = re.sub(r"TornadoGiveaway", "Tornado Giveaway", tweet)
 tweet = re.sub(r"GRupdates", "GR updates", tweet)
 tweet = re.sub(r"SouthDowns", "South Downs", tweet)
 tweet = re.sub(r"braininjury", "brain injury", tweet)
 tweet = re.sub(r"auspol", "Australian politics", tweet)
 tweet = re.sub(r"PlannedParenthood", "Planned Parenthood", tweet)
 tweet = re.sub(r"calgaryweather", "Calgary Weather", tweet)
 tweet = re.sub(r"weallheartonedirection", "we all heart one direction", tweet)
 tweet = re.sub(r"edsheeran", "Ed Sheeran", tweet)
 tweet = re.sub(r"TrueHeroes", "True Heroes", tweet)
 tweet = re.sub(r"ComplexMag", "Complex Magazine", tweet)
 tweet = re.sub(r"TheAdvocateMag", "The Advocate Magazine", tweet)
 tweet = re.sub(r"CityofCalgary", "City of Calgary", tweet)
 tweet = re.sub(r"EbolaOutbreak", "Ebola Outbreak", tweet)
 tweet = re.sub(r"SummerFate", "Summer Fate", tweet)
 tweet = re.sub(r"RAmag", "Royal Academy Magazine", tweet)
 tweet = re.sub(r"offers2go", "offers to go", tweet)
 tweet = re.sub(r"ModiMinistry", "Modi Ministry", tweet)
 tweet = re.sub(r"TAXIWAYS", "taxi ways", tweet)
 tweet = re.sub(r"Calum5SOS", "Calum Hood", tweet)
 tweet = re.sub(r"JamesMelville", "James Melville", tweet)
 tweet = re.sub(r"JamaicaObserver", "Jamaica Observer", tweet)
 tweet = re.sub(r"TweetLikeItsSeptember11th2001", "Tweet like it is september 11th 2001", tweet)
 tweet = re.sub(r"cbplawyers", "cbp lawyers", tweet)
 tweet = re.sub(r"fewmoretweets", "few more tweets", tweet)
 tweet = re.sub(r"BlackLivesMatter", "Black Lives Matter", tweet)
 tweet = re.sub(r"NASAHurricane", "NASA Hurricane", tweet)
 tweet = re.sub(r"onlinecommunities", "online communities", tweet)
 tweet = re.sub(r"humanconsumption", "human consumption", tweet)
 tweet = re.sub(r"Typhoon-Devastated", "Typhoon Devastated", tweet)
 tweet = re.sub(r"Meat-Loving", "Meat Loving", tweet)
 tweet = re.sub(r"facialabuse", "facial abuse", tweet)
 tweet = re.sub(r"LakeCounty", "Lake County", tweet)
 tweet = re.sub(r"BeingAuthor", "Being Author", tweet)
 tweet = re.sub(r"withheavenly", "with heavenly", tweet)
 tweet = re.sub(r"thankU", "thank you", tweet)
 tweet = re.sub(r"iTunesMusic", "iTunes Music", tweet)
 tweet = re.sub(r"OffensiveContent", "Offensive Content", tweet)
 tweet = re.sub(r"WorstSummerJob", "Worst Summer Job", tweet)
 tweet = re.sub(r"HarryBeCareful", "Harry Be Careful", tweet)
 tweet = re.sub(r"NASASolarSystem", "NASA Solar System", tweet)
 tweet = re.sub(r"animalrescue", "animal rescue", tweet)
 tweet = re.sub(r"KurtSchlichter", "Kurt Schlichter", tweet)
 tweet = re.sub(r"Throwingknifes", "Throwing knives", tweet)
 tweet = re.sub(r"GodsLove", "God's Love", tweet)
 tweet = re.sub(r"bookboost", "book boost", tweet)
 tweet = re.sub(r"ibooklove", "I book love", tweet)
 tweet = re.sub(r"NestleIndia", "Nestle India", tweet)
 tweet = re.sub(r"realDonaldTrump", "Donald Trump", tweet)
 tweet = re.sub(r"DavidVonderhaar", "David Vonderhaar", tweet)
 tweet = re.sub(r"CecilTheLion", "Cecil The Lion", tweet)
 tweet = re.sub(r"weathernetwork", "weather network", tweet)
 tweet = re.sub(r"GOPDebate", "GOP Debate", tweet)
 tweet = re.sub(r"RickPerry", "Rick Perry", tweet)
 tweet = re.sub(r"frontpage", "front page", tweet)
 tweet = re.sub(r"NewsInTweets", "News In Tweets", tweet)
 tweet = re.sub(r"ViralSpell", "Viral Spell", tweet)
 tweet = re.sub(r"til_now", "until now", tweet)
 tweet = re.sub(r"volcanoinRussia", "volcano in Russia", tweet)
 tweet = re.sub(r"ZippedNews", "Zipped News", tweet)
 tweet = re.sub(r"MicheleBachman", "Michele Bachman", tweet)
 tweet = re.sub(r"53inch", "53 inch", tweet)
 tweet = re.sub(r"KerrickTrial", "Kerrick Trial", tweet)
 tweet = re.sub(r"abstorm", "Alberta Storm", tweet)
 tweet = re.sub(r"Beyhive", "Beyonce hive", tweet)
 tweet = re.sub(r"RockyFire", "Rocky Fire", tweet)
 tweet = re.sub(r"Listen/Buy", "Listen / Buy", tweet)
 tweet = re.sub(r"ArtistsUnited", "Artists United", tweet)
 tweet = re.sub(r"ENGvAUS", "England vs Australia", tweet)
 tweet = re.sub(r"ScottWalker", "Scott Walker", tweet)
 return tweet
total['text'] = total['text'].apply(clean )<define_variables>
df_train.isnull().sum().sort_index() /len(df_train )
tweets = [tweet for tweet in total['text']]
train = total[:len(train)]
test = total[len(train):]<categorify>
df_train.dtypes
g_train =df_train.columns.to_series().groupby(df_train.dtypes ).groups
g_train
def generate_ngrams(text, n_gram=1):
token = [token for token in text.lower().split(' ')if token != '' if token not in wordcloud.STOPWORDS]
ngrams = zip(*[token[i:] for i in range(n_gram)])
 return [' '.join(ngram)for ngram in ngrams]
disaster_unigrams = defaultdict(int)
 for word in total[train['target'] == 1]['text']:
for word in generate_ngrams(word, n_gram = 1):
disaster_unigrams[word] += 1
disaster_unigrams = pd.DataFrame(sorted(disaster_unigrams.items() , key=lambda x: x[1])[::-1])
 nondisaster_unigrams = defaultdict(int)
 for word in total[train['target'] == 0]['text']:
for word in generate_ngrams(word, n_gram = 1):
nondisaster_unigrams[word] += 1
nondisaster_unigrams = pd.DataFrame(sorted(nondisaster_unigrams.items() , key=lambda x: x[1])[::-1])
 disaster_bigrams = defaultdict(int)
 for word in total[train['target'] == 1]['text']:
for word in generate_ngrams(word, n_gram = 2):
disaster_bigrams[word] += 1
disaster_bigrams = pd.DataFrame(sorted(disaster_bigrams.items() , key=lambda x: x[1])[::-1])
 nondisaster_bigrams = defaultdict(int)
 for word in total[train['target'] == 0]['text']:
for word in generate_ngrams(word, n_gram = 2):
nondisaster_bigrams[word] += 1
nondisaster_bigrams = pd.DataFrame(sorted(nondisaster_bigrams.items() , key=lambda x: x[1])[::-1])
 disaster_trigrams = defaultdict(int)
 for word in total[train['target'] == 1]['text']:
for word in generate_ngrams(word, n_gram = 3):
disaster_trigrams[word] += 1
disaster_trigrams = pd.DataFrame(sorted(disaster_trigrams.items() , key=lambda x: x[1])[::-1])
 nondisaster_trigrams = defaultdict(int)
 for word in total[train['target'] == 0]['text']:
for word in generate_ngrams(word, n_gram = 3):
nondisaster_trigrams[word] += 1
nondisaster_trigrams = pd.DataFrame(sorted(nondisaster_trigrams.items() , key=lambda x: x[1])[::-1])
 disaster_4grams = defaultdict(int)
 for word in total[train['target'] == 1]['text']:
for word in generate_ngrams(word, n_gram = 4):
disaster_4grams[word] += 1
disaster_4grams = pd.DataFrame(sorted(disaster_4grams.items() , key=lambda x: x[1])[::-1])
 nondisaster_4grams = defaultdict(int)
 for word in total[train['target'] == 0]['text']:
for word in generate_ngrams(word, n_gram = 4):
nondisaster_4grams[word] += 1
nondisaster_4grams = pd.DataFrame(sorted(nondisaster_4grams.items() , key=lambda x: x[1])[::-1] )<string_transform>
cat_names= [
'Name', 'Sex', 'Ticket', 'Cabin',
'Embarked', 'Name_wiki', 'Hometown',
'Boarded', 'Destination', 'Lifeboat',
'Body'
]
cont_names = [
'PassengerId', 'Pclass', 'SibSp', 'Parch',
'Age', 'Fare', 'WikiId', 'Age_wiki','Class'
]
to_exclude = '*+-/() %
[\\]{|}^_`~\t'
to_tokenize = '!"
tokenizer = Tokenizer(filters = to_exclude)
 text = 'Why are you so f%
text = re.sub(r'(['+to_tokenize+'])', r' \1 ', text)
 tokenizer.fit_on_texts([text])
 print(tokenizer.word_index )<feature_engineering>
splits = RandomSplitter(valid_pct=0.2 )(range_of(df_train))
to = TabularPandas(df_train, procs=[Categorify, FillMissing,Normalize],
cat_names = cat_names,
cont_names = cont_names,
y_names='Survived',
splits=splits )
<string_transform>
g_train =to.train.xs.columns.to_series().groupby(to.train.xs.dtypes ).groups
g_train
tokenizer = Tokenizer()
tokenizer.fit_on_texts(tweets)
 sequences = tokenizer.texts_to_sequences(tweets)
 word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
data = pad_sequences(sequences)
 labels = train['target']
print('Shape of data tensor:', data.shape)
 print('Shape of label tensor:', labels.shape)
 nlp_train = data[:len(train)]
labels = labels
nlp_test = data[len(train):]
MAX_SEQUENCE_LENGTH = data.shape[1]<feature_engineering>
to.train
embeddings_index = {}
with open('.. /input/glove-global-vectors-for-word-representation/glove.6B.200d.txt','r')as f:
for line in tqdm(f):
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
print('Found %s word vectors in the GloVe library' % len(embeddings_index))<define_variables>
to.train.xs
EMBEDDING_DIM = 200<categorify>
X_train, y_train = to.train.xs, to.train.ys.values.ravel()
X_valid, y_valid = to.valid.xs, to.valid.ys.values.ravel()
embedding_matrix = np.zeros(( len(word_index)+ 1, EMBEDDING_DIM))
for word, i in tqdm(word_index.items()):
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
print("Our embedded matrix is of dimension", embedding_matrix.shape )<choose_model_class>
rnf_classifier= RandomForestClassifier(n_estimators=100, n_jobs=-1)
 rnf_classifier.fit(X_train,y_train )
embedding = Embedding(len(word_index)+ 1, EMBEDDING_DIM, weights = [embedding_matrix],
input_length = MAX_SEQUENCE_LENGTH, trainable = False)
<normalization>
y_pred=rnf_classifier.predict(X_valid)
 accuracy_score(y_pred, y_valid )
def scale(df, scaler):
return scaler.fit_transform(df.iloc[:, 2:])
 meta_train = scale(train, StandardScaler())
 meta_test = scale(test, StandardScaler() )<choose_model_class>
df_test.dtypes
g_train =df_test.columns.to_series().groupby(df_test.dtypes ).groups
g_train
def create_lstm(spatial_dropout, dropout, recurrent_dropout, learning_rate, bidirectional = False):
activation = LeakyReLU(alpha = 0.01)
 nlp_input = Input(shape =(MAX_SEQUENCE_LENGTH,), name = 'nlp_input')
 meta_input_train = Input(shape =(7,), name = 'meta_train')
 emb = embedding(nlp_input)
 emb = SpatialDropout1D(dropout )(emb)
 if bidirectional:
nlp_out =(Bidirectional(LSTM(100, dropout = dropout, recurrent_dropout = recurrent_dropout,
kernel_initializer = 'orthogonal')) )(emb)
 else:
nlp_out =(LSTM(100, dropout = dropout, recurrent_dropout = recurrent_dropout,
kernel_initializer = 'orthogonal'))(emb)
 x = Concatenate()([nlp_out, meta_input_train])
 x = Dropout(dropout )(x)
 preds = Dense(1, activation='sigmoid', kernel_regularizer = regularizers.l2(1e-4))(x)
 model = Model(inputs=[nlp_input , meta_input_train], outputs = preds)
 optimizer = Adam(learning_rate = learning_rate)
 model.compile(loss = 'binary_crossentropy', optimizer = optimizer, metrics = ['accuracy'])
 return model<choose_model_class>
cat_names= [
'Name', 'Sex', 'Ticket', 'Cabin',
'Embarked', 'Name_wiki', 'Hometown',
'Boarded', 'Destination', 'Lifeboat',
'Body'
]
cont_names = [
'PassengerId', 'Pclass', 'SibSp', 'Parch',
'Age', 'Fare', 'WikiId', 'Age_wiki','Class'
]
lstm = create_lstm(spatial_dropout =.2, dropout =.2, recurrent_dropout =.2,
learning_rate = 3e-4, bidirectional = True)
 lstm.summary()<train_model>
test = TabularPandas(df_test, procs=[Categorify, FillMissing,Normalize],
cat_names = cat_names,
cont_names = cont_names,
)
history1 = lstm.fit([nlp_train, meta_train], labels, validation_split =.2,
epochs = 5, batch_size = 21, verbose = 1 )<choose_model_class>
X_test= test.train.xs
callback = EarlyStopping(monitor = 'val_loss', patience = 4)
<choose_model_class>
X_test.dtypes
g_train =X_test.columns.to_series().groupby(X_test.dtypes ).groups
g_train
def create_lstm_2(spatial_dropout, dropout, recurrent_dropout, learning_rate, bidirectional = False):
activation = LeakyReLU(alpha = 0.01)
 nlp_input = Input(shape =(MAX_SEQUENCE_LENGTH,), name = 'nlp_input')
 meta_input_train = Input(shape =(7,), name = 'meta_train')
 emb = embedding(nlp_input)
 emb = SpatialDropout1D(dropout )(emb)
 if bidirectional:
nlp_out =(Bidirectional(LSTM(100, dropout = dropout, recurrent_dropout = recurrent_dropout,
kernel_initializer = 'orthogonal')) )(emb)
 else:
nlp_out =(LSTM(100, dropout = dropout, recurrent_dropout = recurrent_dropout,
kernel_initializer = 'orthogonal'))(emb)
 x = Concatenate()([nlp_out, meta_input_train])
 x = Dropout(dropout )(x)
 x =(Dense(100, activation = activation, kernel_regularizer = regularizers.l2(1e-4),
kernel_initializer = 'he_normal'))(x)
 x = Dropout(dropout )(x)
 preds = Dense(1, activation='sigmoid', kernel_regularizer = regularizers.l2(1e-4))(x)
 model = Model(inputs=[nlp_input , meta_input_train], outputs = preds)
 optimizer = Adam(learning_rate = learning_rate)
 model.compile(loss = 'binary_crossentropy', optimizer = optimizer, metrics = ['accuracy'])
 return model<choose_model_class>
X_test= X_test.drop('Fare_na', axis=1 )
lstm_2 = create_lstm_2(spatial_dropout =.4, dropout =.4, recurrent_dropout =.4,
learning_rate = 3e-4, bidirectional = True)
 lstm_2.summary()<train_model>
y_pred=rnf_classifier.predict(X_test)
history2 = lstm_2.fit([nlp_train, meta_train], labels, validation_split =.2,
epochs = 30, batch_size = 21, verbose = 1 )<predict_on_test>
y_pred= y_pred.astype(int )
<choose_model_class><EOS> 
output= pd.DataFrame({'PassengerId':df_test.PassengerId, 'Survived': y_pred})
 output.to_csv('my_submission_titanic.csv', index=False)
 output.head()