Kaggle competition: Titanic - Machine Learning from Disaster
def build_model() :
ids = tf.keras.layers.Input(( max_len,), dtype=tf.int32)
 att = tf.keras.layers.Input(( max_len,), dtype=tf.int32)
 tok = tf.keras.layers.Input(( max_len,), dtype=tf.int32)
 config_path = RobertaConfig.from_pretrained(config_file)
 roberta_model = TFRobertaModel.from_pretrained(pretrained_file, config=config_path)
 x = roberta_model(ids, attention_mask = att, token_type_ids=tok)
 x1 = tf.keras.layers.Dropout(0.05 )(x[0])
 x1 = tf.keras.layers.Conv1D(128, 2,padding='same' )(x1)
 x1 = tf.keras.layers.LeakyReLU()(x1)
 x1 = tf.keras.layers.Conv1D(64, 2,padding='same' )(x1)
 x1 = tf.keras.layers.Dense(1 )(x1)
 x1 = tf.keras.layers.Flatten()(x1)
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.05 )(x[0])
 x2 = tf.keras.layers.Conv1D(128, 2,padding='same' )(x2)
 x2 = tf.keras.layers.LeakyReLU()(x2)
 x2 = tf.keras.layers.Conv1D(64, 2, padding='same' )(x2)
 x2 = tf.keras.layers.Dense(1 )(x2)
 x2 = tf.keras.layers.Flatten()(x2)
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 model = tf.keras.models.Model(inputs=[ids, att, tok], outputs=[x1,x2])
 optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
 model.compile(loss=custom_loss, optimizer=optimizer)
 return model<save_to_csv>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
<set_options><EOS> 
frames = [train_data,test_data]
full_data = pd.concat(frames)
 master_data = full_data
print(train_data.shape)
 print(test_data.shape)
 print(full_data.shape)
 full_data = full_data.drop(['PassengerId','Survived','Ticket','Cabin'],axis = 1)
 print(full_data.shape)
 full_data['Name'] = full_data['Name'].str.rsplit(',' ).str[-1]
full_data['Name'] = full_data['Name'].str.split('.' ).str[0]
full_data['Name'] = full_data['Name'].replace(['Lady','the Countess','Capt', 'Col',
'Dona','Don', 'Dr', 'Major','','Rev', 'Sir', 'Jonkheer'], 'Rare',regex=True)
 full_data['Name'] = full_data['Name'].replace('Mlle', 'Miss', regex=True)
 full_data['Name'] = full_data['Name'].replace('Ms', 'Miss',regex=True)
 full_data['Name'] = full_data['Name'].replace('Mme', 'Mrs',regex=True)
 pd.crosstab(full_data['Name'], full_data['Sex'])
 full_data.dropna(subset = ["Age"], inplace=True)
 print(np.where(pd.isnull(full_data)))
 full_data.dropna(how='any', inplace=True)
 print(np.where(pd.isnull(full_data)))
 y = full_data['Age']
X = full_data.drop(['Age'],axis = 1)
 X_train ,X_test ,y_train ,y_test = train_test_split(X ,y ,test_size=0.2,random_state = 14)
 numerical_features = ['SibSp','Parch','Fare']
categorical_features = ['Sex','Embarked','Pclass','Name']
numerical_pipeline = make_pipeline(MinMaxScaler())
 categorical_pipeline = make_pipeline(OneHotEncoder())
 preprocessor = make_column_transformer(( numerical_pipeline, numerical_features),(categorical_pipeline, categorical_features))
model = make_pipeline(preprocessor, GradientBoostingRegressor())
 model.fit(X_train, y_train)
 params = {'gradientboostingregressor__n_estimators' : [2000]
, 'gradientboostingregressor__learning_rate' : [0.01]}
grid = GridSearchCV(model, param_grid= params, cv=5)
 grid.fit(X_train, y_train)
 print(grid.best_params_)
 print('train score :' , grid.best_score_)
 best_model = grid.best_estimator_
print('test score :', best_model.score(X_test,y_test))
train_data.dropna(subset = ["Fare","Embarked"], inplace=True)
 train_data = train_data.drop(['Ticket','Cabin'], axis = 1)
 print(train_data.shape)
 train_data['Name'] = train_data['Name'].str.rsplit(',' ).str[-1]
