Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<feature_engineering>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
test['Age']=test[['Age','Pclass']].apply(impute_age,axis = 1 )<drop_column>
sns.set(style="darkgrid")
 warnings.filterwarnings('ignore' )
test.drop('Cabin',inplace = True,axis =1 )<categorify>
train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 print('Training data shape: ', train.shape)
 train.head()
sex = pd.get_dummies(test['Sex'],drop_first=True)
 embark = pd.get_dummies(test['Embarked'],drop_first=True )<concatenate>
test = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 print('Testing data shape: ', test.shape)
 test.head()
test = pd.concat([test,sex,embark],axis=1 )<drop_column>
train.isnull().sum()
test.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )<correct_missing_values>
test.isnull().sum()
test['Fare'].fillna(test['Fare'].mean() ,inplace=True )<categorify>
train['target'].value_counts()
feature_scale = ['Age','Fare']
test[feature_scale] = st.fit_transform(test[feature_scale] )<import_modules>
train1 = train.copy()
test1 = test.copy()
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier<choose_model_class>
def clean_text(text):

 text = text.lower()
text = re.sub('\[.*?\]', '', text)
 text = re.sub('https?://\S+|www\.\S+', '', text)
 text = re.sub('<.*?>+', '', text)
 text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
 text = re.sub('
', '', text)
 text = re.sub('\w*\d\w*', '', text)
 text = re.sub('[‘’“”…]', '', text)
 return text
level1 = LogisticRegression()
model = StackingClassifier(estimators=level0,final_estimator=level1,cv=5 )<choose_model_class>
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 train1['text'] = train1['text'].apply(lambda x: remove_emoji(x))
test1['text'] = test1['text'].apply(lambda x: remove_emoji(x))
level1 = LogisticRegression()
model = StackingClassifier(estimators=level0,final_estimator=level1,cv=5 )<train_model>
def text_preprocessing(text):

 tokenizer_reg = nltk.tokenize.RegexpTokenizer(r'\w+')
 nopunc = clean_text(text)
 tokenized_text = tokenizer_reg.tokenize(nopunc)
 remove_stopwords = [w for w in tokenized_text if w not in stopwords.words('english')]
combined_text = ' '.join(remove_stopwords)
 return combined_text
train1['text'] = train1['text'].apply(lambda x: text_preprocessing(x))
test1['text'] = test1['text'].apply(lambda x: text_preprocessing(x))
train1['text'].head()
model.fit(x,y )<predict_on_test>
count_vectorizer = CountVectorizer(ngram_range =(1,1), min_df = 1)
 train_vectors = count_vectorizer.fit_transform(train1['text'])
 test_vectors = count_vectorizer.transform(test1["text"])
 train_vectors.shape
y_predicted = model.predict(test )<create_dataframe>
tfidf = TfidfVectorizer(ngram_range=(1, 2), min_df = 2, max_df = 0.5)
 train_tfidf = tfidf.fit_transform(train1['text'])
 test_tfidf = tfidf.transform(test1["text"])
 train_tfidf.shape
submission = pd.DataFrame({
"PassengerId":test2['PassengerId'],
"Survived":y_predicted
} )<save_to_csv>
logreg_bow = LogisticRegression(C=1.0)
 scores = model_selection.cross_val_score(logreg_bow, train_vectors, train["target"], cv=5, scoring="f1")
 scores.mean()
submission.to_csv('first_kaggale_titanic_submission.csv',index=False )<load_from_csv>
logreg_tfidf = LogisticRegression(C=1.0)
 scores = model_selection.cross_val_score(logreg_tfidf, train_tfidf, train["target"], cv=5, scoring="f1")
 scores.mean()
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
NB_bow = MultinomialNB()
scores = model_selection.cross_val_score(NB_bow, train_vectors, train["target"], cv=5, scoring="f1")
 scores.mean()
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<feature_engineering>
NB_tfidf = MultinomialNB()
scores = model_selection.cross_val_score(NB_tfidf, train_tfidf, train["target"], cv=5, scoring="f1")
 scores.mean()
train_data['FamilySize'] = train_data['SibSp'] + train_data['Parch'] + 1
test_data['FamilySize'] = test_data['SibSp'] + test_data['Parch'] + 1 
<feature_engineering>
NB_bow.fit(train_vectors, train["target"] )
train_data['Fare'].fillna(train_data['Fare'].median() , inplace=True)
 test_data['Fare'].fillna(test_data['Fare'].median() , inplace=True)
<define_variables>
sample_submission = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv')
 sample_submission["target"] = NB_bow.predict(test_vectors)
 os.chdir('/kaggle/working')
 sample_submission.to_csv("submission1.csv", index=False )
women = train_data.loc[train_data['Sex']=='female']['Survived']
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )<define_variables>
def bert_encode(texts, tokenizer, max_len = 512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )
men = train_data.loc[train_data['Sex']=='male']['Survived']
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )<categorify>
def build_model(bert_layer, max_len = 512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model
train_data.Sex = pd.get_dummies(train_data.Sex)
 test_data.Sex = pd.get_dummies(test_data.Sex )<data_type_conversions>
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )
train_data['Age'].fillna(train_data['Age'].median() , inplace=True)
 test_data['Age'].fillna(test_data['Age'].median() , inplace=True)
<feature_engineering>
train_data['IsAlone'] = 0
train_data.loc[train_data['FamilySize'] == 1, 'IsAlone'] = 1
test_data['IsAlone'] = 0
test_data.loc[test_data['FamilySize'] == 1, 'IsAlone'] = 1
train_data['Age*Class'] = train_data.Age * train_data.Pclass
test_data['Age*Class'] = test_data.Age * test_data.Pclass<filter>
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )
old = train_data.loc[train_data['Age'] > 45]['Survived']
rate_old = sum(old)/len(old)
 print("% of old people who survived:", rate_old )<filter>
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )
mid_age = train_data.loc[(train_data['Age'] <= 45)&(train_data['Age'] >= 15)]['Survived']
rate_mid = sum(mid_age)/len(mid_age)
 print("% of middle aged people who survived:", rate_mid )<define_variables>
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 test_input = bert_encode(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values
young = train_data.loc[train_data['Age'] < 15]['Survived']
rate_young = sum(young)/len(young)
 print("% of young people who survived:", rate_young )<data_type_conversions>
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
callbacks=[checkpoint],
batch_size=16
)
train_data['Age'] = pd.cut(train_data['Age'], bins=[0., 10., 25., 50, 80, np.inf], labels=[0,1,2,3,4] ).astype(int)
 test_data['Age'] = pd.cut(test_data['Age'], bins=[0., 10., 25., 50, 80, np.inf], labels=[0,1,2,3,4] ).astype(int)
<count_missing_values>
model.load_weights('model.h5')
 test_pred = model.predict(test_input )
<categorify><EOS> 
submission['target'] = test_pred.round().astype(int)
 os.chdir('/kaggle/working')
 submission.to_csv("submission2.csv", index=False )