Kaggle competition: Titanic - Machine Learning from Disaster
numerical_transformer_m = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='mean')) , 
('scaler', StandardScaler())])
 numerical_transformer_c = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant', fill_value=0)) , 
('scaler', StandardScaler())])
 categorical_transformer_mf = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown = 'ignore', sparse = False))
])
 categorical_transformer_c = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant', fill_value='NA')) , 
('onehot', OneHotEncoder(handle_unknown = 'ignore', sparse = False))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num_mean', numerical_transformer_m, mean_num_cols), 
('num_constant', numerical_transformer_c, constant_num_cols), 
('cat_mf', categorical_transformer_mf, mf_categorical_cols), 
('cat_c', categorical_transformer_c, constant_categorical_cols)
 ] )<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data["Fare"].fillna(train_data.Fare.mean() ,inplace=True)
 train_data["Embarked"].fillna(train_data.Embarked.mode() ,inplace=True)
 print(train_data.query('PassengerId == 889'))
model = XGBRegressor(learning_rate = 0.01,
n_estimators=2500,
max_depth=4,
min_child_weight=1,
gamma=0,
subsample=0.7,
colsample_bytree=0.6,
reg_alpha = 0.1,
reg_lambda = 1.25 )<train_model>
by_sex_class_train = train_data.groupby(['Sex', 'Pclass'])
 def impute_median(series):
return series.fillna(series.median())
 train_data["Age"].fillna(by_sex_class_train['Age'].transform(impute_median),inplace=True)
 print(train_data.query('PassengerId == 889'))
X_valid_eval = preprocessor.fit(X_train, y_train ).transform(X_valid )<train_model>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 by_sex_class_test = test_data.groupby(['Sex', 'Pclass'])
 test_data["Fare"].fillna(test_data.Fare.mean() ,inplace=True)
 test_data["Embarked"].fillna(test_data.Embarked.mode() ,inplace=True)
 test_data["Age"].fillna(by_sex_class_test['Age'].transform(impute_median),inplace=True)
fit_params = {"model__early_stopping_rounds": 50,
"model__eval_set": [(X_valid_eval, y_valid)],
"model__verbose": False,
"model__eval_metric" : "rmsle"}<train_model>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )
my_pipeline = Pipeline(steps=[('preprocessor', preprocessor), 
('model', model)
 ])
 my_pipeline.fit(X_train, y_train, **fit_params)
 preds = my_pipeline.predict(X_valid)
 score = calculate_root_mean_squared_log_error(y_valid,preds)
 print("Score: {}".format(score)) 
<prepare_output>
men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )
<compute_train_metric><EOS> 
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch","Age", "Embarked"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission_14.csv', index=False)
 print("Your submission 14 was successfully saved!" )