Kaggle competition: Titanic - Machine Learning from Disaster
gkf = GroupKFold(n_splits=20 ).split(X=df_train.question_body, groups=df_train.question_body)
 outputs = compute_output_arrays(df_train, output_categories)
 inputs = compute_input_arays(df_train, input_categories, tokenizer, MAX_SEQUENCE_LENGTH)
 test_inputs = compute_input_arays(df_test, input_categories, tokenizer, MAX_SEQUENCE_LENGTH )<choose_model_class>
X_full = pd.read_csv(".. /input/train.csv",index_col=0)
 X_full_test = pd.read_csv(".. /input/test.csv",index_col=0)
 X_full.shape
histories = []
for fold,(train_idx, valid_idx)in enumerate(gkf):
if fold < 3:
K.clear_session()
model = bert_model()
train_inputs = [inputs[i][train_idx] for i in range(3)]
train_outputs = outputs[train_idx]
valid_inputs = [inputs[i][valid_idx] for i in range(3)]
valid_outputs = outputs[valid_idx]
history = train_and_predict(model,
train_data=(train_inputs, train_outputs),
valid_data=(valid_inputs, valid_outputs),
test_data=test_inputs,
learning_rate=3e-5, epochs=5, batch_size=8,
loss_function='binary_crossentropy', fold=fold)
 histories.append(history )<save_to_csv>
y = X_full.Survived
features = ['Pclass','Sex','Age','SibSp','Parch']
X = X_full[features].copy()
X_test = X_full_test[features].copy()
X_train, X_valid, y_train, y_valid = train_test_split(X,y,train_size=0.8,test_size=0.2,random_state=0)
 X.isnull().sum()
test_predictions = [histories[i].test_predictions for i in range(len(histories)) ]
test_predictions = [np.average(test_predictions[i], axis=0)for i in range(len(test_predictions)) ]
test_predictions = np.mean(test_predictions, axis=0)
 df_sub.iloc[:, 1:] = test_predictions
df_sub.to_csv('submission.csv', index=False )<set_options>
X_train.fillna(X_train.mean() ,inplace=True)
 X_valid.fillna(X_valid.mean() ,inplace=True)
 X_test.fillna(X_valid.mean() ,inplace=True)
 X_train.head()
np.set_printoptions(suppress=True )<load_from_csv>
label_encoder = LabelEncoder()
X_train['Sex'] = label_encoder.fit_transform(X_train['Sex'])
 X_valid['Sex'] = label_encoder.transform(X_valid['Sex'])
 X_test['Sex'] = label_encoder.fit_transform(X_test['Sex'])
 X_train.head()
PATH = '.. /input/google-quest-challenge/'
BERT_PATH = '.. /input/bert-base-from-tfhub/bert_en_uncased_L-12_H-768_A-12'
tokenizer = tokenization.FullTokenizer(BERT_PATH+'/assets/vocab.txt', True)
 MAX_SEQUENCE_LENGTH = 512
