Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<split>
warnings.simplefilter(action='ignore', category=FutureWarning)
gkf = GroupKFold(n_splits=5 ).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, train_add_features,tokenizer, MAX_SEQUENCE_LENGTH)
 test_inputs = compute_input_arays(df_test, input_categories, test_add_features,tokenizer, MAX_SEQUENCE_LENGTH )<choose_model_class>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print(train.shape)
 print(test.shape )
histories = []
count = 1
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(len(inputs)) ]
train_outputs = outputs[train_idx]
valid_inputs = [inputs[i][valid_idx] for i in range(len(test_inputs)) ]
valid_outputs = outputs[valid_idx]
model.summary()
name = "model_" + str(count)+ ".png"
tf.keras.utils.plot_model(model, to_file=name)
 count +=1
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>
test.drop(['Ticket','Cabin'],axis=1, inplace=True)
 test.set_index('PassengerId',inplace=True)
 train.drop(['Ticket','Cabin'],axis=1, inplace=True)
 train.set_index('PassengerId',inplace=True)
 train.head()
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 )<import_modules>
test['title'] = test.Name.str.extract('([A-Za-z]+)\.')
 test['title'].replace(['Mlle','Mme','Ms','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don','Dona','Dr'],['Miss','Miss','Miss','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr','Miss','Mr'],inplace=True)
 test = pd.concat([test.drop(['title','Name'],axis=1), pd.get_dummies(test[['title']], drop_first=True)], axis=1)
 train['title'] = train.Name.str.extract('([A-Za-z]+)\.')
 train['title'].replace(['Mlle','Mme','Ms','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don','Dona','Dr'],['Miss','Miss','Miss','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr','Miss','Mr'],inplace=True)
 train = pd.concat([train.drop(['title','Name'],axis=1), pd.get_dummies(train[['title']], drop_first=True)], axis=1)
 train.head()
import pandas as pd<import_modules>
features_numeric = ['Age','SibSp','Parch','Fare']
features_label = ['Pclass','Sex','Embarked']
test = test.fillna(test[features_numeric].mean())
 test = test.fillna(test[features_label].mode().iloc[0])
 train = train.fillna(train[features_numeric].mean())
 train = train.fillna(train[features_label].mode().iloc[0])
 train.info()
import pandas as pd<load_from_csv>
test = pd.concat([test.drop(['Sex'],axis=1), pd.get_dummies(test[['Sex']], drop_first=True)], axis=1)
 train = pd.concat([train.drop(['Sex'],axis=1), pd.get_dummies(train[['Sex']], drop_first=True)], axis=1)
 test = pd.concat([test.drop(['Embarked'],axis=1), pd.get_dummies(test[['Embarked']], drop_first=True)], axis=1)
 train = pd.concat([train.drop(['Embarked'],axis=1), pd.get_dummies(train[['Embarked']], drop_first=True)], axis=1)
 test['Family'] = test['SibSp'] + test['Parch']
test['Alone'] = 0
test.loc[test.Family==0,'Alone'] = 1
test.drop(['SibSp','Parch'],axis=1,inplace=True)
 train['Family'] = train['SibSp'] + train['Parch']
train['Alone'] = 0
train.loc[train.Family==0,'Alone'] = 1
train.drop(['SibSp','Parch'],axis=1,inplace=True)
 train.head()
limit = 20_000_000
usecols = ['ip', 'app', 'device', 'os', 'channel', 'click_time', 'is_attributed']<load_from_csv>
competition_data = pd.read_csv('.. /input/talkingdata-adtracking-fraud-detection/train.csv', nrows=limit, usecols=usecols, parse_dates=['click_time'] )<count_values>
X,y = train.drop(['Survived'],axis=1), train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
 print(X.shape)
 print(X_train.shape)
 print(X_test.shape)
 print(y.shape)
 print(y_train.shape)
 print(y_test.shape )
competition_data['is_attributed'].value_counts()<count_values>
classifiers = [
KNeighborsClassifier(3), SVC(probability=True), DecisionTreeClassifier() , RandomForestClassifier() , AdaBoostClassifier() ,
GradientBoostingClassifier() , GaussianNB() , LinearDiscriminantAnalysis() , QuadraticDiscriminantAnalysis() , LogisticRegression() ,
XGBClassifier()
]
log_cols = ["Classifier", "Accuracy"]
log = pd.DataFrame(columns=log_cols)
 acc_dict = {}
for clf in classifiers:
name = clf.__class__.__name__
clf.fit(X_train, y_train)
 train_predictions = clf.predict(X_test)
 acc = accuracy_score(y_test, train_predictions)
 if name in acc_dict:
acc_dict[name] += acc
else:
acc_dict[name] = acc
for clf in acc_dict:
acc_dict[clf] = acc_dict[clf]
log_entry = pd.DataFrame([[clf, acc_dict[clf]]], columns=log_cols)
 log = log.append(log_entry)
 plt.figure(figsize=(10,10))
plt.xlabel('Accuracy')
 plt.title('Classifier Accuracy')
 plt.grid(True)
 sns.barplot(x='Accuracy', y='Classifier', data=log, color="b");
competition_data['is_attributed'].value_counts(normalize=True )<load_from_csv>
xgb = XGBClassifier()
xgb.fit(X_train, y_train, early_stopping_rounds=50, eval_metric='auc', eval_set=[(X_train, y_train),(X_test, y_test)] )
click_data = pd.read_csv('.. /input/feature-engineering-data/train_sample.csv', nrows=limit, usecols=usecols, parse_dates=['click_time'] )<count_values>
survivors = xgb.predict(test)
 submission = pd.DataFrame({'PassengerId':test.index,'Survived':survivors})
 submission.head()
<count_values><EOS> 
submission.to_csv('Titanic_Survivors_Predictions.csv',index=False )