train_data['Name'] = train_data['Name'].str.split('.' ).str[0]
train_data['Name'] = train_data['Name'].replace(['Lady','the Countess','Capt','Col','Dona','Don', 'Dr','Major','','Rev','Sir','Jonkheer'], 'Rare',regex=True)
 train_data['Name'] = train_data['Name'].replace('Mlle', 'Miss', regex=True)
 train_data['Name'] = train_data['Name'].replace('Ms', 'Miss',regex=True)
 train_data['Name'] = train_data['Name'].replace('Mme', 'Mrs',regex=True)
 train_data
train_data['age_predicted'] = best_model.predict(train_data)
 train_data['Age'].fillna(train_data['age_predicted'], inplace=True)
 train_data = train_data.drop(['age_predicted'],axis = 1)
 train_data
train_data['family_size'] = train_data['SibSp'] + train_data['Parch']+1
train_data
split = [0, 3, 14, 24, 34, 44, 54, 64]
names = ['0', '1', '2', '3', '4','5','6','7']
d = dict(enumerate(names, 1))
train_data['Age'] = np.vectorize(d.get )(np.digitize(train_data['Age'], split))
train_data
master_y = train_data['Survived']
master_X = train_data.drop(['Survived'], axis = 1)
 print('master_y',master_y.shape)
 print('master_X',master_X.shape)
 master_X_train ,master_X_test ,master_y_train ,master_y_test = train_test_split(master_X ,master_y ,test_size=0.2,random_state = 14)
 master_numerical_features = ['SibSp','Parch','Fare']
master_categorical_features = ['Sex','Embarked','Pclass','Name','family_size']
master_numerical_pipeline = make_pipeline(MinMaxScaler())
 master_categorical_pipeline = make_pipeline(OneHotEncoder())
 master_preprocessor = make_column_transformer(( master_numerical_pipeline, master_numerical_features),(master_categorical_pipeline, master_categorical_features))
master_model = make_pipeline(master_preprocessor, GradientBoostingClassifier())
 master_model.fit(master_X_train, master_y_train)
 master_model.fit(master_X_test, master_y_test)
 master_params = {'gradientboostingclassifier__n_estimators' : [1000]
, 'gradientboostingclassifier__learning_rate' : [0.01]}
master_grid = GridSearchCV(master_model, param_grid= master_params, cv=5)
 master_grid.fit(master_X_train, master_y_train)
 print(master_grid.best_params_)
 print('Master train score :' , master_grid.best_score_)
 master_best_model = master_grid.best_estimator_
print('Master test score :', master_best_model.score(master_X_test,master_y_test))
test_data = test_data.drop(['Ticket','Cabin'], axis = 1)
 test_data['Fare'].fillna(value = test_data['Fare'].median() , inplace = True)
 test_data['Name'] = test_data['Name'].str.rsplit(',' ).str[-1]
test_data['Name'] = test_data['Name'].str.split('.' ).str[0]
test_data['Name'] = test_data['Name'].replace(['Lady','the Countess','Capt','Col','Dona','Don', 'Dr','Major','','Rev','Sir','Jonkheer'], 'Rare',regex=True)
 test_data['Name'] = test_data['Name'].replace('Mlle', 'Miss', regex=True)
 test_data['Name'] = test_data['Name'].replace('Ms', 'Miss',regex=True)
 test_data['Name'] = test_data['Name'].replace('Mme', 'Mrs',regex=True)
 test_data['age_predicted'] = best_model.predict(test_data)
 test_data['Age'].fillna(test_data['age_predicted'], inplace=True)
 test_data = test_data.drop(['age_predicted'],axis = 1)
 split = [0, 3, 14, 24, 34, 44, 54, 64]
names = ['0', '1', '2', '3', '4','5','6','7']
d = dict(enumerate(names, 1))
test_data['Age'] = np.vectorize(d.get )(np.digitize(test_data['Age'], split))
test_data
test_data['family_size'] = test_data['SibSp'] + test_data['Parch']+1
test_data
predictions = master_best_model.predict(test_data ).astype(int)
 predictions
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!")