df_train = pd.read_csv(PATH+'train.csv')
 df_test = pd.read_csv(PATH+'test.csv')
 df_sub = pd.read_csv(PATH+'sample_submission.csv')
 print('train shape =', df_train.shape)
 print('test shape =', df_test.shape)
 output_categories = list(df_train.columns[11:])
 input_categories = list(df_train.columns[[1,2,5]])
 print('
output categories:
\t', output_categories)
 print('
input categories:
\t', input_categories )<define_variables>
my_model = XGBClassifier(n_estimators=1000, learning_rate=0.05)
 my_model.fit(X_train, y_train,
early_stopping_rounds=5,
eval_set=[(X_valid, y_valid)],
verbose=False )
targets = [
'question_asker_intent_understanding',
'question_body_critical',
'question_conversational',
'question_expect_short_answer',
'question_fact_seeking',
'question_has_commonly_accepted_answer',
'question_interestingness_others',
'question_interestingness_self',
'question_multi_intent',
'question_not_really_a_question',
'question_opinion_seeking',
'question_type_choice',
'question_type_compare',
'question_type_consequence',
'question_type_definition',
'question_type_entity',
'question_type_instructions',
'question_type_procedure',
'question_type_reason_explanation',
'question_type_spelling',
'question_well_written',
'answer_helpful',
'answer_level_of_information',
'answer_plausible',
'answer_relevance',
'answer_satisfaction',
'answer_type_instructions',
'answer_type_procedure',
'answer_type_reason_explanation',
'answer_well_written'
]
input_columns = ['question_title','question_body','answer']<feature_engineering>
predictions = my_model.predict(X_valid)
 print("Accuracy Score: " + str(accuracy_score(predictions, y_valid)) )
eng_stopwords = set(stopwords.words("english"))
def include_window_datas(df_q):
out_df = pd.DataFrame()
' can be used to count the number of sentences in each comment
out_df['count_sent']=df_q["question_body"].apply(lambda x: len(re.findall("
",str(x)))+1)
 out_df['count_word']=df_q["question_body"].apply(lambda x: len(str(x ).split()))
out_df['count_unique_word']=df_q["question_body"].apply(lambda x: len(set(str(x ).split())))
 out_df['count_letters']=df_q["question_body"].apply(lambda x: len(str(x)))
 out_df["count_punctuations"] =df_q["question_body"].apply(lambda x: len([c for c in str(x)if c in string.punctuation]))
out_df["count_words_upper"] = df_q["question_body"].apply(lambda x: len([w for w in str(x ).split() if w.isupper() ]))
out_df["count_words_title"] = df_q["question_body"].apply(lambda x: len([w for w in str(x ).split() if w.istitle() ]))
out_df["count_stopwords"] = df_q["question_body"].apply(lambda x: len([w for w in str(x ).lower().split() if w in eng_stopwords]))
out_df['answer_count_word']=df_q["answer"].apply(lambda x: len(str(x ).split()))
out_df['question_title_count_word']=df_q["question_title"].apply(lambda x: len(str(x ).split()))
return out_df
df_train_add_features = include_window_datas(df_train)
 df_test_add_features = include_window_datas(df_test)
 ADD_FEATURES_COL_LEN = len(df_train_add_features.columns)
<normalization>
preds = my_model.predict(X_valid)
 preds_test = my_model.predict(X_test)
sc = StandardScaler()
train_add_features = sc.fit_transform(df_train_add_features)
 test_add_features = sc.fit_transform(df_test_add_features)
 train_add_features[:5]<string_transform>
features = ['Pclass','Sex','Age','SibSp','Embarked']
X = X_full[features].copy()
X_test = X_full_test[features].copy()
X.fillna(X.mean() ,inplace=True)
 X_test.fillna(X_test.mean() ,inplace=True)
 X['Embarked'] = X['Embarked'].fillna('S')
 X_test['Embarked'] = X_test['Embarked'].fillna('S')
 X['Sex'] = label_encoder.fit_transform(X['Sex'])
 X_test['Sex'] = label_encoder.transform(X_test['Sex'])
 X['Embarked'] = label_encoder.fit_transform(X['Embarked'])
 X_test['Embarked'] = label_encoder.transform(X_test['Embarked'])
 X_train, X_valid, y_train, y_valid = train_test_split(X,y,train_size=0.8,test_size=0.2,random_state=0)
 my_model.fit(X_train, y_train,
early_stopping_rounds=5,
eval_set=[(X_valid, y_valid)],
verbose=False)
 predictions = my_model.predict(X_valid)
 print("Accuracy Score: " + str(accuracy_score(predictions, y_valid)))
 preds = my_model.predict(X_valid)
 preds_test = my_model.predict(X_test)
<compute_test_metric><EOS> 
output = pd.DataFrame({'PassengerId': X_test.index,
'Survived': preds_test})
 output.to_csv('submission.csv', index=False )